[英]Bug in collision detecting algorithm, Javascript
OK so I'm making a 2D platform game in JavaScript and for my collision detecting I have 2 types of objects: game objects which are detected by focus areas and focus areas; 好的,所以我正在用JavaScript制作2D平台游戏,并且为了进行碰撞检测,我有两种类型的对象:被焦点区域和焦点区域检测到的游戏对象; which detect the objects both have 4 coordinates which form a box x1 and y1 are the left/top and x2 and y2 are the left/bottom
检测对象都具有4个坐标,这些坐标形成一个框x1和y1在左侧/顶部,x2和y2在左侧/底部
the game object's box coordinates are unchanging numbers which must be calculated to be relative to the game objects position whereas the focus areas coordinates are updated to be the exact spots 游戏对象的框坐标是不变的数字,必须将其计算为相对于游戏对象的位置,而焦点区域的坐标将更新为确切的位置
my problem is for some reason when this code (below) comes across a focus area that is smaller than the game object (in coordinates) it goes undetected unless the focus area conflicts with the upper-left pixel of the game object's box, how do I fix this? 我的问题是由于某种原因,当此代码(以下)遇到的焦点区域小于游戏对象(在坐标中)时,除非焦点区域与游戏对象框的左上像素冲突,否则它将无法检测到,怎么办我解决这个吗? (by the way this is all in a double 'for loop')
(顺便说一下,这都是双重的“ for循环”)
var O_x=game.objects[ii].phy.pos.x,
O_aur_x_1=game.objects[ii].col.aur.x1,
O_aur_x_2=game.objects[ii].col.aur.x2,
F_x_1=game.focusAreas[i].x1,
F_x_2=game.focusAreas[i].x2,
O_y=game.objects[ii].phy.pos.y,
O_aur_y_1=game.objects[ii].col.aur.y1,
O_aur_y_2=game.objects[ii].col.aur.y2,
F_y_1=game.focusAreas[i].y1,
F_y_2=game.focusAreas[i].y2;
if ( (O_x + O_aur_x_1) >= F_x_1 && (O_x + O_aur_x_1) <= F_x_2 || F_x_1 >= (O_x + O_aur_x_1) && F_x_1 <=(O_x + O_aur_x_2))
{// if object's x1 is within xfocus range
}
else if ( (O_x + O_aur_x_2) >= F_x_1 && (O_x + O_aur_x_2)<= F_x_2 || F_x_2 >= (O_x + O_aur_x_1) && F_x_1 <=(O_x + O_aur_x_2))
{// if object's x2 is within xfocus range
}
else{continue;}
if ( (O_y + O_aur_y_1) >= F_y_1 && (O_y + O_aur_y_1) <= F_y_2 || F_y_1 >= (O_y + O_aur_y_1) && F_y_1 <=(O_y + O_aur_y_2))
{// if object's y1 is within yfocus range
}
else if ( (O_y + O_aur_y_2) >= F_y_1 && (O_y + O_aur_y_2) <= F_y_2 || F_y_2 >= (O_y + O_aur_y_1) && F_y_1 <=(O_y + O_aur_y_2))
{// if object's y2 is within yfocus range
}
else{continue;}
Mind your parentheses! 注意括号!
JS uses C-style short-circuit evaluation, meaning: JS使用C型短路评估,意思是:
So you need an extra pair of brackets to group the expressions on both sides of the or-operator: 因此,您需要额外一对方括号以将or运算符两侧的表达式分组:
if ((ax1 >= bx1 && ax1 <= bx2) || (bx1 >= ax1 && bx1 <= ax2)) {
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.