2012-02-15 34 views
1

我尝试如何改变质心?

MassData md = mBody.getMassData(); 
md.center.set(2f, 0); mBody.setMassData(md); 

但这不能正常工作。帮助我正确地做到这一点。 我想为我的身体下半部分增加一点重量。

+3

请定义'不工作'。有什么改变吗? – iforce2d 2012-02-15 12:48:20

+0

你找到答案了吗? – sm4 2017-03-20 02:09:40

回答

0

而不是试图直接改变质量,你想要做的是添加一个额外的夹具到你的身体的底部。你可以做到,例如像这样(这是一个片段,不是完整的代码;创建你的纹理,场景等)。

Sprite sprite = new Sprite(CAMERA_WIDTH/2, CAMERA_HEIGHT/2, 32, 32, mDTextureRegion, vbom); 

Body body = PhysicsFactory.createCircleBody(mPhysicsWorld, sprite, BodyType.DynamicBody, PhysicsFactory.createFixtureDef(0.5f, 0.5f, 0.5f)); // create a simple circle body from the sprite 
scene.attachChild(sprite); 
Debug.i("ORIG MASS Y: " + body.getMassData().center.y); // this is just generated info 
FixtureDef fd = PhysicsFactory.createFixtureDef(10f, 1f, 1f, true); // high density fixture; it is a sensor, i.e. it won't affect collisions 
CircleShape cs = new CircleShape(); 
cs.setRadius(0.1f); // make it very small 
fd.shape = cs; 
cs.setPosition(new Vector2(0, -0.5f)); // attach below the current centre 

body.createFixture(fd); // attach to body 
Debug.i("NEW MASS Y: " + mDBody.getMassData().center.y); // the generated info changed 

mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(sprite, body, true, true)); 

这个例子的身体就会像一个spintop,它会尝试祭出所以精灵的底部会接触地面。当你旋转它时,它会尝试再次到达那个位置。