Example usage for com.badlogic.gdx.physics.box2d Transform mul

List of usage examples for com.badlogic.gdx.physics.box2d Transform mul

Introduction

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

Prototype

public Vector2 mul(Vector2 v) 

Source Link

Document

Transforms the given vector by this transform

Usage

From source file:com.altportalgames.colorrain.utils.Box2DDebugRenderer.java

License:Apache License

private void drawShape(Fixture fixture, Transform transform, Color color) {
    if (fixture.getType() == Type.Circle) {
        CircleShape circle = (CircleShape) fixture.getShape();
        t.set(circle.getPosition());/* w ww. jav  a2s .c  o  m*/
        transform.mul(t);
        drawSolidCircle(t, circle.getRadius(),
                axis.set(transform.vals[Transform.COL1_X], transform.vals[Transform.COL1_Y]), color);
    } else {
        PolygonShape poly = (PolygonShape) fixture.getShape();
        int vertexCount = poly.getVertexCount();
        for (int i = 0; i < vertexCount; i++) {
            poly.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
        }
        drawSolidPolygon(vertices, vertexCount, color);
    }
}

From source file:com.badlogic.gdx.tests.box2d.ApplyForce.java

License:Apache License

@Override
protected void createWorld(World world) {
    world.setGravity(new Vector2(0, 0));

    float k_restitution = 0.4f;
    Body ground;/*from  w  w  w .  ja  v  a2  s .co m*/

    {
        BodyDef bd = new BodyDef();
        bd.position.set(0, 20);
        ground = world.createBody(bd);

        EdgeShape shape = new EdgeShape();

        FixtureDef sd = new FixtureDef();
        sd.shape = shape;
        sd.density = 0;
        sd.restitution = k_restitution;

        shape.set(new Vector2(-20, -20), new Vector2(-20, 20));
        ground.createFixture(sd);

        shape.set(new Vector2(20, -20), new Vector2(20, 20));
        ground.createFixture(sd);

        shape.set(new Vector2(-20, 20), new Vector2(20, 20));
        ground.createFixture(sd);

        shape.set(new Vector2(-20, -20), new Vector2(20, -20));
        ground.createFixture(sd);

        shape.dispose();
    }

    {
        Transform xf1 = new Transform(new Vector2(), 0.3524f * (float) Math.PI);
        xf1.setPosition(xf1.mul(new Vector2(1, 0)));

        Vector2[] vertices = new Vector2[3];
        vertices[0] = xf1.mul(new Vector2(-1, 0));
        vertices[1] = xf1.mul(new Vector2(1, 0));
        vertices[2] = xf1.mul(new Vector2(0, 0.5f));

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

        FixtureDef sd1 = new FixtureDef();
        sd1.shape = poly1;
        sd1.density = 4.0f;

        Transform xf2 = new Transform(new Vector2(), -0.3524f * (float) Math.PI);
        xf2.setPosition(xf2.mul(new Vector2(-1, 0)));

        vertices[0] = xf2.mul(new Vector2(-1, 0));
        vertices[1] = xf2.mul(new Vector2(1, 0));
        vertices[2] = xf2.mul(new Vector2(0, 0.5f));

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

        FixtureDef sd2 = new FixtureDef();
        sd2.shape = poly2;
        sd2.density = 2.0f;

        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.angularDamping = 5.0f;
        bd.linearDamping = 0.1f;

        bd.position.set(0, 2);
        bd.angle = (float) Math.PI;
        bd.allowSleep = false;
        m_body = world.createBody(bd);
        m_body.createFixture(sd1);
        m_body.createFixture(sd2);
        poly1.dispose();
        poly2.dispose();
    }

    {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.5f, 0.5f);

        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 1.0f;
        fd.friction = 0.3f;

        for (int i = 0; i < 10; i++) {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DynamicBody;

            bd.position.set(0, 5 + 1.54f * i);
            Body body = world.createBody(bd);

            body.createFixture(fd);

            float gravity = 10.0f;
            float I = body.getInertia();
            float mass = body.getMass();

            float radius = (float) Math.sqrt(2 * I / mass);

            FrictionJointDef jd = new FrictionJointDef();
            jd.localAnchorA.set(0, 0);
            jd.localAnchorB.set(0, 0);
            jd.bodyA = ground;
            jd.bodyB = body;
            jd.collideConnected = true;
            jd.maxForce = mass * gravity;
            jd.maxTorque = mass * radius * gravity;

            world.createJoint(jd);
        }

        shape.dispose();
    }
}

From source file:com.blindtigergames.werescrewed.debug.SBox2DDebugRenderer.java

License:Apache License

private void drawAABB(Fixture fixture, Transform transform) {
    if (fixture.getType() == Type.Circle) {

        CircleShape shape = (CircleShape) fixture.getShape();
        float radius = shape.getRadius();
        vertices[0].set(shape.getPosition());
        vertices[0].rotate(transform.getRotation()).add(transform.getPosition());
        lower.set(vertices[0].x - radius, vertices[0].y - radius);
        upper.set(vertices[0].x + radius, vertices[0].y + radius);

        // define vertices in ccw fashion...
        vertices[0].set(lower.x, lower.y);
        vertices[1].set(upper.x, lower.y);
        vertices[2].set(upper.x, upper.y);
        vertices[3].set(lower.x, upper.y);

        drawSolidPolygon(vertices, 4, AABB_COLOR);
    } else if (fixture.getType() == Type.Polygon) {
        PolygonShape shape = (PolygonShape) fixture.getShape();
        int vertexCount = shape.getVertexCount();

        shape.getVertex(0, vertices[0]);
        lower.set(transform.mul(vertices[0]));
        upper.set(lower);//from   ww  w.j av  a 2s . c  o m
        for (int i = 1; i < vertexCount; i++) {
            shape.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
            lower.x = Math.min(lower.x, vertices[i].x);
            lower.y = Math.min(lower.y, vertices[i].y);
            upper.x = Math.max(upper.x, vertices[i].x);
            upper.y = Math.max(upper.y, vertices[i].y);
        }

        // define vertices in ccw fashion...
        vertices[0].set(lower.x, lower.y);
        vertices[1].set(upper.x, lower.y);
        vertices[2].set(upper.x, upper.y);
        vertices[3].set(lower.x, upper.y);

        drawSolidPolygon(vertices, 4, AABB_COLOR);
    }
}

From source file:com.blindtigergames.werescrewed.debug.SBox2DDebugRenderer.java

License:Apache License

private void drawShape(Fixture fixture, Transform transform, Color color) {
    if (fixture.getType() == Type.Circle) {
        CircleShape circle = (CircleShape) fixture.getShape();
        t.set(circle.getPosition());/*from w  w  w  .j  av a  2 s .  c  o  m*/
        transform.mul(t);
        drawSolidCircle(t, circle.getRadius(),
                axis.set(transform.vals[Transform.COS], transform.vals[Transform.SIN]), color);
    }

    if (fixture.getType() == Type.Edge) {
        EdgeShape edge = (EdgeShape) fixture.getShape();
        edge.getVertex1(vertices[0]);
        edge.getVertex2(vertices[1]);
        transform.mul(vertices[0]);
        transform.mul(vertices[1]);
        drawSolidPolygon(vertices, 2, color);
    }

    if (fixture.getType() == Type.Polygon) {
        PolygonShape chain = (PolygonShape) fixture.getShape();
        int vertexCount = chain.getVertexCount();
        for (int i = 0; i < vertexCount; i++) {
            chain.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
        }
        drawSolidPolygon(vertices, vertexCount, color);
    }

    if (fixture.getType() == Type.Chain) {
        ChainShape chain = (ChainShape) fixture.getShape();
        int vertexCount = chain.getVertexCount();
        for (int i = 0; i < vertexCount; i++) {
            chain.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
        }
        drawSolidPolygon(vertices, vertexCount, color);
    }
}

From source file:com.me.mygdxgame.Entities.MydebugRenderer.java

License:Apache License

private void drawAABB(Fixture fixture, Transform transform) {
    if (fixture.getType() == Type.Circle) {

        CircleShape shape = (CircleShape) fixture.getShape();
        float radius = shape.getRadius();
        vertices[0].set(shape.getPosition());
        vertices[0].rotate(transform.getRotation()).add(transform.getPosition());
        lower.set(vertices[0].x - radius, vertices[0].y - radius);
        upper.set(vertices[0].x + radius, vertices[0].y + radius);

        // define vertices in ccw fashion...
        vertices[0].set(lower.x, lower.y);
        vertices[1].set(upper.x, lower.y);
        vertices[2].set(upper.x, upper.y);
        vertices[3].set(lower.x, upper.y);

        drawSolidPolygon(vertices, 4, AABB_COLOR, true);
    } else if (fixture.getType() == Type.Polygon) {
        PolygonShape shape = (PolygonShape) fixture.getShape();
        int vertexCount = shape.getVertexCount();

        shape.getVertex(0, vertices[0]);
        lower.set(transform.mul(vertices[0]));
        upper.set(lower);//w  w  w.  j  a  va  2  s .  c  o m
        for (int i = 1; i < vertexCount; i++) {
            shape.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
            lower.x = Math.min(lower.x, vertices[i].x);
            lower.y = Math.min(lower.y, vertices[i].y);
            upper.x = Math.max(upper.x, vertices[i].x);
            upper.y = Math.max(upper.y, vertices[i].y);
        }

        // define vertices in ccw fashion...
        vertices[0].set(lower.x, lower.y);
        vertices[1].set(upper.x, lower.y);
        vertices[2].set(upper.x, upper.y);
        vertices[3].set(lower.x, upper.y);

        drawSolidPolygon(vertices, 4, AABB_COLOR, true);
    }
}

From source file:com.me.mygdxgame.Entities.MydebugRenderer.java

License:Apache License

private void drawShape(Fixture fixture, Transform transform, Color color) {
    if (fixture.getType() == Type.Circle) {
        CircleShape circle = (CircleShape) fixture.getShape();
        t.set(circle.getPosition());//from  ww  w  .j a v a  2  s  .c  o m

        transform.mul(t);
        t.x = t.x * 29f;
        t.y = t.y * 29f;
        drawSolidCircle(t, circle.getRadius() * 29f,
                axis.set(transform.vals[Transform.COS], transform.vals[Transform.SIN]), color);
        return;
    }

    if (fixture.getType() == Type.Edge) {
        EdgeShape edge = (EdgeShape) fixture.getShape();
        edge.getVertex1(vertices[0]);
        edge.getVertex2(vertices[1]);
        transform.mul(vertices[0]);
        transform.mul(vertices[1]);
        drawSolidPolygon(vertices, 2, color, true);
        return;
    }

    if (fixture.getType() == Type.Polygon) {
        PolygonShape chain = (PolygonShape) fixture.getShape();
        int vertexCount = chain.getVertexCount();
        for (int i = 0; i < vertexCount; i++) {
            chain.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
            vertices[i].x = vertices[i].x * 29f;
            vertices[i].y = vertices[i].y * 29f;
        }
        drawSolidPolygon(vertices, vertexCount, color, true);
        return;
    }

    if (fixture.getType() == Type.Chain) {
        ChainShape chain = (ChainShape) fixture.getShape();
        int vertexCount = chain.getVertexCount();
        for (int i = 0; i < vertexCount; i++) {
            chain.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
        }
        drawSolidPolygon(vertices, vertexCount, color, false);
    }
}

From source file:org.box2d.r3.gdx.GDXBox2DDebugRenderer.java

License:Apache License

private void drawAABB(final Fixture fixture, final Transform transform) {
    if (fixture.getType() == Type.Circle) {

        final CircleShape shape = (CircleShape) fixture.getShape();
        final float radius = shape.getRadius();
        vertices[0].set(shape.getPosition());
        vertices[0].rotate(transform.getRotation()).add(transform.getPosition());
        lower.set(vertices[0].x - radius, vertices[0].y - radius);
        upper.set(vertices[0].x + radius, vertices[0].y + radius);

        // define vertices in ccw fashion...
        vertices[0].set(lower.x, lower.y);
        vertices[1].set(upper.x, lower.y);
        vertices[2].set(upper.x, upper.y);
        vertices[3].set(lower.x, upper.y);

        this.drawSolidPolygon(vertices, 4, this.AABB_COLOR, true);
    } else if (fixture.getType() == Type.Polygon) {
        final PolygonShape shape = (PolygonShape) fixture.getShape();
        final int vertexCount = shape.getVertexCount();

        shape.getVertex(0, vertices[0]);
        lower.set(transform.mul(vertices[0]));
        upper.set(lower);/*w w w . j a v  a  2 s  .co m*/
        for (int i = 1; i < vertexCount; i++) {
            shape.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
            lower.x = Math.min(lower.x, vertices[i].x);
            lower.y = Math.min(lower.y, vertices[i].y);
            upper.x = Math.max(upper.x, vertices[i].x);
            upper.y = Math.max(upper.y, vertices[i].y);
        }

        // define vertices in ccw fashion...
        vertices[0].set(lower.x, lower.y);
        vertices[1].set(upper.x, lower.y);
        vertices[2].set(upper.x, upper.y);
        vertices[3].set(lower.x, upper.y);

        this.drawSolidPolygon(vertices, 4, this.AABB_COLOR, true);
    }
}

From source file:org.box2d.r3.gdx.GDXBox2DDebugRenderer.java

License:Apache License

private void drawShape(final Fixture fixture, final Transform transform, final Color color) {
    if (fixture.getType() == Type.Circle) {
        final CircleShape circle = (CircleShape) fixture.getShape();
        t.set(circle.getPosition());//from   w w  w  . jav a2s  .co  m
        transform.mul(t);
        this.drawSolidCircle(t, circle.getRadius(),
                axis.set(transform.vals[Transform.COS], transform.vals[Transform.SIN]), color);
        return;
    }

    if (fixture.getType() == Type.Edge) {
        final EdgeShape edge = (EdgeShape) fixture.getShape();
        edge.getVertex1(vertices[0]);
        edge.getVertex2(vertices[1]);
        transform.mul(vertices[0]);
        transform.mul(vertices[1]);
        this.drawSolidPolygon(vertices, 2, color, true);
        return;
    }

    if (fixture.getType() == Type.Polygon) {
        final PolygonShape chain = (PolygonShape) fixture.getShape();
        final int vertexCount = chain.getVertexCount();
        for (int i = 0; i < vertexCount; i++) {
            chain.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
        }
        this.drawSolidPolygon(vertices, vertexCount, color, true);
        return;
    }

    if (fixture.getType() == Type.Chain) {
        final ChainShape chain = (ChainShape) fixture.getShape();
        final int vertexCount = chain.getVertexCount();
        for (int i = 0; i < vertexCount; i++) {
            chain.getVertex(i, vertices[i]);
            transform.mul(vertices[i]);
        }
        this.drawSolidPolygon(vertices, vertexCount, color, false);
    }
}

From source file:org.catrobat.catroid.physics.PhysicsObject.java

License:Open Source License

private void calculateAabb(Fixture fixture, Transform transform) {
    fixtureAabbLowerLeft = new Vector2(Integer.MAX_VALUE, Integer.MAX_VALUE);
    fixtureAabbUpperRight = new Vector2(Integer.MIN_VALUE, Integer.MIN_VALUE);
    if (fixture.getType() == Shape.Type.Circle) {
        CircleShape shape = (CircleShape) fixture.getShape();
        float radius = shape.getRadius();
        tmpVertice.set(shape.getPosition());
        tmpVertice.rotate(transform.getRotation()).add(transform.getPosition());
        fixtureAabbLowerLeft.set(tmpVertice.x - radius, tmpVertice.y - radius);
        fixtureAabbUpperRight.set(tmpVertice.x + radius, tmpVertice.y + radius);
    } else if (fixture.getType() == Shape.Type.Polygon) {
        PolygonShape shape = (PolygonShape) fixture.getShape();
        int vertexCount = shape.getVertexCount();

        shape.getVertex(0, tmpVertice);//  w  w w .j av a  2 s  .  c  om
        fixtureAabbLowerLeft.set(transform.mul(tmpVertice));
        fixtureAabbUpperRight.set(fixtureAabbLowerLeft);
        for (int i = 1; i < vertexCount; i++) {
            shape.getVertex(i, tmpVertice);
            transform.mul(tmpVertice);
            fixtureAabbLowerLeft.x = Math.min(fixtureAabbLowerLeft.x, tmpVertice.x);
            fixtureAabbLowerLeft.y = Math.min(fixtureAabbLowerLeft.y, tmpVertice.y);
            fixtureAabbUpperRight.x = Math.max(fixtureAabbUpperRight.x, tmpVertice.x);
            fixtureAabbUpperRight.y = Math.max(fixtureAabbUpperRight.y, tmpVertice.y);
        }
    }

    bodyAabbLowerLeft.x = Math.min(fixtureAabbLowerLeft.x, bodyAabbLowerLeft.x);
    bodyAabbLowerLeft.y = Math.min(fixtureAabbLowerLeft.y, bodyAabbLowerLeft.y);
    bodyAabbUpperRight.x = Math.max(fixtureAabbUpperRight.x, bodyAabbUpperRight.x);
    bodyAabbUpperRight.y = Math.max(fixtureAabbUpperRight.y, bodyAabbUpperRight.y);
}

From source file:zzz.box2dtest.ApplyForce.java

License:Apache License

@Override
protected void createWorld(World world) {
    world.setGravity(new Vector2(0, 0));
    Gdx.input.setInputProcessor(this);

    float k_restitution = 0.4f;
    Body ground;/* ww w  . ja va  2s  . c  om*/

    {
        BodyDef bd = new BodyDef();
        bd.position.set(0, 20);
        ground = world.createBody(bd);

        EdgeShape shape = new EdgeShape();

        FixtureDef sd = new FixtureDef();
        sd.shape = shape;
        sd.density = 0;
        sd.restitution = k_restitution;

        shape.set(new Vector2(-20, -20), new Vector2(-20, 20));
        ground.createFixture(sd);

        shape.set(new Vector2(20, -20), new Vector2(20, 20));
        ground.createFixture(sd);

        shape.set(new Vector2(-20, 20), new Vector2(20, 20));
        ground.createFixture(sd);

        shape.set(new Vector2(-20, -20), new Vector2(20, -20));
        ground.createFixture(sd);

        shape.dispose();
    }

    {
        Transform xf1 = new Transform(new Vector2(), 0.3524f * (float) Math.PI);
        xf1.setPosition(xf1.mul(new Vector2(1, 0)));

        Vector2[] vertices = new Vector2[3];
        vertices[0] = xf1.mul(new Vector2(-1, 0));
        vertices[1] = xf1.mul(new Vector2(1, 0));
        vertices[2] = xf1.mul(new Vector2(0, 0.5f));

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

        FixtureDef sd1 = new FixtureDef();
        sd1.shape = poly1;
        sd1.density = 4.0f;

        Transform xf2 = new Transform(new Vector2(), -0.3524f * (float) Math.PI);
        xf2.setPosition(xf2.mul(new Vector2(-1, 0)));

        vertices[0] = xf2.mul(new Vector2(-1, 0));
        vertices[1] = xf2.mul(new Vector2(1, 0));
        vertices[2] = xf2.mul(new Vector2(0, 0.5f));

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

        FixtureDef sd2 = new FixtureDef();
        sd2.shape = poly2;
        sd2.density = 2.0f;

        BodyDef bd = new BodyDef();
        bd.type = BodyType.DynamicBody;
        bd.angularDamping = 5.0f;
        bd.linearDamping = 0.1f;

        bd.position.set(0, 2);
        bd.angle = (float) Math.PI;
        bd.allowSleep = false;
        m_body = world.createBody(bd);
        m_body.createFixture(sd1);
        m_body.createFixture(sd2);
        poly1.dispose();
        poly2.dispose();
    }

    {
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(0.5f, 0.5f);

        FixtureDef fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 1.0f;
        fd.friction = 0.3f;

        for (int i = 0; i < 10; i++) {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DynamicBody;

            bd.position.set(0, 5 + 1.54f * i);
            Body body = world.createBody(bd);

            body.createFixture(fd);

            float gravity = 10.0f;
            float I = body.getInertia();
            float mass = body.getMass();

            float radius = (float) Math.sqrt(2 * I / mass);

            FrictionJointDef jd = new FrictionJointDef();
            jd.localAnchorA.set(0, 0);
            jd.localAnchorB.set(0, 0);
            jd.bodyA = ground;
            jd.bodyB = body;
            jd.collideConnected = true;
            jd.maxForce = mass * gravity;
            jd.maxTorque = mass * radius * gravity;

            world.createJoint(jd);
        }

        shape.dispose();
    }
}