Search in sources :

Example 11 with Fixture

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

the class BodyBuilder method build.

public Body build(boolean disposeShapes) {
    Body body = world.createBody(bodyDef);
    for (int i = 0; i < fixtureDefs.size(); i++) {
        FixtureDef fixtureDef = fixtureDefs.get(i);
        Fixture fixture = body.createFixture(fixtureDef);
        fixture.setUserData(fixtureUserDatas.get(i));
    }
    if (massSet) {
        MassData bodyMassData = body.getMassData();
        // massData.center.set(position);
        massData.center.set(bodyMassData.center);
        // massData.I = bodyMassData.I;
        body.setMassData(massData);
    }
    // MassData massData = body.getMassData();
    // massData.mass = mass;
    // massData.I = 1f;
    body.setUserData(userData);
    body.setTransform(position, angle);
    reset(disposeShapes);
    return body;
}
Also used : MassData(com.badlogic.gdx.physics.box2d.MassData) Fixture(com.badlogic.gdx.physics.box2d.Fixture) Body(com.badlogic.gdx.physics.box2d.Body) FixtureDef(com.badlogic.gdx.physics.box2d.FixtureDef)

Example 12 with Fixture

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

the class Contacts method addContact.

public void addContact(com.badlogic.gdx.physics.box2d.Contact contact, boolean AB) {
    Vector2 normal = contact.getWorldManifold().getNormal();
    Fixture myFixture;
    Fixture otherFixture;
    if (AB) {
        myFixture = contact.getFixtureA();
        otherFixture = contact.getFixtureB();
    } else {
        myFixture = contact.getFixtureB();
        otherFixture = contact.getFixtureA();
        // if the body in contact is the first one declared by the contact, then we have to invert the normal.
        normal.mul(-1);
    }
    addContact(myFixture, otherFixture, normal);
}
Also used : Vector2(com.badlogic.gdx.math.Vector2) Fixture(com.badlogic.gdx.physics.box2d.Fixture)

Example 13 with Fixture

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

the class ContactsTest method removesSecondContactIfItMatches.

@Test
public void removesSecondContactIfItMatches() {
    final Fixture fixtureC = mockery.mock(Fixture.class);
    mockery.checking(new Expectations() {

        {
            ignoring(fixtureA);
            ignoring(fixtureB);
            ignoring(fixtureC);
        }
    });
    contacts.addContact(fixtureA, fixtureC, new Vector2());
    contacts.addContact(fixtureA, fixtureB, normal);
    assertTrue(contacts.isInContact());
    assertThat(2, IsEqual.equalTo(contacts.getContactCount()));
    contacts.removeContact(fixtureA, fixtureB);
    assertTrue(contacts.isInContact());
    assertThat(1, IsEqual.equalTo(contacts.getContactCount()));
    Contact stillValidContact = contacts.contacts.get(0);
    assertTrue(stillValidContact.inContact);
    assertSame(fixtureA, stillValidContact.myFixture);
    assertSame(fixtureC, stillValidContact.otherFixture);
    Contact removedContact = contacts.contacts.get(1);
    assertFalse(removedContact.inContact);
    assertNull(removedContact.myFixture);
    assertNull(removedContact.otherFixture);
}
Also used : Expectations(org.jmock.Expectations) Vector2(com.badlogic.gdx.math.Vector2) Fixture(com.badlogic.gdx.physics.box2d.Fixture) Contact(com.gemserk.commons.gdx.box2d.Contacts.Contact) Test(org.junit.Test)

Example 14 with Fixture

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

the class ContactsTest method removesFirstOfTwoContactIfItMatches.

@Test
public void removesFirstOfTwoContactIfItMatches() {
    final Fixture fixtureC = mockery.mock(Fixture.class);
    mockery.checking(new Expectations() {

        {
            ignoring(fixtureA);
            ignoring(fixtureB);
            ignoring(fixtureC);
        }
    });
    contacts.addContact(fixtureA, fixtureB, normal);
    contacts.addContact(fixtureA, fixtureC, new Vector2());
    assertTrue(contacts.isInContact());
    assertThat(2, IsEqual.equalTo(contacts.getContactCount()));
    contacts.removeContact(fixtureA, fixtureB);
    assertTrue(contacts.isInContact());
    assertThat(1, IsEqual.equalTo(contacts.getContactCount()));
    Contact stillValidContact = contacts.contacts.get(0);
    assertTrue(stillValidContact.inContact);
    assertSame(fixtureA, stillValidContact.myFixture);
    assertSame(fixtureC, stillValidContact.otherFixture);
    Contact removedContact = contacts.contacts.get(1);
    assertFalse(removedContact.inContact);
    assertNull(removedContact.myFixture);
    assertNull(removedContact.otherFixture);
}
Also used : Expectations(org.jmock.Expectations) Vector2(com.badlogic.gdx.math.Vector2) Fixture(com.badlogic.gdx.physics.box2d.Fixture) Contact(com.gemserk.commons.gdx.box2d.Contacts.Contact) Test(org.junit.Test)

Example 15 with Fixture

use of com.badlogic.gdx.physics.box2d.Fixture in project RubeLoader by tescott.

the class RubeLoaderTest method endContact.

@Override
public void endContact(Contact contact) {
    Fixture fixA = contact.getFixtureA();
    Fixture fixB = contact.getFixtureB();
    if ((fixA.isSensor()) && (fixA.getUserData() != null)) {
        B2Controller b2c = (B2Controller) fixA.getUserData();
        b2c.removeBody(fixB.getBody());
    } else if ((fixB.isSensor()) && (fixB.getUserData() != null)) {
        B2Controller b2c = (B2Controller) fixB.getUserData();
        b2c.removeBody(fixA.getBody());
    }
}
Also used : Fixture(com.badlogic.gdx.physics.box2d.Fixture) B2Controller(com.gushikustudios.box2d.controllers.B2Controller)

Aggregations

Fixture (com.badlogic.gdx.physics.box2d.Fixture)18 Body (com.badlogic.gdx.physics.box2d.Body)6 Vector2 (com.badlogic.gdx.math.Vector2)5 PolygonShape (com.badlogic.gdx.physics.box2d.PolygonShape)4 Contact (com.gemserk.commons.gdx.box2d.Contacts.Contact)3 B2Controller (com.gushikustudios.box2d.controllers.B2Controller)3 Texture (com.badlogic.gdx.graphics.Texture)2 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)2 CircleShape (com.badlogic.gdx.physics.box2d.CircleShape)2 FixtureDef (com.badlogic.gdx.physics.box2d.FixtureDef)2 MassData (com.badlogic.gdx.physics.box2d.MassData)2 Expectations (org.jmock.Expectations)2 Test (org.junit.Test)2 Entity (com.artemis.Entity)1 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)1 PolygonRegion (com.badlogic.gdx.graphics.g2d.PolygonRegion)1 PolygonSpriteBatch (com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch)1 SpriteBatch (com.badlogic.gdx.graphics.g2d.SpriteBatch)1 EarClippingTriangulator (com.badlogic.gdx.math.EarClippingTriangulator)1 Vector3 (com.badlogic.gdx.math.Vector3)1