gpt4 book ai didi

java - libgdx 中 BoundingBox 和 Sphere 之间的碰撞检测

转载 作者:太空狗 更新时间:2023-10-29 16:42:38 24 4
gpt4 key购买 nike

在我的 libgdx 游戏中,我有 3D BoundingBoxes 和 Spheres 用于 map 和玩家对象。我想计算它们是否相互碰撞,以便正确模拟这些物体的运动。我可以用什么方法来计算这些物体是否碰撞/相交?

最佳答案

您可以使用以下方法:

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);
}

仿照

A Simple Method for Box-Sphere Intersection Testing by Jim Arvo from "Graphics Gems", Academic Press, 1990

可在此处找到其示例 C 代码:http://www.realtimerendering.com/resources/GraphicsGems/gems/BoxSphere.c

关于java - libgdx 中 BoundingBox 和 Sphere 之间的碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15247347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com