我正在使用box2dweb并且我正在尝试制作一个函数,该函数将添加说明如何根据“身体”形状绘制“身体”。
即:当收到一个'b2BodyDef'时得到形状,并结合外部信息,得到形状规格。为此,我需要将“b2Shape”转换回“b2CircleShape”。
我想用 C++ 这会像
b2CircleShape* shape_circle = dynamic_cast< (b2CircleShape*) >( shape );
我如何用javascript做类似的事情?我知道还有很多其他方法可以做到这一点(比如在这个例子中传递想要的半径作为参数),但我想做我觉得正确的事情,而不是 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'
早些时候我有:
// 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 );