简体   繁体   中英

Andengine Box2D physics body is not rotating when increase weight

Andengine Box2D physics body is not rotating when increase weight otherwise its perfectly rotating.I am making a game in which the player can throw the bomb, when the player throws the bomb with bombsBody's natural weight its rotating perfectly but when i increase the weight of bomb the bomb does not rotate.im stuck here..plz help. Thank you.

if (bomb)           
{
    mScene.detachChild(target);
    target = null;      
    bombFire = new Sprite(mBall2.getX()+mBall2.getWidth()/2,mBall2.getY(),bombFireRegion);
    mScene.attachChild(bombFire);
    bombBody = PhysicsFactory.createCircleBody(mPhysicsWorld, bombFire,BodyType.DynamicBody, bombFixDef);
    bombBody.setMassData(bombMass);
    mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(bombFire, bombBody, true, true));               
    Vector2 v = new Vector2((xpt-mBall2.getX()),(ypt-mBall2.getY()));
    bombBody.applyLinearImpulse(v,bombBody.getWorldCenter());
}

Changing the mass of an existing body is best done by scaling the existing massData of that body. This keeps the center of mass in the right place and also ensures that the mass and rotational inertia match correctly.

b2MassData massData;
body->GetMassData(&massData);

float scaleFactor = desiredMass / massData.mass;
massData.mass *= scaleFactor;
massData.I *= scaleFactor;

body->SetMassData(&massData);

One thing to be aware of is that this does not affect the density of the fixtures on the body. If you add or remove any fixtures from the body after this, the mass data will be recalculated from the fixtures, not from your mass data. So you would have to do this again after changing fixtures to restore your desired mass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM