Search in sources :

Example 1 with WorldManifold

use of com.badlogic.gdx.physics.box2d.WorldManifold in project libgdx by libgdx.

the class Box2DCharacterControllerTest method isPlayerGrounded.

private boolean isPlayerGrounded(float deltaTime) {
    groundedPlatform = null;
    Array<Contact> contactList = world.getContactList();
    for (int i = 0; i < contactList.size; i++) {
        Contact contact = contactList.get(i);
        if (contact.isTouching() && (contact.getFixtureA() == playerSensorFixture || contact.getFixtureB() == playerSensorFixture)) {
            Vector2 pos = player.getPosition();
            WorldManifold manifold = contact.getWorldManifold();
            boolean below = true;
            for (int j = 0; j < manifold.getNumberOfContactPoints(); j++) {
                below &= (manifold.getPoints()[j].y < pos.y - 1.5f);
            }
            if (below) {
                if (contact.getFixtureA().getUserData() != null && contact.getFixtureA().getUserData().equals("p")) {
                    groundedPlatform = (Platform) contact.getFixtureA().getBody().getUserData();
                }
                if (contact.getFixtureB().getUserData() != null && contact.getFixtureB().getUserData().equals("p")) {
                    groundedPlatform = (Platform) contact.getFixtureB().getBody().getUserData();
                }
                return true;
            }
            return false;
        }
    }
    return false;
}
Also used : WorldManifold(com.badlogic.gdx.physics.box2d.WorldManifold) Vector2(com.badlogic.gdx.math.Vector2) Contact(com.badlogic.gdx.physics.box2d.Contact)

Example 2 with WorldManifold

use of com.badlogic.gdx.physics.box2d.WorldManifold 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 3 with WorldManifold

use of com.badlogic.gdx.physics.box2d.WorldManifold in project commons-gdx by gemserk.

the class ContactsTest method setUp.

@Before
public void setUp() {
    contacts = new Contacts();
    box2dcontact = mockery.mock(com.badlogic.gdx.physics.box2d.Contact.class);
    worldManifold = mockery.mock(WorldManifold.class);
    normal = new Vector2(0, 1);
    fixtureA = mockery.mock(Fixture.class, "fixtureA");
    fixtureB = mockery.mock(Fixture.class, "fixtureB");
}
Also used : WorldManifold(com.badlogic.gdx.physics.box2d.WorldManifold) Vector2(com.badlogic.gdx.math.Vector2) Fixture(com.badlogic.gdx.physics.box2d.Fixture) Contact(com.gemserk.commons.gdx.box2d.Contacts.Contact) Before(org.junit.Before)

Aggregations

Vector2 (com.badlogic.gdx.math.Vector2)3 WorldManifold (com.badlogic.gdx.physics.box2d.WorldManifold)3 Contact (com.badlogic.gdx.physics.box2d.Contact)2 Body (com.badlogic.gdx.physics.box2d.Body)1 Fixture (com.badlogic.gdx.physics.box2d.Fixture)1 MouseJoint (com.badlogic.gdx.physics.box2d.joints.MouseJoint)1 Contact (com.gemserk.commons.gdx.box2d.Contacts.Contact)1 Before (org.junit.Before)1