简体   繁体   English

我无法处理JavaScript冲突。 您应该如何检查它们?

[英]I can't get my JavaScript collisions to work. How should you check for them?

I have been working on a small physics game in JS where you use a block to move other blocks and they collide. 我一直在用JS开发一个小型的物理游戏,在该游戏中,您使用一个块移动其他块并使它们碰撞。 My collisions sometimes stop working, and I do not know why. 我的碰撞有时会停止,我也不知道为什么。 I'm also not sure if I'm checking for them the correct way. 我也不确定是否要检查它们的正确方法。

this.checkCollision = function(otherobj) {
    var myleft = this.x;
    var myright = this.x + (this.width);
    var mytop = this.y;
    var mybottom = this.y + (this.height);
    var otherleft = otherobj.x;
    var otherright = otherobj.x + (otherobj.width);
    var othertop = otherobj.y;
    var otherbottom = otherobj.y + (otherobj.height);
    var crash = true;
    if ((mybottom < othertop) ||
        (mytop > otherbottom) ||
        (myright < otherleft) ||
        (myleft > otherright)) {
        crash = false;
    }
    if (crash) {
        this.colliding = true;
        $('body').css('background-color', 'red');
        this.collide(otherobj);
        // this.x++;
    } else {
        $('body').css('background-color', 'white');
        this.colliding = false;
    }
    return crash;
}
this.collide = function(otherobj) {
    //for when they stick
    if (this.speedX < 0.1 && this.speedY < 0.1) {
        this.x -= 2;
        otherobj.x += 2;
    }
    //get speed of x
    otherobj.speedX = this.speedX;
    this.speedX = -((this.mass * this.speedX) + (otherobj.mass * otherobj.speedX)) / (this.mass + otherobj.mass);
    // get speed of y
    otherobj.speedY = this.speedY;
    this.speedY = -((this.mass * this.speedY) + (otherobj.mass * otherobj.speedY)) / (this.mass + otherobj.mass);
}

Fiddle is Here 小提琴在这里

Your collision detection code seems to work in the example given if you set blueGamePiece.speedX = -5 . 如果设置blueGamePiece.speedX = -5则碰撞检测代码在给定的示例中似乎可以使用。 If your goal is to make the two bodies exchange velocities when they collide, the following will help: 如果您的目标是使两个物体在碰撞时交换速度,则以下方法将有所帮助:

this.collide = function (otherobj) {
    // swap X speeds
    temp = otherobj.speedX;
    otherobj.speedX = this.speedX;
    this.speedX = temp;
    // swap Y speeds
    temp = otherobj.speedY;
    otherobj.speedY = this.speedY;
    this.speedY = temp;
}

This simply swaps the velocities without negating (notice the temporary temp variables), which causes the collisions to work fine, and for the objects to move in opposite directions. 这样就可以简单地交换速度而不会取反(请注意临时的temp变量),这会使碰撞正常进行,并使对象沿相反的方向移动。 If you want to use proper momentum, you can try to adjust the code to use the relevant formulae. 如果要使用适当的动量,则可以尝试调整代码以使用相关公式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的 JavaScript 不起作用。 我怎么解决这个问题? - My JavaScript doesn't work. How can I solve this problem? 我的 Javascript AVG 计算器不工作。 我该如何解决? - My Javascript AVG Calculator doesn't work. How can i fix? 尝试异步 Javascript,无法正常工作。 更新 - Trying Asynchronous Javascript, can't get it to work. UPDATED 我无法使有角度的ui路由器嵌套状态正常工作。 里面的代码 - I can't get my angular ui-router nested states to work. Code inside document.getElementsByTagName 不起作用。 如何获取body的NodeList的长度? - document.getElementsByTagName doesn't work. How can I get the length of NodeList of body? 我的Javascript不起作用。不知道它是函数,我是如何调用它,我使用的CSS或者是什么 - My Javascript doesn't work. Don't know if it's the Function, how I'm calling it, the CSS I used or what VUEJS 组件将我的显示路由添加到他们的 get 调用中,我怎样才能让我的 journals/:id 页面工作。 哑剧错误 - VUEJS components adding my show route to their get call, how can I get my journals/:id pages to work. Mime Error 我想使用ajax获取数据表单数据库,但它不起作用。 我应该改变什么? - i want to get data form database using ajax, but it doesn't work. what should i change? 我正在尝试使用javascript更改表单元格的属性,并且找到了我的建议,但似乎无法使它们正常工作 - I am trying to change the properties of a table cell with javascript and have found my suggestions but I can't seem to get them to work 试图使webGL(2d)绘图起作用。 看不到我的三角形 - Trying to get webGL (2d) drawing to work. Can't see my triangles
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM