Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding a collision contact listener #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 72 additions & 1 deletion src/com/uwsoft/editor/renderer/systems/PhysicsSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import com.uwsoft.editor.renderer.physics.PhysicsBodyLoader;
import com.uwsoft.editor.renderer.utils.ComponentRetriever;
import com.uwsoft.editor.renderer.utils.TransformMathUtils;
import com.uwsoft.editor.renderer.physics.Contacts;
import com.uwsoft.editor.renderer.scripts.IScript;

public class PhysicsSystem extends IteratingSystem {
public class PhysicsSystem extends IteratingSystem implements ContactListener {

protected ComponentMapper<TransformComponent> transformComponentMapper = ComponentMapper.getFor(TransformComponent.class);

Expand All @@ -29,6 +31,7 @@ public class PhysicsSystem extends IteratingSystem {
public PhysicsSystem(World world) {
super(Family.all(PhysicsBodyComponent.class).get());
this.world = world;
world.setContactListener(this);
}

@Override
Expand Down Expand Up @@ -98,5 +101,73 @@ private void doPhysicsStep(float deltaTime) {
public void setPhysicsOn(boolean isPhysicsOn) {
this.isPhysicsOn = isPhysicsOn;
}
@Override
public void beginContact(Contact contact) {
collision_contact(contact, true);

}

@Override
public void endContact(Contact contact) {
collision_contact(contact, false);
}

private void collision_contact(Contact contact, boolean in) {
// Get both fixtures
Fixture f1 = contact.getFixtureA();
Fixture f2 = contact.getFixtureB();
// Get both bodies
Body b1 = f1.getBody();
Body b2 = f2.getBody();

// Get our objects that reference these bodies
Object o1 = b1.getUserData();
Object o2 = b2.getUserData();

// cast to entity
Entity et1 = (Entity) o1;
Entity et2 = (Entity) o2;
// get script comp
ScriptComponent ic1 = ComponentRetriever.get(et1, ScriptComponent.class);
ScriptComponent ic2 = ComponentRetriever.get(et2, ScriptComponent.class);

// cast script to contacts, if scripts implement contacts
for (IScript sc : ic1.scripts) {
try {
Contacts ct = (Contacts) sc;
if (in)
ct.beginContact(et2);
else
ct.endContact(et2);
} catch (ClassCastException exc) {

}

}
for (IScript sc : ic2.scripts) {
try {
Contacts ct = (Contacts) sc;
if (in)
ct.beginContact(et1);
else
ct.endContact(et1);
} catch (ClassCastException exc) {

}

}
}

@Override
public void preSolve(Contact contact, Manifold oldManifold) {
// TODO Auto-generated method stub

}

@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
// TODO Auto-generated method stub

}

}