Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript游戏冲突_Javascript_Collision_Game Physics - Fatal编程技术网

JavaScript游戏冲突

JavaScript游戏冲突,javascript,collision,game-physics,Javascript,Collision,Game Physics,在一个线性战斗游戏中,我使用贝娄公式来获得X碰撞和Y碰撞(当角色跳到另一架战斗机上时)。 我真的不知道这个公式出了什么问题,因为我在游戏中遇到了一些无意义的逻辑错误。(当碰撞发生时,我会让角色前后移动,所以有时会出现无意义的位置。)。 除了我的配方,如果你还有其他配方,请分享。谢谢 if (this.x < secondPlayer.x + secondPlayer.width + this.border && this.x + this.width

在一个线性战斗游戏中,我使用贝娄公式来获得X碰撞和Y碰撞(当角色跳到另一架战斗机上时)。 我真的不知道这个公式出了什么问题,因为我在游戏中遇到了一些无意义的逻辑错误。(当碰撞发生时,我会让角色前后移动,所以有时会出现无意义的位置。)。 除了我的配方,如果你还有其他配方,请分享。谢谢

    if (this.x < secondPlayer.x + secondPlayer.width + this.border &&
        this.x + this.width + this.border > secondPlayer.x && this.y < secondPlayer.y + secondPlayer.height + this.border &&
        this.height + this.y + this.border > secondPlayer.y && this.x != secondPlayer.x){
            return 'x';
        }
        else if (this.x == secondPlayer.x && this.y < secondPlayer.y + secondPlayer.height + this.border &&
            this.height + this.y + this.border > secondPlayer.y) {
                return 'y';
     }else{
         return 'n';
     }
if(this.xsecondPlayer.x&&this.ysecondPlayer.y&&this.x!=secondPlayer.x){
返回'x';
}
如果(this.x==secondPlayer.x&&this.ysecondPlayer.y){
返回“y”;
}否则{
返回'n';
}

我没有时间给你一个精确的答案,只是想把你重定向到。定义一个类似矩形的结构,并检查链接中的边框:

myBox.x = this.x 
myBox.y = this.y
myBox.width = this.width + this.border
myBox.height = this.height + this.border
您也可以采用不同的方法:

myBox.left = this.x 
myBox.top = this.y
myBox.right = this.x + this.width + this.border
myBox.bottom = this.y + this.height + this.border
并检查: 我的左手对抗对手的右手,我的上半身对抗对手的下半身,然后倒立

编辑: 使用代码时,不能同时检查两个冲突:代码显示:

如果(x碰撞)返回“x”,否则如果(y碰撞)返回“y”

因此,如果发生“x”冲突,函数将返回,而不检查“y”。
此外,在第二次测试中,我认为<>代码> x==第二播放器.x/COD>是错误的:只有当两个玩家有相同的位置时才会检查“Y”冲突。但是它们可以重叠而不在同一个“X”上(例如,我在对手的精灵中跳跃和下降)!

是代码>这个。或者一个对象?请解释“border”代表什么。另外,在条件中需要更多的括号!是的,它的值(不是object)对于字符的边框。@NicolasCailloux:我没有语法错误,但我认为可能在Y碰撞中存在一些逻辑问题,因为它必须位于字符之上。单元测试可能会突出这个问题?您可以生成满足if中每个条件的测试数据,然后为不同的条件集混合和匹配它们。这几乎是一样的正如我在commit中解释的那样,我的字符顶部也有Y碰撞,这意味着我应该得到Y碰撞+X碰撞