0

here is my code for displaying bounding box

 Vector3[] corners = box.GetCorners();
        for (int i = 0; i < 8; i++)
        {
            verts[i].Position = Vector3.Transform(corners[i],modelMatrix);
            verts[i].Color = Color.White;
        }

        vbo.SetData(verts);
        ibo.SetData(indices);

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            effect.World = Matrix.Identity; 
            effect.View = view;
            effect.Projection = projection;

            pass.Apply();
            ContentLoader.SetBuffers(ibo, vbo);
        }

I'd like to achieve same result using BoundingBox class. I tried to do it like this,but it doesn't work

        for (int i = 0; i < boundingBoxes.Count; i++)
        {
            Vector3 min = Vector3.Transform(boundingBoxes[i].Min, modelMatrix);
            Vector3 max = Vector3.Transform(boundingBoxes[i].Max, modelMatrix);
            boundingBoxes[i] = new BoundingBox(min, max);
        }

the code above works if there is no rotation.With rotation things get messed up.Any idea why and how to fix it?

user1075940
  • 1,086
  • 2
  • 22
  • 46
  • 3
    [`BoundingBox`](http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.boundingbox.aspx) is axis-aligned. It cannot be rotated - it's edges are always parallel to the main axes (x, y and z). – neeKo Dec 08 '12 at 11:56
  • How do you draw the boundingbox? if you use DrawIndexedPrimitives or siminlar, maybe you should pass your Matrix rotatin as world transform... – Blau Dec 08 '12 at 14:06

1 Answers1

-1

You can not rotate a BoundingBox object in Xna. The built in collision detection methods of the BoundingBox class will always be calculated from min & max for a box in axis alignment only. By transforming min & max, you are not rotating the box, you are only changing the x,y,z dimensions of the axis aligned box.

You might be better off studying up on "oriented bounding boxes". You would draw an oriented box by using the corners as verts and choosing 'LineList' as your PrimitiveType instead of 'TriangleList' in the 'DrawIndexedPrimitives' method. Collision detection for an oriented box is different & more complex than for an axis aligned box.

Steve H
  • 5,479
  • 4
  • 20
  • 26
  • This is false, as I proved in this answer: https://stackoverflow.com/a/63094985/3214889 It's pretty incredible how many people think there is a difference between an AABB and an OBB. Mathematically they are the exact same. – Krythic Aug 05 '20 at 01:18
  • Actually, the answer is not false. The answer states that rotating the min and max will not solve the problem. this is true. Yes, your also correct that you could conceivably rotate all corners of an OBB to align with the cardinal axis and then test is as an AABB. Why on earth would you waste all those CPU cycles doing that? If a box is oriented, just use OBB tests on it... the Dot & Cross product methodology is so much more CPU efficient than all those required rotations (which use trig functions in the background and CPU intensive square rooting to normalize the transforms). – Steve H Aug 11 '20 at 21:16
  • @Krythic Also, all detection methods that use the AABB min/max values will fail when you rotate either the corners or the min/max. The min/max values will always be referenced to Axis Alignment even if they or the corners are rotated. The min/max are used with the built in collision detection methods. In short, your idea of rotating an AABB to make it a OBB will result in inaccurate collision detection. Also, even if you wrote your own detection methods that account for that, don't forget you will have to rotate the object you are testing against to get it into the same reference as the BB. – Steve H Aug 12 '20 at 11:19
  • Look at this video, i show the bounding boxes at one point in it. I have fully confirmed that traditional aabb collision tests work with this. In that video I'm frustum culling rotated geometry, including the crate. It detects all of it perfectly; even if i fly up to a corner of it. Ray cast collisions also work perfectly. You're wrong. I politely ask that you delete your comments. https://youtu.be/WsvsRpjkaF8 – Krythic Aug 12 '20 at 16:47