Search in sources :

Example 21 with Vector2

use of com.badlogic.gdx.math.Vector2 in project libgdx by libgdx.

the class Box2DTest method render.

@Override
public void render() {
    // first we update the world. For simplicity
    // we use the delta time provided by the Graphics
    // instance. Normally you'll want to fix the time
    // step.
    long start = TimeUtils.nanoTime();
    world.step(Gdx.graphics.getDeltaTime(), 8, 3);
    float updateTime = (TimeUtils.nanoTime() - start) / 1000000000.0f;
    // next we clear the color buffer and set the camera
    // matrices
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.update();
    // next we render the ground body
    renderBox(groundBody, 50, 1);
    // next we render each box via the SpriteBatch.
    // for this we have to set the projection matrix of the
    // spritebatch to the camera's combined matrix. This will
    // make the spritebatch work in world coordinates
    batch.getProjectionMatrix().set(camera.combined);
    batch.begin();
    for (int i = 0; i < boxes.size(); i++) {
        Body box = boxes.get(i);
        // that's the box's center position
        Vector2 position = box.getPosition();
        // the rotation angle around the center
        float angle = MathUtils.radiansToDegrees * box.getAngle();
        // the bottom left corner of the box, unrotated
        batch.draw(// the bottom left corner of the box, unrotated
        textureRegion, // the bottom left corner of the box, unrotated
        position.x - 1, // the bottom left corner of the box, unrotated
        position.y - 1, // the rotation center relative to the bottom left corner of the box
        1f, // the rotation center relative to the bottom left corner of the box
        1f, // the width and height of the box
        2, // the width and height of the box
        2, // the scale on the x- and y-axis
        1, // the scale on the x- and y-axis
        1, // the rotation angle
        angle);
    }
    batch.end();
    // next we use the debug renderer. Note that we
    // simply apply the camera again and then call
    // the renderer. the camera.apply() call is actually
    // not needed as the opengl matrices are already set
    // by the spritebatch which in turn uses the camera matrices :)
    debugRenderer.render(world, camera.combined);
    // finally we render all contact points
    renderer.setProjectionMatrix(camera.combined);
    renderer.begin(ShapeType.Point);
    renderer.setColor(0, 1, 0, 1);
    for (int i = 0; i < world.getContactCount(); i++) {
        Contact contact = world.getContactList().get(i);
        // we only render the contact if it actually touches
        if (contact.isTouching()) {
            // get the world manifold from which we get the
            // contact points. A manifold can have 0, 1 or 2
            // contact points.
            WorldManifold manifold = contact.getWorldManifold();
            int numContactPoints = manifold.getNumberOfContactPoints();
            for (int j = 0; j < numContactPoints; j++) {
                Vector2 point = manifold.getPoints()[j];
                renderer.point(point.x, point.y, 0);
            }
        }
    }
    renderer.end();
    // finally we render the time it took to update the world
    // for this we have to set the projection matrix again, so
    // we work in pixel coordinates
    batch.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    batch.begin();
    font.draw(batch, "fps: " + Gdx.graphics.getFramesPerSecond() + " update time: " + updateTime, 0, 20);
    batch.end();
}
Also used : WorldManifold(com.badlogic.gdx.physics.box2d.WorldManifold) Vector2(com.badlogic.gdx.math.Vector2) Body(com.badlogic.gdx.physics.box2d.Body) MouseJoint(com.badlogic.gdx.physics.box2d.joints.MouseJoint) Contact(com.badlogic.gdx.physics.box2d.Contact)

Example 22 with Vector2

use of com.badlogic.gdx.math.Vector2 in project libgdx by libgdx.

the class Box2DTest method createPhysicsWorld.

private void createPhysicsWorld() {
    // we instantiate a new World with a proper gravity vector
    // and tell it to sleep when possible.
    world = new World(new Vector2(0, -10), true);
    float[] vertices = { -0.07421887f, -0.16276085f, -0.12109375f, -0.22786504f, -0.157552f, -0.7122401f, 0.04296875f, -0.7122401f, 0.110677004f, -0.6419276f, 0.13151026f, -0.49869835f, 0.08984375f, -0.3190109f };
    PolygonShape shape = new PolygonShape();
    shape.set(vertices);
    // next we create a static ground platform. This platform
    // is not moveable and will not react to any influences from
    // outside. It will however influence other bodies. First we
    // create a PolygonShape that holds the form of the platform.
    // it will be 100 meters wide and 2 meters high, centered
    // around the origin
    PolygonShape groundPoly = new PolygonShape();
    groundPoly.setAsBox(50, 1);
    // next we create the body for the ground platform. It's
    // simply a static body.
    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.type = BodyType.StaticBody;
    groundBody = world.createBody(groundBodyDef);
    // finally we add a fixture to the body using the polygon
    // defined above. Note that we have to dispose PolygonShapes
    // and CircleShapes once they are no longer used. This is the
    // only time you have to care explicitly for memory management.
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = groundPoly;
    fixtureDef.filter.groupIndex = 0;
    groundBody.createFixture(fixtureDef);
    groundPoly.dispose();
    // We also create a simple ChainShape we put above our
    // ground polygon for extra funkyness.
    ChainShape chainShape = new ChainShape();
    chainShape.createLoop(new Vector2[] { new Vector2(-10, 10), new Vector2(-10, 5), new Vector2(10, 5), new Vector2(10, 11) });
    BodyDef chainBodyDef = new BodyDef();
    chainBodyDef.type = BodyType.StaticBody;
    Body chainBody = world.createBody(chainBodyDef);
    chainBody.createFixture(chainShape, 0);
    chainShape.dispose();
    createBoxes();
    // You can savely ignore the rest of this method :)
    world.setContactListener(new ContactListener() {

        @Override
        public void beginContact(Contact contact) {
        // System.out.println("begin contact");
        }

        @Override
        public void endContact(Contact contact) {
        // System.out.println("end contact");
        }

        @Override
        public void preSolve(Contact contact, Manifold oldManifold) {
        // Manifold.ManifoldType type = oldManifold.getType();
        // Vector2 localPoint = oldManifold.getLocalPoint();
        // Vector2 localNormal = oldManifold.getLocalNormal();
        // int pointCount = oldManifold.getPointCount();
        // ManifoldPoint[] points = oldManifold.getPoints();
        // System.out.println("pre solve, " + type +
        // ", point: " + localPoint +
        // ", local normal: " + localNormal +
        // ", #points: " + pointCount +
        // ", [" + points[0] + ", " + points[1] + "]");
        }

        @Override
        public void postSolve(Contact contact, ContactImpulse impulse) {
        // float[] ni = impulse.getNormalImpulses();
        // float[] ti = impulse.getTangentImpulses();
        // System.out.println("post solve, normal impulses: " + ni[0] + ", " + ni[1] + ", tangent impulses: " + ti[0] + ", " + ti[1]);
        }
    });
}
Also used : PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) World(com.badlogic.gdx.physics.box2d.World) Contact(com.badlogic.gdx.physics.box2d.Contact) WorldManifold(com.badlogic.gdx.physics.box2d.WorldManifold) Manifold(com.badlogic.gdx.physics.box2d.Manifold) Vector2(com.badlogic.gdx.math.Vector2) ContactImpulse(com.badlogic.gdx.physics.box2d.ContactImpulse) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) ChainShape(com.badlogic.gdx.physics.box2d.ChainShape) Body(com.badlogic.gdx.physics.box2d.Body) FixtureDef(com.badlogic.gdx.physics.box2d.FixtureDef) ContactListener(com.badlogic.gdx.physics.box2d.ContactListener)

Example 23 with Vector2

use of com.badlogic.gdx.math.Vector2 in project libgdx by libgdx.

the class Box2DTest method renderBox.

private void renderBox(Body body, float halfWidth, float halfHeight) {
    // get the bodies center and angle in world coordinates
    Vector2 pos = body.getWorldCenter();
    float angle = body.getAngle();
    // set the translation and rotation matrix
    transform.setToTranslation(pos.x, pos.y, 0);
    transform.rotate(0, 0, 1, (float) Math.toDegrees(angle));
    // render the box
    renderer.begin(ShapeType.Line);
    renderer.setTransformMatrix(transform);
    renderer.setColor(1, 1, 1, 1);
    renderer.rect(-halfWidth, -halfHeight, halfWidth * 2, halfHeight * 2);
    renderer.end();
}
Also used : Vector2(com.badlogic.gdx.math.Vector2)

Example 24 with Vector2

use of com.badlogic.gdx.math.Vector2 in project libgdx by libgdx.

the class Affine2Test method create.

@Override
public void create() {
    Vector2 trn = new Vector2(30, 50);
    float rot = 35;
    float cos = (float) Math.cos(MathUtils.degreesToRadians * rot);
    float sin = (float) Math.sin(MathUtils.degreesToRadians * rot);
    Vector2 scl = new Vector2(0.42f, 1.19f);
    Vector2 shear = new Vector2(0.35f, 0.71f);
    Matrix3 mat1 = new Matrix3();
    Matrix3 mat2 = new Matrix3();
    Affine2 afn1 = new Affine2();
    Affine2 afn2 = new Affine2();
    // check setter - identity
    checkEqual(mat1, new float[] { 1, 0, 0, 0, 1, 0, 0, 0, 1 });
    checkEqual(mat1, mat2.idt());
    checkEqual(mat1, afn1);
    checkEqual(mat1, afn1.idt());
    // check setter - translation
    mat1.setToTranslation(trn);
    checkEqual(mat1, new float[] { 1, 0, 0, 0, 1, 0, trn.x, trn.y, 1 });
    afn1.setToTranslation(trn);
    checkEqual(mat1, afn1);
    // check setter - scale
    mat1.setToScaling(scl);
    checkEqual(mat1, new float[] { scl.x, 0, 0, 0, scl.y, 0, 0, 0, 1 });
    afn1.setToScaling(scl);
    checkEqual(mat1, afn1);
    // check setter - rotation
    mat1.setToRotation(rot);
    checkEqual(mat1, new float[] { cos, sin, 0, -sin, cos, 0, 0, 0, 1 });
    afn1.setToRotation(rot);
    checkEqual(mat1, afn1);
    mat1.setToRotationRad(MathUtils.degreesToRadians * rot);
    checkEqual(mat1, afn1);
    afn1.setToRotationRad(MathUtils.degreesToRadians * rot);
    checkEqual(mat1, afn1);
    // check setter - shearing
    afn1.setToShearing(shear);
    checkEqual(mat1.set(afn1), new float[] { 1, shear.y, 0, shear.x, 1, 0, 0, 0, 1 });
    // check setter - translation x rotation x scale
    afn1.setToTrnRotScl(trn, rot, scl);
    afn2.setToTrnRotRadScl(trn, MathUtils.degreesToRadians * rot, scl);
    checkEqual(afn1, afn2);
    afn2.setToTranslation(trn).rotate(rot).scale(scl);
    checkEqual(afn1, afn2);
    // check setter - translation x scale
    afn1.setToTrnRotScl(trn, 0, scl);
    afn2.setToTrnScl(trn, scl);
    checkEqual(afn1, afn2);
    // check post-multiplication
    mat1.idt().scale(scl).rotate(rot).translate(trn).mul(mat2.set(afn2.setToShearing(shear)));
    afn1.idt().scale(scl).rotate(rot).translate(trn).shear(shear);
    checkEqual(mat1, afn1);
    afn1.idt().mul(afn2.setToScaling(scl)).mul(afn2.setToRotation(rot)).mul(afn2.setToTranslation(trn)).mul(afn2.setToShearing(shear));
    checkEqual(mat1, afn1);
    // check pre-multiplication
    afn1.idt().preShear(shear).preTranslate(trn).preRotate(rot).preScale(scl);
    checkEqual(mat1, afn1);
    afn1.idt().preMul(afn2.setToShearing(shear)).preMul(afn2.setToTranslation(trn)).preMul(afn2.setToRotation(rot)).preMul(afn2.setToScaling(scl));
    checkEqual(mat1, afn1);
    mat1.set(afn2.setToShearing(shear)).trn(trn).mulLeft(mat2.setToRotation(rot)).mulLeft(mat2.setToScaling(scl));
    checkEqual(mat1, afn1);
    // check determinant and inverse
    checkEqual(mat1.det(), afn1.det());
    check(afn1.det() == (afn1.m00 * afn1.m11 - afn1.m01 * afn1.m10));
    mat1.inv();
    afn2.set(afn1).inv();
    checkEqual(mat1, afn2);
    checkEqual(afn1.det(), 1 / afn2.det());
    // check for exception when trying to invert singular matrices
    boolean didThrow = false;
    afn1.setToShearing(1, 1);
    try {
        afn1.inv();
    } catch (GdxRuntimeException e) {
        didThrow = true;
    }
    check(didThrow);
    System.out.println("All tests passed.");
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Affine2(com.badlogic.gdx.math.Affine2) Vector2(com.badlogic.gdx.math.Vector2) Matrix3(com.badlogic.gdx.math.Matrix3)

Example 25 with Vector2

use of com.badlogic.gdx.math.Vector2 in project libgdx by libgdx.

the class VerticalStack method createWorld.

@Override
protected void createWorld(World world) {
    {
        BodyDef bd = new BodyDef();
        Body ground = world.createBody(bd);
        EdgeShape shape = new EdgeShape();
        shape.set(new Vector2(-40, 0), new Vector2(40, 0));
        ground.createFixture(shape, 0.0f);
        shape.set(new Vector2(20, 0), new Vector2(20, 20));
        ground.createFixture(shape, 0);
        shape.dispose();
    }
    float[] xs = { 0, -10, -5, 5, 10 };
    for (int j = 0; j < e_columnCount; j++) {
        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 < e_rowCount; i++) {
            BodyDef bd = new BodyDef();
            bd.type = BodyType.DynamicBody;
            int n = j * e_rowCount + i;
            m_indices[n] = n;
            float x = 0;
            bd.position.set(xs[j] + x, 0.752f + 1.54f * i);
            Body body = world.createBody(bd);
            body.setUserData(n);
            m_bodies[n] = body;
            body.createFixture(fd);
        }
        shape.dispose();
    }
    m_bullet = null;
}
Also used : EdgeShape(com.badlogic.gdx.physics.box2d.EdgeShape) PolygonShape(com.badlogic.gdx.physics.box2d.PolygonShape) Vector2(com.badlogic.gdx.math.Vector2) BodyDef(com.badlogic.gdx.physics.box2d.BodyDef) Body(com.badlogic.gdx.physics.box2d.Body) FixtureDef(com.badlogic.gdx.physics.box2d.FixtureDef)

Aggregations

Vector2 (com.badlogic.gdx.math.Vector2)103 Body (com.badlogic.gdx.physics.box2d.Body)21 BodyDef (com.badlogic.gdx.physics.box2d.BodyDef)14 FixtureDef (com.badlogic.gdx.physics.box2d.FixtureDef)10 PolygonShape (com.badlogic.gdx.physics.box2d.PolygonShape)10 Texture (com.badlogic.gdx.graphics.Texture)7 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)7 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)7 EdgeShape (com.badlogic.gdx.physics.box2d.EdgeShape)7 Vector3 (com.badlogic.gdx.math.Vector3)6 Fixture (com.badlogic.gdx.physics.box2d.Fixture)6 World (com.badlogic.gdx.physics.box2d.World)6 GameEntity (ilargia.egdx.logicbricks.gen.game.GameEntity)6 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)5 CircleShape (com.badlogic.gdx.physics.box2d.CircleShape)5 Test (org.junit.Test)5 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)4 Box2DDebugRenderer (com.badlogic.gdx.physics.box2d.Box2DDebugRenderer)4 Contact (com.badlogic.gdx.physics.box2d.Contact)4 WorldManifold (com.badlogic.gdx.physics.box2d.WorldManifold)4