Stu*_*123 0 c++ math collision
我一直在使用以下方法测试两个圆圈之间的碰撞:
Circle A = (x1,y1) Circle b = (x2,y2)
Radius A Radius b
x1 - x2 = x' * x'
y1 - y2 = y' * y'
x' + y' = distance
square root of distance - Radius A + Radius B
Run Code Online (Sandbox Code Playgroud)
如果得到的答案是负数,则它是相交的.
我在测试中使用了这种方法,但它似乎并不是非常准确.
bool circle::intersects(circle & test)
{
Vector temp;
temp.setX(centre.getX() - test.centre.getX());
temp.setY(centre.getY() - test.centre.getY());
float distance;
float temp2;
float xt;
xt = temp.getX();
temp2 = xt * xt;
temp.setX(temp2);
xt = temp.getY();
temp2 = xt * xt;
temp.setY(temp2);
xt = temp.getX() + temp.getY();
distance = sqrt(xt);
xt = radius + test.radius;
if( distance - xt < test.radius)
{
return true;
}
else return false;
}
Run Code Online (Sandbox Code Playgroud)
这是使用这种方法的功能,也许我在这里错了.我只是想知道我可以使用哪些其他方法.我知道分离轴定理更好,但我不知道从哪里开始.

if( distance - xt < test.radius)
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
distance - xt将评估为蓝线,两个磁盘之间的距离.它还满足小于测试半径的条件,但没有发生碰撞.
解决方案:

if(distance <= (radius + test.radius) )
return true;
Run Code Online (Sandbox Code Playgroud)
distance距离中心的距离在哪里.