0

I am working with box2dweb and i am trying to make a function, that would add instructions how to draw a 'body' based on the 'body' shape.

That is: When received a 'b2BodyDef' get the shape, and with external information, get the shape specifications. To do this i need to cast 'b2Shape' back to 'b2CircleShape'.

I guess with C++ this would be something like

 b2CircleShape* shape_circle = dynamic_cast< (b2CircleShape*) >( shape );

How do i do similar thing with javascript? I do know there are tons of other ways to do this (like pass the wanted radius on this example as parameter) but i would like to do what i feel like right and not a hack.

function Add_new_drawable_object_to_world( body, type )
{
      GLOBAL_world_objects.push( body );
      var s = new Sprite();

      if ( type == OBJECT_TYPE_PLAYER )
      {
         s.graphics.beginFill ( 0x2222ff, 0.6);

         var b2CircleShape   = Box2D.Collision.Shapes.b2CircleShape;

         var fixture_list = body.GetFixtureList();
         var shape        = fixture_list.GetShape() ;

         // FIXME: TypeError: shape.GetRadius is not a function 
          var radius = shape.GetRadius();

         // here i would draw fancy circle with 'radius'

And earlier i have:

   // Create player
   var player     = new b2FixtureDef();   // ball fixture definition
   player.shape   = new b2CircleShape();
   player.density = 0.5;
   player.shape.SetRadius( 0.2 );

   var bodyDef = new b2BodyDef();
   bodyDef.type = b2Body.b2_dynamicBody;
   bodyDef.position.Set( 0.0, 0.0 );

   var body = GLOBAL_world.CreateBody(bodyDef);
   body.CreateFixture( player );

   Add_new_drawable_object_to_world( body, OBJECT_TYPE_PLAYER );
susundberg
  • 650
  • 7
  • 14
  • 2
    [JavaScript has no classes](http://stackoverflow.com/a/13418980/1048572). It has no strict types, and I doubt that you need to cast objects. What is `shape` actually, can you `console.log` it please? – Bergi Jan 20 '13 at 14:03
  • Thanks @Bergi fast reply, the console.log shows: [16:20:26.637] ({m_p:{x:0, y:0}, m_type:0, m_radius:0.2}) Is there way to call the function GetRadius somehow -- i would rather avoid touching the 'internals' of that class. – susundberg Jan 20 '13 at 14:23

1 Answers1

0

As Bergi says, Javascript has no classes so the concept of private/protected members does not really exist. I would suggest having a look at how box2dweb does this, because the debug draw display does almost the exact same thing you are doing here. Search for b2World.prototype.DrawShape in the box2dweb source.

If it makes you feel better, in the original C++ b2CircleShape the member variables are public, so there is no GetRadius and the 'internals' are accessed directly :)

iforce2d
  • 8,194
  • 3
  • 29
  • 40
  • For others finding this with google, the DrawShape is: ` case b2Shape.e_circleShape: { var circle = ((shape instanceof b2CircleShape ? shape : null)); var center = b2Math.MulX(xf, circle.m_p); var radius = circle.m_radius; var axis = xf.R.col1; this.m_debugDraw.DrawSolidCircle(center, radius, axis, color); } break;` – susundberg Feb 05 '13 at 18:17