Getting fixture's AABB position

Topics: Developer Forum, User Forum
Jul 12, 2013 at 5:34 PM
Edited Jul 12, 2013 at 5:35 PM
Hiyas,

I am currently testing out a few ideas/ways to do certain things in my game. I am working on an ingame editor where I can create polygons (static and dynamic) I made a function where a user can select a said polygon. Upon selection it places a bounding box around it. I get the bounding box the following way
Vector2[] selectionBox;
Fixture f = world.TestPoint(CursorWorldPosition);
                        if (f != null)
                        {
                            // we selected something!
                            Body b = f.Body;
                            AABB aabb;
                            f.GetAABB(out aabb, 0);
                            selectionBox = aabb.Vertices.ToArray();
                        }
                        else
                        {
                            selectionBox = null;
                        }
I then draw the selection box the following way (in my draw method after spritebatch begin)
if (selectionBox != null)
            {
                ScreenManager.SpriteBatch.DrawLine(lineTexture, ConvertUnits.ToDisplayUnits(new Vector2(selectionBox[0].X, selectionBox[0].Y)), ConvertUnits.ToDisplayUnits(new Vector2(selectionBox[0 + 1].X, selectionBox[0 + 1].Y)));
                ScreenManager.SpriteBatch.DrawLine(lineTexture, ConvertUnits.ToDisplayUnits(new Vector2(selectionBox[1].X, selectionBox[1].Y)), ConvertUnits.ToDisplayUnits(new Vector2(selectionBox[1 + 1].X, selectionBox[1 + 1].Y)));
                ScreenManager.SpriteBatch.DrawLine(lineTexture, ConvertUnits.ToDisplayUnits(new Vector2(selectionBox[2].X, selectionBox[2].Y)), ConvertUnits.ToDisplayUnits(new Vector2(selectionBox[2 + 1].X, selectionBox[2 + 1].Y)));
                ScreenManager.SpriteBatch.DrawLine(lineTexture, ConvertUnits.ToDisplayUnits(new Vector2(selectionBox[3].X, selectionBox[3].Y)), ConvertUnits.ToDisplayUnits(new Vector2(selectionBox[0].X, selectionBox[0].Y)));
            }
but this is the issue I ran into.
http://youtu.be/ONKYxF_qpl0

Can someone advise me on what I am doing wrong or what am I not understanding relating to the AABBs.

Also, with the World.TestPoint() method it only selects ONE of the polygons in the shape, how can i select all?
Jul 14, 2013 at 5:20 PM
Have you tried using the shape instead?

So replacing

AABB aabb;
                            f.GetAABB(out aabb, 0);
with
AABB aabb;
                Transform transform;
                f.Body.GetTransform(out transform);
                f.Shape.ComputeAABB(out aabb, ref transform, 0);
As for selecting all the fixtures all I can think of is to loop through Body.FixtureList and add all the vertices to a list then scan them for the furthest points in each direction.
Jul 14, 2013 at 5:25 PM
Edited Jul 15, 2013 at 5:24 AM
Thanks mate, that did the trick!

Note, when I execute world.TestPoint() it returns a fixture. If my shape has multiple polygons, how can i get the combined AABB for the entire shape?

See picture below:
Image
Jul 15, 2013 at 3:12 AM
Just loop through b.FixtureList and find the extremes.

I'll probably need something like this soon so hold on.....

...
...

Ok,, here's a function that'll return vector list or null if there's nothing at that point.
        public static Vector2[] GetBoundingBoxForBody(World World, Vector2 Point)
        {

            List<Vector2[]> selectionBoxes = new List<Vector2[]>();

            Vector2[] ret = null;

            Fixture f = World.TestPoint(Point);
            if (f != null)
            {
                
                Body b = f.Body;

                for (int x = 0; x < b.FixtureList.Count; x++)
                {
                    AABB aabb;
                    f = b.FixtureList[x];
                    f.GetAABB(out aabb, 0);

                    if (ret == null)
                        ret = aabb.Vertices.ToArray();

                    selectionBoxes.Add(aabb.Vertices.ToArray());
                }


                for (int x = 0; x < selectionBoxes.Count; x++)
                {

                    Vector2[] v = selectionBoxes[x];

                    for (int y = 0; y < v.Length; y++)
                    {

                        //Top Left
                        if (v[y].X < ret[0].X)
                            ret[0].X = v[y].X;
                        if (v[y].Y < ret[0].Y)
                            ret[0].Y = v[y].Y;

                        //Top Right
                        if (v[y].X > ret[1].X)
                            ret[1].X = v[y].X;
                        if (v[y].Y < ret[1].Y)
                            ret[1].Y = v[y].Y;

                        //Bottom Right
                        if (v[y].X > ret[2].X)
                            ret[2].X = v[y].X;
                        if (v[y].Y > ret[2].Y)
                            ret[2].Y = v[y].Y;

                        //Bottom Left
                        if (v[y].X < ret[3].X)
                            ret[3].X = v[y].X;
                        if (v[y].Y > ret[3].Y)
                            ret[3].Y = v[y].Y;

                    }
                }

            }


            return ret;
        }
Jul 15, 2013 at 4:11 AM
Edited Jul 15, 2013 at 5:24 AM
Worked like a charm, thanks mate!
Image
Jul 16, 2013 at 9:20 AM
Edited Jul 16, 2013 at 6:24 PM
EDIT:
Wrong, post