Box2D Difference Between WorldCenter and Position
Posted
by
Free Lancer
on Game Development
See other posts from Game Development
or by Free Lancer
Published on 2012-03-26T04:44:40Z
Indexed on
2012/03/26
11:44 UTC
Read the original article
Hit count: 255
So this problem has been brothering for a couple of days now. First off, what is the difference between say Body.getWorldCenter() and Body.getPosition(). I heard that WorldCenter might have to do with the center of gravity or something.
Second, When I create a Box2D Body for a sprite the Body is always at the lower left corner. I check it by printing a Rectangle of 1 pixel around the box.getWorldCenter(). From what I understand the Body should be in the center of the Sprite and its bounding box should wrap around the Sprite, correct?
Here's an image of what I mean (The Sprite is Red, Body Blue):
Here's some code:
Body Creator:
public static Body createBoxBody( final World pPhysicsWorld, final BodyType pBodyType,
final FixtureDef pFixtureDef, Sprite pSprite ) {
float pRotation = 0;
float pCenterX = pSprite.getX() + pSprite.getWidth() / 2;
float pCenterY = pSprite.getY() + pSprite.getHeight() / 2;
float pWidth = pSprite.getWidth();
float pHeight = pSprite.getHeight();
final BodyDef boxBodyDef = new BodyDef();
boxBodyDef.type = pBodyType;
//boxBodyDef.position.x = pCenterX / Constants.PIXEL_METER_RATIO;
//boxBodyDef.position.y = pCenterY / Constants.PIXEL_METER_RATIO;
boxBodyDef.position.x = pSprite.getX() / Constants.PIXEL_METER_RATIO;
boxBodyDef.position.y = pSprite.getY() / Constants.PIXEL_METER_RATIO;
Vector2 v = new Vector2( boxBodyDef.position.x * Constants.PIXEL_METER_RATIO, boxBodyDef.position.y * Constants.PIXEL_METER_RATIO );
Gdx.app.log("@Physics", "createBoxBody():: Box Position: " + v);
// Temporary Box shape of the Body
final PolygonShape boxPoly = new PolygonShape();
final float halfWidth = pWidth * 0.5f / Constants.PIXEL_METER_RATIO;
final float halfHeight = pHeight * 0.5f / Constants.PIXEL_METER_RATIO;
boxPoly.setAsBox( halfWidth, halfHeight ); // set the anchor point to be the center of the sprite
pFixtureDef.shape = boxPoly;
final Body boxBody = pPhysicsWorld.createBody(boxBodyDef);
Gdx.app.log("@Physics", "createBoxBody():: Box Center: " + boxBody.getPosition().mul(Constants.PIXEL_METER_RATIO));
boxBody.createFixture(pFixtureDef);
boxBody.setTransform( boxBody.getWorldCenter(), MathUtils.degreesToRadians * pRotation );
boxPoly.dispose();
return boxBody;
}
Making the Sprite:
public Car( Texture texture, float pX, float pY, World world ) {
super( "Car" );
mSprite = new Sprite( texture );
mSprite.setSize( mSprite.getWidth() / 6, mSprite.getHeight() / 6 );
mSprite.setPosition( pX, pY );
mSprite.setOrigin( mSprite.getWidth()/2, mSprite.getHeight()/2);
FixtureDef carFixtureDef = new FixtureDef();
// Set the Fixture's properties, like friction, using the car's shape
carFixtureDef.restitution = 1f;
carFixtureDef.friction = 1f;
carFixtureDef.density = 1f; // needed to rotate body using applyTorque
mBody = Physics.createBoxBody( world, BodyDef.BodyType.DynamicBody, carFixtureDef, mSprite );
}
© Game Development or respective owner