3

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.

Neos
  • 53
  • 5
  • Please remove the tag java-3d as it has nothing to do with the scenegraph API Java3D. – gouessej Jan 03 '15 at 23:02
  • Are you using JME's physics engine or working on an own collision detection algorithm? – 1000ml Jan 04 '15 at 17:56
  • @1000ml I don't use/need physics at all, and also i don't work on my own collision detection algorithm. I just wanted to use the provided collision functionality for no-physics movable objects but as i said in the question, this functionality is not supported now, so i am searching for alternatives. (I'll check GhostControl as sugested by Serj.by) – Neos Jan 04 '15 at 18:27
  • For that you might want to start with the basics of Bullet, the physics engine: http://hub.jmonkeyengine.org/wiki/doku.php/jme3:advanced:physics Also, the solution you're searching for is highly dependant on the 'other spatials'. What shape are they? – 1000ml Jan 04 '15 at 20:48
  • Thanks for the suggestions @100ml. I would not like to use Bullet because for both parties involved in the collision should be created collision shapes then put in the physics engine so i'll probably end up in changing a lot of code. The shapes are pretty simple, i can use boxes for all the different classes of Spatials. I checked the GhostControl but it uses AABB for collision detection. – Neos Jan 04 '15 at 23:01
  • Another considerable option is to implement it yourself. Box-Box collisions can be detected using the Separating Axis Theorem. I don't know if jME3 already has functions for it but the maths and algorithm shouldn't be too hard. It would not depent on Bullet and it would use your Box-shapes that you already have. Maybe you could use the OOB-Code from jME2 as a start: https://code.google.com/p/jmonkeyengine/source/browse/branches/2.0/src/com/jme/bounding/OrientedBoundingBox.java – 1000ml Jan 07 '15 at 12:14

2 Answers2

2

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.

enter image description here

Krythic
  • 4,184
  • 5
  • 26
  • 67
  • I think you should study up more on this concept. You forgot to advise that the object that your box needs to test against would also have to be rotated for collision testing. It is much simpler and faster to learn how work with and test against OBBs. Also, it appears that you are storing 3D orientations using 3 angles. In your above image, you are only rotating 2 of the 3 axis. If you need to rotate all 3, your angle representation will require unnecessary complexity. https://stevehazen.wordpress.com/2010/02/15/matrix-basics-how-to-step-away-from-storing-an-orientation-as-3-angles/ – Steve H Aug 12 '20 at 11:50
  • @SteveH Once you watch the video I linked to you and realize you're wrong, i respectfully ask you to remove this comment, and if you're the one who downvoted me, please undo that as well. Thank you for your understanding in this matter. – Krythic Aug 12 '20 at 17:06
  • your `CreateOrientedBoundingBox(Vector3 min, Vector3 max, Matrix transform)` method will always, only, return a axis aligned box that happens to contain all your rotated points... You need to run a reflector on the code behind `BoundingBox.FromPoints()` , you will find that it can only generate an axis aligned min and max. All its built in tests run against those returned axis aligned min & max values which contain some volume of space outside your rotated corners. – Steve H Aug 12 '20 at 17:59
  • if you would like to continue our conversation, we probably should move away from this comment area. My email address is in my profile. Send me an email if you would like. – Steve H Aug 12 '20 at 19:07
  • "always, only, return a axis aligned box that happens to contain all your rotated points" There is no point in repeating this nonsense, because my video entirely proves you wrong. Clearly you're too stubborn to admit when you're wrong, and for that I pity you. A wise man knows when to humble himself. Consider this my last interaction with you. If you decide to change your mind and delete your comments, just know there are no hard feelings; I forgive you. Crap happens. Have a good one! – Krythic Aug 12 '20 at 20:20
-1

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.

Serj.by
  • 534
  • 5
  • 17
  • Hi, thanks for the quick reply. I am trying to implement collision detection, and as stated here: http://hub.jmonkeyengine.org/wiki/doku.php/jme3:advanced:collision_and_intersection you can only collide geometry vs bounding volumes or rays. I know that BoundingBox is axis aligned(AABB) and that is the reason why i need an Oriented BB. If you know how this can be achieved please share. – Neos Jan 03 '15 at 17:03
  • To check collision you may use GhostControl (check more about it here - http://hub.jmonkeyengine.org/wiki/doku.php/jme3:advanced:physics_listeners ) - it has almost no limitations on shape using for collision detection. It could be even more complex mesh than box. Please let me know if it was helpful. – Serj.by Jan 04 '15 at 16:20
  • I checked the GhostControl but it uses AABB for collision detection. Another thing is that i'll have to create collision shape for every spatial and than put it in the physics. Also note that all the spatials are movable which means that Orienting bounding volumes should be used. Thanks. – Neos Jan 04 '15 at 23:07
  • 1
    As per http://hub.jmonkeyengine.org/wiki/doku.php/jme3:advanced:bullet_pitfalls in the case you need to avoid AABB test for your collisions you should try either PhysicsSpace.sweepTest() or use kinematic objects - what is most suitable for your case. – Serj.by Jan 06 '15 at 08:15
  • You can use an AABB as an OBB; the math works out the exact same. Where did you hear that you should not do this? – Krythic Jul 26 '20 at 00:35