Game Development Reference
In-Depth Information
trace("first body: "+bodyA.GetUserData());
trace("second body: "+bodyB.GetUserData());
trace("---------------------------");
}
}
}
The purpose of the customContact class is to override Box2D functions called
BeginContact and EndContact , which by default don't do anything, and get some
information about the collision.
Trace the beginning and the end of a
collision
As the names suggest, the Begin Contact Event is called when two fixtures begin to
overlap, while the End Contact Event is called when two fixtures cease to overlap.
1.
Let's analyze the BeginContact function line by line:
var fixtureA:b2Fixture=contact.GetFixtureA();
var fixtureB:b2Fixture=contact.GetFixtureB();
As you can see, BeginContact has a b2Contact object passed as an
argument. It contains all collision information we are looking for at the
moment. GetFixtureA and GetFixtureB methods return the fixtures
involved in the collision. I am saving them in fixtureA and fixtureB
variables.
2. Then, as I am looking for bodies, I need to get the body from a fixture.
You should already know about the GetBody method:
var bodyA:b2Body=fixtureA.GetBody();
var bodyB:b2Body=fixtureB.GetBody();
3.
Now bodyA and bodyB are the colliding bodies. Time to print some text
in the output window:
trace("first body: "+bodyA.GetUserData());
trace("second body: "+bodyB.GetUserData());
I am printing their user data, which can be Floor or Ball .
The EndContact function has the same code, so there's no need to explain it.
 
Search WWH ::




Custom Search