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

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

Introduction

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

Prototype

public void setTransform(float x, float y, float angle) 

Source Link

Document

Set the position of the body's origin and rotation.

Usage

From source file:com.dongbat.game.util.PhysicsUtil.java

/**
 * Create box2d Edge, use for making world, floor, ...
 *
 * @param world artemis world/*  w w w.  ja v  a  2 s .  c  o  m*/
 * @param type Body type
 * @param x1 x start
 * @param y1 x finish
 * @param x2 y start
 * @param y2 y finish
 * @param density densisty of edge
 * @return Edge that was just created
 */
public static Body createEdge(com.artemis.World world, BodyDef.BodyType type, float x1, float y1, float x2,
        float y2, float density) {
    BodyDef def = new BodyDef();
    def.type = type;
    Body box = getPhysicsWorld(world).createBody(def);

    EdgeShape poly = new EdgeShape();
    poly.set(new Vector2(0, 0), new Vector2(x2 - x1, y2 - y1));
    //    box.createFixture(poly, density);
    box.setTransform(x1, y1, 0);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = poly;
    //    fixtureDef.filter.maskBits = 2;
    //    fixtureDef.filter.categoryBits = 2 | 1;
    box.createFixture(fixtureDef);
    //    box.setUserData(new Box2dSteeringEntity(box, true, 0.1f));
    poly.dispose();
    return box;
}

From source file:com.me.main.startgame.java

License:Apache License

private Body createEdge(BodyType type, float x1, float y1, float x2, float y2, float density) {
    BodyDef def = new BodyDef();
    def.type = type;/*  www.ja  va2s.c o  m*/
    Body box = world.createBody(def);

    PolygonShape poly = new PolygonShape();
    poly.setAsBox(x2 - x1, y2 - y1);
    box.createFixture(poly, density);
    box.setTransform(x1, y1, 0);
    poly.dispose();

    return box;
}

From source file:fr.plnech.igem.game.model.Wall.java

License:Open Source License

public Wall(float pX, float pY, float pWidth, float pHeight, Type pType,
        VertexBufferObjectManager pVertexBufferObjectManager, PhysicsWorld physicsWorld, boolean masked) {
    super(pX, pY, pWidth, pHeight, pVertexBufferObjectManager);
    setVisible(false);/*ww  w  . ja  va 2  s. c  o  m*/
    type = pType;
    final FixtureDef itemFD;
    if (masked) {
        itemFD = PhysicsFactory.createFixtureDef(PhysicalWorldObject.BODY_DENSITY,
                PhysicalWorldObject.BODY_ELASTICITY, PhysicalWorldObject.BODY_FRICTION, false,
                GutGame.CATEGORY_WALL, GutGame.MASK_WALL, GutGame.GROUP_INDEX);
    } else {
        itemFD = PhysicsFactory.createFixtureDef(PhysicalWorldObject.BODY_DENSITY,
                PhysicalWorldObject.BODY_ELASTICITY, PhysicalWorldObject.BODY_FRICTION);
    }
    Body body = PhysicsFactory.createBoxBody(physicsWorld, this, BodyDef.BodyType.StaticBody, itemFD);
    body.setUserData(type);
    body.setTransform(pX / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT,
            pY / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, 0);
    physicsWorld.registerPhysicsConnector(new PhysicsConnector(this, body, true, false));
}

From source file:org.matheusdev.util.TmxObjectsLoader.java

License:Open Source License

private void createBox2DBody(TmxObject object, Physics physics, float tileWidth, float tileHeight,
        int mapheight, int mapwidth) {
    // From Tiled to Box2D space (32 Pixels = 1 Meter):
    final float objectX = object.x / tileWidth;
    final float objectY = object.y / tileHeight;
    final float objectWidth = object.width / tileWidth;
    final float objectHeight = object.height / tileHeight;
    // From Tiled Rectangles to Box2D Rectangles
    // Difference:
    //  - Tiled Rectangles:
    //     Origin at top-left
    //     Width / Height from top-left to bottom-right
    //  - Box2D Rectangles:
    //     Origin in the middle
    //     Width / Height from the origin to an edge
    //     Upside down y coordinate, relative to Tiled
    final float w = (objectWidth / 2);// / (float) map.tileWidth;
    final float h = (objectHeight / 2);// / (float) map.tileHeight;
    final float x = ((objectX - (objectWidth / 2)) + w * 2);// / (float) map.tileWidth;
    final float y = (mapheight) - (objectY - (objectHeight / 2) + h * 2);// / (float) map.tileHeight;

    switch (object.objectType) {
    case RECTANGLE:
        Body box = physics.createBox(BodyType.StaticBody, w, h, 1);
        box.setTransform(x, y, 0f);
        break;//from  w  w  w.  j av a2 s  .com
    case ELLIPSE:
        Body circle = physics.createCircle(BodyType.StaticBody, (w + h) / 2, 1);
        circle.setTransform(x, y, 0f);
        break;
    case POLYGON:
        createPolygon(object, physics, tileWidth, tileHeight, mapwidth, mapheight);
        break;
    case POLYLINE:
        createEdge(object, physics, tileWidth, tileHeight, mapwidth, mapheight);
        break;
    }
}

From source file:se.danielj.geometridestroyer.EntityCreator.java

License:GNU General Public License

private static Body rectangleEntity(World world, Entity entity, float x, float y, float width, float height,
        float angle) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DynamicBody;
    bodyDef.position.set(x, y);/*from   ww  w. j a  v  a  2 s  .  c om*/
    Body body = world.createBody(bodyDef);
    body.setUserData(entity);
    body.setTransform(x, y, angle);

    PolygonShape shape = new PolygonShape();
    Vector2[] vertices = { new Vector2(-width / 2, height / 2), new Vector2(-width / 2, -height / 2),
            new Vector2(width / 2, -height / 2), new Vector2(width / 2, height / 2) };
    shape.set(vertices);

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 0.5f;
    fixtureDef.friction = 0.4f;
    fixtureDef.restitution = 0;

    body.createFixture(fixtureDef);

    return body;
}

From source file:Utils.CreateCircle.java

public static Body createCircle(World world, int x, int y, float radius) {
    Shape c = new CircleShape();
    c.setRadius(radius);//ww  w  . j a v a 2 s .c om
    BodyDef bd = new BodyDef();
    bd.type = BodyDef.BodyType.DynamicBody;

    Body b = world.createBody(bd);
    b.createFixture(c, 1f);
    b.setTransform(x, y, 0);
    return b;
}