Example usage for com.badlogic.gdx.physics.box2d PolygonShape PolygonShape

List of usage examples for com.badlogic.gdx.physics.box2d PolygonShape PolygonShape

Introduction

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

Prototype

public PolygonShape() 

Source Link

Document

Constructs a new polygon

Usage

From source file:Tabox2D.java

License:Open Source License

/**
 * Creates a new Box body//  w  w  w  . ja  v a  2  s .com
 * @param type "dynamic" or "static"
 * @param x Left-bottom corner X of the box
 * @param y Left-bottom corner Y of the box
 * @param w Width of the box
 * @param h Height of the box
 * @return A new Tabody instance
 */
public Tabody newBox(String type, float x, float y, float w, float h) {
    // Scale proportions:
    x = x / meterSize;
    y = y / meterSize;
    w = w / meterSize;
    h = h / meterSize;

    PolygonShape polygonShape;
    BodyDef defBox = new BodyDef();

    setType(defBox, type);

    defBox.position.set(x + w / 2, y + h / 2);

    Tabody box = new Tabody();
    box.body = world.createBody(defBox);
    polygonShape = new PolygonShape();
    polygonShape.setAsBox(w / 2, h / 2);

    FixtureDef fixtureBox = new FixtureDef();
    fixtureBox.shape = polygonShape;
    fixtureBox.density = 1;
    fixtureBox.friction = 1;
    fixtureBox.restitution = 0;

    ////////////////////////////////////////
    box.w = w * meterSize;
    box.h = h * meterSize;
    box.fixture = fixtureBox;
    box.bodyType = "box";
    ////////////////////////////////////////

    box.body.createFixture(fixtureBox);
    polygonShape.dispose();
    tabodies.add(box);
    return box;
}

From source file:Tabox2D.java

License:Open Source License

private Tabody generateRegularPoly(String name, String type, float x, float y, float rad) {
    // Scale proportions:
    x /= meterSize;//from  w ww . j ava 2 s  . c o  m
    y /= meterSize;
    rad /= meterSize;

    PolygonShape polygonShape;
    BodyDef defPoly = new BodyDef();

    setType(defPoly, type);

    // Generate points:
    List<Vector2> pts = new ArrayList<Vector2>();
    Vector2 p0 = new Vector2(0, rad);

    float conv = MathUtils.degreesToRadians;
    float angleInDeg = polyInfo.get(name + "_angle");
    float cos = MathUtils.cos(conv * angleInDeg);
    float sin = MathUtils.sin(conv * angleInDeg);

    for (int i = 0; i < polyInfo.get(name); i++) {
        pts.add(new Vector2(p0.x, p0.y));
        p0.set(p0.x, p0.y);

        float newX = p0.x * cos - p0.y * sin;
        float newY = p0.x * sin + p0.y * cos;

        p0.x = newX;
        p0.y = newY;
    }

    // Get bounding box:

    float[] rawPoints = new float[pts.size() * 2];
    int pointIndex = 0;
    for (int i = 0; i < rawPoints.length - 1; i += 2) {
        rawPoints[i] = pts.get(pointIndex).x;
        rawPoints[i + 1] = pts.get(pointIndex).y;
        pointIndex++;
    }

    Polygon polyForBox = new Polygon();
    polyForBox.setVertices(rawPoints);

    Rectangle boundingRect = polyForBox.getBoundingRectangle();
    float boxX = boundingRect.x;
    float boxY = boundingRect.y;
    float boxW = boundingRect.getWidth();
    float boxH = boundingRect.getHeight();

    Vector2 aabbCenter = new Vector2(boxX + boxW / 2, boxY + boxH / 2);
    defPoly.position.set(x, y);

    Tabody regularPoly = new Tabody();
    regularPoly.body = world.createBody(defPoly);
    //regularPoly.body.setFixedRotation(true);
    polygonShape = new PolygonShape();

    //polygonShape.setAsBox(w / 2, h / 2);
    for (int i = 0; i < rawPoints.length - 1; i += 2) {
        rawPoints[i] -= aabbCenter.x;
        rawPoints[i + 1] -= aabbCenter.y;
    }
    //rawPoints[0] += 0.5;
    polygonShape.set(rawPoints);

    FixtureDef fixtureBox = new FixtureDef();
    fixtureBox.shape = polygonShape;
    fixtureBox.density = 1;
    fixtureBox.friction = 1;
    fixtureBox.restitution = 0;

    ////////////////////////////////////////
    regularPoly.w = boxW * meterSize;//radius * 2 * meterSize;
    regularPoly.h = boxH * meterSize;//radius * 2 * meterSize;
    regularPoly.fixture = fixtureBox;
    regularPoly.bodyType = "poly";
    ////////////////////////////////////////

    regularPoly.body.createFixture(fixtureBox);
    polygonShape.dispose();
    tabodies.add(regularPoly);
    return regularPoly;
}

From source file:Tabox2D.java

License:Open Source License

/**
 * Creates a Polygons with the given points
 * @param type "dynamic" or "static"//  www .ja  v  a2s  . c o  m
 * @param pts points for the polygon
 * @return A new Tabody instance
 */
public Tabody newPoly(String type, float[] pts) {
    // Scale proportions:
    for (int i = 0; i < pts.length; i++) {
        pts[i] /= meterSize;
    }

    PolygonShape polygonShape;
    BodyDef defPoly = new BodyDef();

    setType(defPoly, type);

    // Get bounding box:

    Polygon polyForBox = new Polygon();
    polyForBox.setVertices(pts);

    //polyForBox.translate(center.x, center.y);

    Rectangle boundingRect = boundingBoxOf(polyForBox.getVertices());
    Vector2 aabbCenter = new Vector2(boundingRect.x + boundingRect.width / 2,
            boundingRect.y + boundingRect.height / 2);
    defPoly.position.set(aabbCenter.x, aabbCenter.y);

    Tabody regularPoly = new Tabody();
    regularPoly.body = world.createBody(defPoly);
    polygonShape = new PolygonShape();

    for (int i = 0; i < pts.length - 1; i += 2) {
        pts[i] -= aabbCenter.x;
        pts[i + 1] -= aabbCenter.y;
    }
    polygonShape.set(pts);

    FixtureDef fixtureBox = new FixtureDef();
    fixtureBox.shape = polygonShape;
    fixtureBox.density = 1;
    fixtureBox.friction = 1;
    fixtureBox.restitution = 0;

    ////////////////////////////////////////
    regularPoly.w = boundingRect.width * meterSize;//radius * 2 * meterSize;
    regularPoly.h = boundingRect.height * meterSize;//radius * 2 * meterSize;
    regularPoly.fixture = fixtureBox;
    regularPoly.bodyType = "poly";
    ////////////////////////////////////////

    regularPoly.body.createFixture(fixtureBox);
    polygonShape.dispose();
    tabodies.add(regularPoly);
    return regularPoly;
}

From source file:ch.coldpixel.mario.Sprites.InteractiveTileObject.java

public InteractiveTileObject(World world, TiledMap map, Rectangle bounds) {
    this.world = world;
    this.map = map;
    this.bounds = bounds;

    BodyDef bdef = new BodyDef();
    FixtureDef fdef = new FixtureDef();
    PolygonShape shape = new PolygonShape();

    bdef.type = BodyDef.BodyType.StaticBody;
    bdef.position.set((bounds.getX() + bounds.getWidth() / 2) / MarioBros.PPM,
            (bounds.getY() + bounds.getHeight() / 2) / MarioBros.PPM);

    body = world.createBody(bdef);//  w  w w  .j a va  2 s . c o m

    shape.setAsBox(bounds.getWidth() / 2 / MarioBros.PPM, bounds.getHeight() / 2 / MarioBros.PPM);
    fdef.shape = shape;
    body.createFixture(fdef);

}

From source file:ch.coldpixel.mario.Tools.B2WorldCreator.java

public B2WorldCreator(World world, TiledMap map) {
    //Create body and fixture variables
    BodyDef bdef = new BodyDef();
    PolygonShape shape = new PolygonShape();
    FixtureDef fdef = new FixtureDef();
    Body body;/*ww w.  j  a  va 2  s .c o  m*/

    //create ground bodies/fixture
    for (MapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)) { //get(2) = in TILED from below the 2nd object(starts with 0, so it gets ground)
        Rectangle rect = ((RectangleMapObject) object).getRectangle(); //Get Rectangle Object itself

        bdef.type = BodyDef.BodyType.StaticBody;//3 different bodys, static=selfexplaining, dynamic=typical player, kinematic=not effected by forces like gravity, but can manipulated with velocity, ex: moving plattforms
        bdef.position.set((rect.getX() + rect.getWidth() / 2) / MarioBros.PPM,
                (rect.getY() + rect.getHeight() / 2) / MarioBros.PPM);

        body = world.createBody(bdef);//add body to our world

        shape.setAsBox(rect.getWidth() / 2 / MarioBros.PPM, rect.getHeight() / 2 / MarioBros.PPM);
        fdef.shape = shape;
        body.createFixture(fdef);
    }

    //create pipe bodies/fixture
    for (MapObject object : map.getLayers().get(3).getObjects().getByType(RectangleMapObject.class)) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();

        bdef.type = BodyDef.BodyType.StaticBody;
        bdef.position.set((rect.getX() + rect.getWidth() / 2) / MarioBros.PPM,
                (rect.getY() + rect.getHeight() / 2) / MarioBros.PPM);

        body = world.createBody(bdef);

        shape.setAsBox(rect.getWidth() / 2 / MarioBros.PPM, rect.getHeight() / 2 / MarioBros.PPM);
        fdef.shape = shape;
        body.createFixture(fdef);
    }

    //create brick bodies/fixture
    for (MapObject object : map.getLayers().get(5).getObjects().getByType(RectangleMapObject.class)) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();

        new Brick(world, map, rect);

    }

    //create coin bodies/fixtures
    for (MapObject object : map.getLayers().get(4).getObjects().getByType(RectangleMapObject.class)) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();

        new Coin(world, map, rect);

    }

}

From source file:com.agateau.pixelwheels.bonus.BonusSpot.java

License:Open Source License

public BonusSpot(Assets assets, AudioManager audioManager, GameWorld gameWorld, float x, float y) {
    final float U = Constants.UNIT_FOR_PIXEL;
    mAudioManager = audioManager;//from  w ww. j  av  a  2s.co  m
    mX = x;
    mY = y;

    mRegion = assets.gift;
    mSound = assets.soundAtlas.get("bonus");

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(U * mRegion.getRegionWidth() / 2, U * mRegion.getRegionHeight() / 2);

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(mX, mY);

    mBody = gameWorld.getBox2DWorld().createBody(bodyDef);
    Fixture fixture = mBody.createFixture(shape, 1f);
    fixture.setSensor(true);
    mBody.setUserData(this);

    mBody.setAngularVelocity(240 * MathUtils.degreesToRadians);

    shape.dispose();
}

From source file:com.agateau.pixelwheels.bonus.Bullet.java

License:Open Source License

private void firstInit(Assets assets) {
    mAssets = assets;/*from w  w  w.  j  av  a2s  .c o  m*/
    mBodyDef = new BodyDef();
    mBodyDef.type = BodyDef.BodyType.DynamicBody;
    mBodyDef.bullet = true;

    mShape = new PolygonShape();
    mShape.setAsBox(assets.bullet.getRegionWidth() * Constants.UNIT_FOR_PIXEL / 2,
            assets.bullet.getRegionHeight() * Constants.UNIT_FOR_PIXEL / 2);
}

From source file:com.agateau.pixelwheels.racer.Wheel.java

License:Open Source License

public Wheel(GameWorld gameWorld, Vehicle vehicle, TextureRegion region, float posX, float posY, float angle) {
    mGameWorld = gameWorld;/*from   ww w  .j  av a 2  s.c o  m*/
    mVehicle = vehicle;
    mRegion = region;

    float w = Constants.UNIT_FOR_PIXEL * region.getRegionWidth();
    float h = Constants.UNIT_FOR_PIXEL * region.getRegionHeight();

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(posX, posY);
    bodyDef.angle = angle * MathUtils.degreesToRadians;
    mBody = mGameWorld.getBox2DWorld().createBody(bodyDef);

    PolygonShape shape = new PolygonShape();
    shape.set(Box2DUtils.createOctogon(w, h, w / 4, w / 4));
    mBody.createFixture(shape, 2f);
    shape.dispose();
}

From source file:com.agateau.pixelwheels.utils.Box2DUtils.java

License:Open Source License

@SuppressWarnings("unused")
public static Body createStaticBox(World world, float x, float y, float width, float height) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.StaticBody;
    bodyDef.position.set(x + width / 2, y + height / 2);
    Body body = world.createBody(bodyDef);

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(width / 2, height / 2);

    body.createFixture(shape, 1);/*from   w  w w .j  a  v  a  2 s. c  om*/
    return body;
}

From source file:com.agateau.pixelwheels.utils.Box2DUtils.java

License:Open Source License

public static Body createStaticBodyForMapObject(World world, MapObject object) {
    final float u = Constants.UNIT_FOR_PIXEL;
    float rotation = object.getProperties().get("rotation", 0f, Float.class);

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.StaticBody;
    bodyDef.angle = -rotation * MathUtils.degreesToRadians;

    if (object instanceof RectangleMapObject) {
        Rectangle rect = ((RectangleMapObject) object).getRectangle();

        /*/*  w  w  w  .j  a v  a2 s.c o  m*/
          A          D
           x--------x
           |        |
           x--------x
          B          C
         */
        float[] vertices = new float[8];
        // A
        vertices[0] = 0;
        vertices[1] = 0;
        // B
        vertices[2] = 0;
        vertices[3] = -rect.getHeight();
        // C
        vertices[4] = rect.getWidth();
        vertices[5] = -rect.getHeight();
        // D
        vertices[6] = rect.getWidth();
        vertices[7] = 0;
        scaleVertices(vertices, u);

        bodyDef.position.set(u * rect.getX(), u * (rect.getY() + rect.getHeight()));
        Body body = world.createBody(bodyDef);

        PolygonShape shape = new PolygonShape();
        shape.set(vertices);

        body.createFixture(shape, 1);
        return body;
    } else if (object instanceof PolygonMapObject) {
        Polygon polygon = ((PolygonMapObject) object).getPolygon();
        float[] vertices = polygon.getVertices().clone();
        scaleVertices(vertices, u);

        bodyDef.position.set(polygon.getX() * u, polygon.getY() * u);
        Body body = world.createBody(bodyDef);

        PolygonShape shape = new PolygonShape();
        shape.set(vertices);

        body.createFixture(shape, 1);
        return body;
    } else if (object instanceof EllipseMapObject) {
        Ellipse ellipse = ((EllipseMapObject) object).getEllipse();
        float radius = ellipse.width * u / 2;
        float x = ellipse.x * u + radius;
        float y = ellipse.y * u + radius;

        bodyDef.position.set(x, y);
        Body body = world.createBody(bodyDef);

        CircleShape shape = new CircleShape();
        shape.setRadius(radius);

        body.createFixture(shape, 1);
        return body;
    }
    throw new RuntimeException("Unsupported MapObject type: " + object);
}