Example usage for com.badlogic.gdx.physics.box2d Body getMassData

List of usage examples for com.badlogic.gdx.physics.box2d Body getMassData

Introduction

In this page you can find the example usage for com.badlogic.gdx.physics.box2d Body getMassData.

Prototype

public MassData getMassData() 

Source Link

Document

Get the mass data of the body.

Usage

From source file:loon.physics.PhysicsBodyBuilder.java

License:Apache License

public Body build(boolean disposeShapes) {
    Body body = world.createBody(bodyDef);

    for (int i = 0; i < fixtureDefs.size(); i++) {
        FixtureDef fixtureDef = fixtureDefs.get(i);
        Fixture fixture = body.createFixture(fixtureDef);
        fixture.setUserData(fixtureUserDatas.get(i));
    }//from   w w w.j  a  va2  s.  c o m

    if (massSet) {
        MassData bodyMassData = body.getMassData();
        massData.center.set(bodyMassData.center);
        body.setMassData(massData);
    }

    body.setUserData(userData);
    body.setTransform(position, angle);

    reset(disposeShapes);
    return body;
}

From source file:org.matheusdev.ror.view.EntityView.java

License:Open Source License

public void draw(SpriteBatch batch, Entity e, Sprite sprite, float width, float xoffset, float yoffset) {
    Body body = e.getBody();
    // Super-Duper important Box2D-Magic code:
    // This should be / is in almost every Box2D project
    // It takes the Body and the associated sprite and
    // renders the sprite properly, using the body's
    // position, rotation and origin.
    final float worldToSprite = sprite.getWidth() / width;
    final float spriteToWorld = width / sprite.getWidth();
    // Get body position:
    final float bodyX = e.getX();
    final float bodyY = e.getY();
    // Get body center:
    final Vector2 center = body.getLocalCenter();
    final Vector2 massCenter = body.getMassData().center;
    center.sub(massCenter).add(xoffset, yoffset);
    // Compute sprite-space center:
    final Vector2 spriteCenter = new Vector2(sprite.getWidth() / 2, sprite.getHeight() / 2)
            .sub((center.cpy().scl(worldToSprite)));
    // Upload to sprite:
    sprite.setScale(1f * spriteToWorld);
    sprite.setRotation(e.getRotation() * MathUtils.radiansToDegrees);
    sprite.setOrigin(spriteCenter.x, spriteCenter.y);
    sprite.setPosition(bodyX - spriteCenter.x, bodyY - spriteCenter.y);
    // Draw Sprite:
    sprite.draw(batch);//from   w  w w.j av a 2 s  . c o m
}