Search in sources :

Example 1 with Contact

use of com.gemserk.commons.gdx.box2d.Contacts.Contact in project commons-gdx by gemserk.

the class ContactsTest method whenAddingOneContactBAItIsAdded.

@Test
public void whenAddingOneContactBAItIsAdded() {
    mockery.checking(new Expectations() {

        {
            oneOf(box2dcontact).getWorldManifold();
            will(returnValue(worldManifold));
            oneOf(worldManifold).getNormal();
            will(returnValue(normal));
            oneOf(box2dcontact).getFixtureA();
            will(returnValue(fixtureA));
            oneOf(box2dcontact).getFixtureB();
            will(returnValue(fixtureB));
            ignoring(fixtureA);
            ignoring(fixtureB);
        }
    });
    contacts.addContact(box2dcontact, false);
    assertTrue(contacts.isInContact());
    assertThat(1, IsEqual.equalTo(contacts.getContactCount()));
    Contact contact = contacts.getContact(0);
    assertThat(contact.getNormal().dst(0, -1), OrderingComparisons.lessThan(0.1f));
    assertSame(fixtureA, contact.getOtherFixture());
    assertSame(fixtureB, contact.getMyFixture());
}
Also used : Expectations(org.jmock.Expectations) Contact(com.gemserk.commons.gdx.box2d.Contacts.Contact) Test(org.junit.Test)

Example 2 with Contact

use of com.gemserk.commons.gdx.box2d.Contacts.Contact in project commons-gdx by gemserk.

the class PhysicsUtils method releaseContacts.

public static void releaseContacts(Contacts contacts) {
    // removes contact from the other entity
    for (int i = 0; i < contacts.getContactCount(); i++) {
        Contact contact = contacts.getContact(i);
        Fixture myFixture = contact.getMyFixture();
        Fixture otherFixture = contact.getOtherFixture();
        contactsToRemove.add(contact);
        Body otherBody = otherFixture.getBody();
        if (otherBody == null)
            continue;
        Entity otherEntity = (Entity) otherBody.getUserData();
        if (otherEntity == null)
            continue;
        // entitiesInContact.add(otherEntity);
        // removes the contact from the other entity.
        PhysicsComponent otherPhyiscsComponent = Components.getPhysicsComponent(otherEntity);
        otherPhyiscsComponent.getContact().removeContact(otherFixture, myFixture);
    }
    for (int i = 0; i < contactsToRemove.size; i++) {
        Contact contact = contactsToRemove.get(i);
        contacts.removeContact(contact.getMyFixture(), contact.getOtherFixture());
    }
    contactsToRemove.clear();
}
Also used : Entity(com.artemis.Entity) PhysicsComponent(com.gemserk.commons.artemis.components.PhysicsComponent) Fixture(com.badlogic.gdx.physics.box2d.Fixture) Body(com.badlogic.gdx.physics.box2d.Body) Contact(com.gemserk.commons.gdx.box2d.Contacts.Contact)

Example 3 with Contact

use of com.gemserk.commons.gdx.box2d.Contacts.Contact in project commons-gdx by gemserk.

the class ContactsTest method addingAContactReusesOneIfAvailable.

@Test
public void addingAContactReusesOneIfAvailable() {
    mockery.checking(new Expectations() {

        {
            ignoring(fixtureA);
            ignoring(fixtureB);
        }
    });
    Contact reusedContact = new Contact();
    contacts.contacts.add(reusedContact);
    contacts.activeContacts = 0;
    contacts.addContact(fixtureA, fixtureB, new Vector2());
    assertTrue(contacts.isInContact());
    assertThat(1, IsEqual.equalTo(contacts.getContactCount()));
    Contact contact = contacts.contacts.get(0);
    assertThat(true, IsEqual.equalTo(contact.inContact));
    assertThat(contact.myFixture, same(fixtureA));
    assertThat(contact.otherFixture, same(fixtureB));
}
Also used : Expectations(org.jmock.Expectations) Vector2(com.badlogic.gdx.math.Vector2) Contact(com.gemserk.commons.gdx.box2d.Contacts.Contact) Test(org.junit.Test)

Example 4 with Contact

use of com.gemserk.commons.gdx.box2d.Contacts.Contact in project commons-gdx by gemserk.

the class ContactsTest method removesFirstContactIfItMatches.

@Test
public void removesFirstContactIfItMatches() {
    mockery.checking(new Expectations() {

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

Example 5 with Contact

use of com.gemserk.commons.gdx.box2d.Contacts.Contact in project commons-gdx by gemserk.

the class ContactsTest method whenAddingOneContactABItIsAdded.

@Test
public void whenAddingOneContactABItIsAdded() {
    mockery.checking(new Expectations() {

        {
            oneOf(box2dcontact).getWorldManifold();
            will(returnValue(worldManifold));
            oneOf(worldManifold).getNormal();
            will(returnValue(normal));
            oneOf(box2dcontact).getFixtureA();
            will(returnValue(fixtureA));
            oneOf(box2dcontact).getFixtureB();
            will(returnValue(fixtureB));
            ignoring(fixtureA);
            ignoring(fixtureB);
        }
    });
    contacts.addContact(box2dcontact, true);
    assertTrue(contacts.isInContact());
    assertThat(1, IsEqual.equalTo(contacts.getContactCount()));
    Contact contact = contacts.getContact(0);
    assertThat(contact.getNormal().dst(0, 1), OrderingComparisons.lessThan(0.1f));
    assertSame(fixtureA, contact.getMyFixture());
    assertSame(fixtureB, contact.getOtherFixture());
}
Also used : Expectations(org.jmock.Expectations) Contact(com.gemserk.commons.gdx.box2d.Contacts.Contact) Test(org.junit.Test)

Aggregations

Contact (com.gemserk.commons.gdx.box2d.Contacts.Contact)7 Expectations (org.jmock.Expectations)6 Test (org.junit.Test)6 Vector2 (com.badlogic.gdx.math.Vector2)3 Fixture (com.badlogic.gdx.physics.box2d.Fixture)3 Entity (com.artemis.Entity)1 Body (com.badlogic.gdx.physics.box2d.Body)1 PhysicsComponent (com.gemserk.commons.artemis.components.PhysicsComponent)1