ner*_*and 3 java android collision-detection game-physics libgdx
在我的 libgdx 游戏中,我有用于地图和玩家对象的 3D BoundingBox 和球体。我想计算它们是否相互碰撞,以便正确模拟这些物体的运动。我可以用什么方法来计算这些物体是否碰撞/相交?
您可以使用以下方法:
public static boolean intersectsWith(BoundingBox boundingBox, Sphere sphere) {
float dmin = 0;
Vector3 center = sphere.center;
Vector3 bmin = boundingBox.getMin();
Vector3 bmax = boundingBox.getMax();
if (center.x < bmin.x) {
dmin += Math.pow(center.x - bmin.x, 2);
} else if (center.x > bmax.x) {
dmin += Math.pow(center.x - bmax.x, 2);
}
if (center.y < bmin.y) {
dmin += Math.pow(center.y - bmin.y, 2);
} else if (center.y > bmax.y) {
dmin += Math.pow(center.y - bmax.y, 2);
}
if (center.z < bmin.z) {
dmin += Math.pow(center.z - bmin.z, 2);
} else if (center.z > bmax.z) {
dmin += Math.pow(center.z - bmax.z, 2);
}
return dmin <= Math.pow(sphere.radius, 2);
}
Run Code Online (Sandbox Code Playgroud)
它是仿照
箱体-球体相交测试的简单方法,作者:Jim Arvo,来自“Graphics Gems”,学术出版社,1990 年
可以在此处找到示例 C 代码: http: //www.realtimerendering.com/resources/GraphicsGems/gems/BoxSphere.c