圆形矩形碰撞检测在 Corona SDK 中不起作用
我需要摆脱游戏中的物理因素,因此我需要实现自定义碰撞检测函数(每一帧都会调用碰撞检测函数)。我想检查一个矩形是否与一个圆相交。我从这里获取了代码(e.James的答案):
bool intersects(CircleType circle, RectType rect)
{
circleDistance.x = abs(circle.x - rect.x - rect.width/2);
circleDistance.y = abs(circle.y - rect.y - rect.height/2);
if (circleDistance.x > (rect.width/2 + circle.r)) { return false; }
if (circleDistance.y > (rect.height/2 + circle.r)) { return false; }
if (circleDistance.x <= (rect.width/2)) { return true; }
if (circleDistance.y <= (rect.height/2)) { return true; }
cornerDistance_sq = (circleDistance.x - rect.width/2)^2 +
(circleDistance.y - rect.height/2)^2;
return (cornerDistance_sq <= (circle.r^2));
}
并更改了它对此:
--Collision detection between circle and rect
local function intersects(circle, rect)
if circle ~= nil then
local circleDistance_x = math.abs(circle.x - rect.x - rect.width/2);
local circleDistance_y = math.abs(circle.y - rect.y - rect.height/2);
if (circleDistance_x > (rect.width/2 + circle.r)) then
return false
end
if (circleDistance_y > (rect.height/2 + circle.r)) then
return false
end
if (circleDistance_x <= (rect.width/2)) then
return true
end
if (circleDistance_y <= (rect.height/2)) then
return true
end
cornerDistance_sq = (circleDistance_x - rect.width/2)^2 +
(circleDistance_y - rect.height/2)^2;
return (cornerDistance_sq <= (circle.r^2));
else
return false
end
end
我确保给圆一个 r
属性并将其设置为 20。我没有收到任何错误。当矩形靠近圆时,它们就会被删除(看起来当矩形到达圆的中心时,函数返回 true,但我可能是错的)。我在转换过程中做错了什么吗?
I needed to get rid of physics in my game so I needed to implement a custom collision detection function (the collision detection function is being called every frame). I want to check if a rectangle has intersected a circle. I took the code from here (the answer by e.James):
bool intersects(CircleType circle, RectType rect)
{
circleDistance.x = abs(circle.x - rect.x - rect.width/2);
circleDistance.y = abs(circle.y - rect.y - rect.height/2);
if (circleDistance.x > (rect.width/2 + circle.r)) { return false; }
if (circleDistance.y > (rect.height/2 + circle.r)) { return false; }
if (circleDistance.x <= (rect.width/2)) { return true; }
if (circleDistance.y <= (rect.height/2)) { return true; }
cornerDistance_sq = (circleDistance.x - rect.width/2)^2 +
(circleDistance.y - rect.height/2)^2;
return (cornerDistance_sq <= (circle.r^2));
}
And changed it to this:
--Collision detection between circle and rect
local function intersects(circle, rect)
if circle ~= nil then
local circleDistance_x = math.abs(circle.x - rect.x - rect.width/2);
local circleDistance_y = math.abs(circle.y - rect.y - rect.height/2);
if (circleDistance_x > (rect.width/2 + circle.r)) then
return false
end
if (circleDistance_y > (rect.height/2 + circle.r)) then
return false
end
if (circleDistance_x <= (rect.width/2)) then
return true
end
if (circleDistance_y <= (rect.height/2)) then
return true
end
cornerDistance_sq = (circleDistance_x - rect.width/2)^2 +
(circleDistance_y - rect.height/2)^2;
return (cornerDistance_sq <= (circle.r^2));
else
return false
end
end
I made sure to give the circle a r
property and made it 20. I am not getting any errors. And the rectangles are removed when they get near the circle (it seems like the function returns true when the rectangle hits the center of the circle but I may be wrong). Am I doing something wrong in my conversion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改这两行:
To:
似乎可以解决问题。随机猜测万岁!
Changing these two lines:
To:
Seems to do the trick. Hurray for random guessing!