问题
I am using jmonkeyengine 3 and i have been struggling for days to implement a collision detection for a moving plane/box with other spatials. Finally i read in the collision_and_intersection tutorial (jme hub) that the BoundingBox does’t rotate and also that Oriented bounding box is not supported yet.
I searched the jme forum but i found very old posts for OBB class that doesn’t exists in JME3.
How can i solve this problem, what are my options?
Thanks in advance, any help is much appreciated.
回答1:
A lot of people don't know, apparently, that you can use an Axis Aligned Bounding Box as an Oriented Bounding Box. The math works out the exact same. The only difference is that you need to transform the corners first using the rotation and translation matrix. Here is some example code:
public static BoundingBox CreateOrientedBoundingBox(Vector3 min, Vector3 max, Matrix transform)
{
Vector3[] corners = new Vector3[]
{
Vector3.TransformCoordinate(new Vector3(min.X, max.Y, max.Z), transform),
Vector3.TransformCoordinate(new Vector3(max.X, max.Y, max.Z), transform),
Vector3.TransformCoordinate(new Vector3(max.X, min.Y, max.Z), transform),
Vector3.TransformCoordinate(new Vector3(min.X, min.Y, max.Z), transform),
Vector3.TransformCoordinate(new Vector3(min.X, max.Y, min.Z), transform),
Vector3.TransformCoordinate(new Vector3(max.X, max.Y, min.Z), transform),
Vector3.TransformCoordinate(new Vector3(max.X, min.Y, min.Z), transform),
Vector3.TransformCoordinate(new Vector3(min.X, min.Y, min.Z), transform)
};
return BoundingBox.FromPoints(corners);
}
Of which a transformation matrix fed into it would be defined as:
Matrix transform = VoidwalkerMath.CreateRotationMatrix(this.Rotation) * Matrix.Translation(this.Location);
Also, for the sake of clarity and completion, I create a rotation matrix as such:
/// <summary>
/// Converts degrees to radians.
/// </summary>
/// <param name="degrees">The angle in degrees.</param>
public static float ToRadians(float degrees)
{
return degrees / 360.0f * TwoPi;
}
/// <summary>
/// Creates a rotation matrix using degrees.
/// </summary>
/// <param name="xDegrees"></param>
/// <param name="yDegrees"></param>
/// <param name="zDegrees"></param>
/// <returns></returns>
public static Matrix CreateRotationMatrix(float xDegrees, float yDegrees, float zDegrees)
{
return
Matrix.RotationX(ToRadians(xDegrees)) *
Matrix.RotationY(ToRadians(yDegrees)) *
Matrix.RotationZ(ToRadians(zDegrees));
}
Then you use it with the same collision tests as a traditional AABB. I currently have this working in my game for Frustum culling. So to alleviate/bust this myth: Yes. An AABB can be used as an OBB; the only difference is how the points are transformed.
Update: Here is a visual example of this. Two Crates. The left one is rotated, the right one is not. Both are AABBS.
回答2:
Why don't you use general Box mesh? BoundingBox is axis oriented so it is really could not be rotated. You could check it in the class documentation here: http://hub.jmonkeyengine.org/javadoc/com/jme3/bounding/BoundingBox.html
BoundingBox defines an axis-aligned cube that defines a container for a group of vertices of a particular piece of geometry.
You should use Box mesh to make it working. If there some reason to use especially BoundingBox - please let us know - probably there will be some different solution.
来源:https://stackoverflow.com/questions/27756405/oriented-bounding-box