[英]Collision Detection Between Non Axis-Aligned Circle and Rectangle in C++ using SFML
我一直在使用C ++和图形库SFML创建游戏。 我想要一个布尔方法来检查sf :: CircleShape和sf :: RectangleShape是否在给定这两个对象作为参数的情况下发生冲突。 问题在于对象未与轴对齐。 我将如何创建方法?
我对方法的尝试(但仅适用于轴对齐的形状):
bool checkCollision(CircleShape circle, RectangleShape rectangle)
{
double circleDistanceX = abs(circle.getPosition().x - rectangle.getPosition().x);
double circleDistanceY = abs(circle.getPosition().y - rectangle.getPosition().y);
if (circleDistanceX > (rectangle.getSize().x/2 + circle.getRadius()))
{
return false;
}
if (circleDistanceY > (rectangle.getSize().y/2 + circle.getRadius()))
{
return false;
}
if (circleDistanceX <= (rectangle.getSize().x/2))
{
return true;
}
if (circleDistanceY <= (rectangle.getSize().y/2))
{
return true;
}
double cornerDistance_sq = pow((circleDistanceX - rectangle.getSize().x/2), 2) + pow((circleDistanceY - rectangle.getSize().y/2),2);
return (cornerDistance_sq <= pow((circle.getRadius()),2));
}
这本来应该是评论,但看来我的声誉太低了。 如果我是您,我会检查SAT方法,因为它确实很酷,可用于检查任何形状(包括矩形和圆形)之间的分隔。 这是带有有效源代码示例的教程: http : //content.gpwiki.org/VB :Tutorials: Building_A_Physics_Engine : Basic_Intersection_Detection
此外,该网站还提供了一些非常酷的示例,说明了其工作原理:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.