Get width and height of a fixture

Mar 30, 2011 at 1:15 PM

Hi,

I've run into a very basic problem.. When drawing I need the bounding rectangle of my fixture, and can't seem to find any way to access this information. I could create a subclass of fixture for all my fixtures, and store width/height in that, but I'm guessing there has to be something built in for this already, right?

Thanks in advance,

PV

Mar 30, 2011 at 2:50 PM

Look into finding the AABB for that fixture.  That should give you the vertices that make up that bounding box and from there, you can calculate the width and heights.

Mar 30, 2011 at 3:07 PM

Hm, with that approach it's probably easier to go the subclassing route, instead of calculating it each time.

Basically I have very basic shapes (rect, circle) and want to scale my texture2d accordingly.. Going through the vertices seems more trouble than it's worth for that kind of thing (even though it's just a matter of finding lowest/heighest x/y values).

-PV

Mar 30, 2011 at 3:13 PM
Edited Mar 30, 2011 at 3:13 PM

Actually, you can just do it once when you first create your fixture and store those values.  That should eliminate the need to calculate those everytime you need it.  Do you have a class or data structure you've set up for your game objects?

Mar 30, 2011 at 3:27 PM

Storing them would still require me to create a game object class I guess.. I was just starting to implement one, and you're right about it maybe being nicer if I do use the AABB method while I'm going to implement it (makes it more neat and flexible for the future). I'm not sure how the GetAABB method works exactly btw. I'm seeing two params, one outputs an AABB, and the second wants an index? Is there any documentation for this?

-PV

Mar 30, 2011 at 9:48 PM

Hm, think I got it sorted.. Just curious what the index parameter is for?

Mar 30, 2011 at 10:06 PM
petervel wrote:

Hm, think I got it sorted.. Just curious what the index parameter is for?


Hrm...I'm not too sure, but my guess is it's probably used for bodies that contain more than one fixture? 

Developer
Mar 31, 2011 at 9:13 AM

Don't use Fixture.GetAABB(...). That is used for the dynamic tree and won't give you an exact AABB of the Fixture, as it is just updated sporadically. You get the AABB of the Fixture, which is currently used for broadphase collisions from that.

For an exact AABB use Fixture.Shape.ComputeAABB(...). Shapes can have childs, that is what the index is for. Fixture.Shape.ChildCount tells you how many childs a shape has. The Transform needed to calculate the AABB can be obtained from  Fixture.Body.GetTransform(...).

Mar 31, 2011 at 10:18 AM

Ok, now I'm confused.. So can I just do this and use the Upper and Lower bound of the AABB:

AABB aabb;
Transform transform;

f.Body.GetTransform(out transform);

f.Shape.ComputeAABB(out aabb, ref transform, 0);

Or do I have to loop through it in some way, using the index (and ChildCount), keeping track of the bounds of each AABB and remembering the extremes?

Developer
Mar 31, 2011 at 12:14 PM

If you are just using shapes with a child count of one your code fragment is correct. The only shapes with more than one child are currently loop shapes, in which case your fragment would produce the AABB of the first edge of the loop shape. So as long as you just use circles and polygon shapes you should be fine.