How to get the size of a collision shape?

Alexin

  • *
  • Posts: 3130
I need to retrieve the size of an actor's collision shape.

My first try, based on a Box2D forum post was:

Code: [Select]
private function getShapeSize(target:Actor, size:Rectangle):Rectangle
{
for(var f:b2Fixture = target.getBody().m_fixtureList; f; f = f.m_next)
{
//var shape:b2Shape = target.getBody().m_fixtureList.m_shape;
var shape:b2Shape = f.m_shape;
print("m_type=" + shape.m_type);

if(shape.m_type == b2Shape.e_circle)
{print("circle");
size.width =  (shape as b2CircleShape).m_radius;
size.height =  size.width;
}
else if(shape.m_type == b2Shape.e_polygon)
{
print("poly");
var vertex:V2 = (shape as b2PolygonShape).GetVertex(1);

size.width = vertex.x;
size.height = vertex.y;
}
}

return size;
}

b2Shape.m_type returns e_typeCount.


Has anyone any idea on how to get a b2Shape size?
"Find the fun"
alexin@stencyl.com

Greg

  • *
  • Posts: 1260
You may be able to do something like this -

Code: [Select]
var fixtureList:Array = new Array;

for(var fixture:b2Fixture = actor.getBody().GetFixtureList(); fixture; fixture = fixture.GetNext())
{
     fixtureList.push(fixture);
}

for each(var f:b2Fixture in fixtureList)
{
     var poly:b2Shape = f.GetShape();

     if(poly instanceof b2CircleShape)
     {

     }

     if(poly instanceof b2PolygonShape)
     {
print("width: " + Math.round(GameState.toPixelUnits((f.m_aabb.upperBound.x - f.m_aabb.lowerBound.x))));
print("height: " + Math.round(GameState.toPixelUnits((f.m_aabb.upperBound.y - f.m_aabb.lowerBound.y))));     
     }
}

Alexin

  • *
  • Posts: 3130
Thank you. I didn't think about using instanceof...
"Find the fun"
alexin@stencyl.com