42 videos
238 minutes
To implement these collisions, we Box2D what is known as Contacts. Every time there is a collision or two fixtures somehow collide or try to occupy the same region of space, contact occurs and can be notified of it. For this World brings a method called setContactListener, with which we can associate a ContactListener our world, which is associated listener to hear when those contacts occur.
So I'm just going to make a setContactListener (new ContactListener ()) to define one quick, although this can be done in a better way. As you can see, ContactListener implements several methods. We can use beginContact to detect when we have begun a collision, and endContact to detect when we have finished a collision. preSolve and postSolve run when physical contact being processed and thus can
do physical using special parameters, but in our case we will not use yet. We will take only BeginContact and EndContact. Each time the player somehow clash with soil, it will produce a contact between two Fixtures, which you can access by getFixtureB of the Contact getFixtureA and methods. This is where the problems come because we can not initially know where the Body,
if the fixture A or B fixture, so everything we do here, and all the decisions we make to detect when the body hits the ground, we must consider whether == body A, B == ground, as when A == soil, B == body, so we have to write the same thing twice. It is not as clean as it could be. For example, I can get the contact fixture using contact.getFixtureA A () and B fixture using contact.getFixtureB ().
And now I can detect if my fixture corresponds to the Body or not. And this is where the problems come because ... how do I do? Initially I only know the Fixture itself. What we have to do is determine, for example, if fixtureA is the same variable that minijoeFixture and fixtureB the same as sueloFixture, although I will show how we can do this in other ways by UserDatas. For now, if the Fixture A
It is to Minijoe, and Fixture B is the soil, can ask our body to jump. You might think it's easy, calling jump. I also thought it would be so, but it does not. You can not make a jump start within a ContactListener, because it jumps require that the engine is not working, and during a contact Box2D will be working on things like this it will not work. Instead, what we do is ask at one point Box2d
which is free, jump. And this translates to remember in our game to be a jump start. And this means that basically I create a Boolean variable that has the value haColisionado. I can here when a collision occurs true worth. And as I have said before, because I need both my fixture A and B fixture is associated with a body or another, I have to do this twice. For example, if the soil is fixtureA
instead of being the Joe, I must do the same. HaColisionado set to true. And once we are out of our manager collisions I can already make, if we played or has collided, we jump, and then also to be put haColisionado to false to avoid that for some reason can jump several times. Thus, now every time a collision occurs, will jump again. As I say, you can not make jumps within the same manager collisions,
because you do when you are working. So no choice but to put a flag to do so later in the case for example of haColisionado. This is often done with entities, as it could be a serious entity as BodyEntity or PlayerEntity, with properties like estaSaltando, debeSaltar, enElAire, to be more flexible to control. In my case like this it is so convenient to access all variables still am not doing,
but rest assured that when Scene2D incorporate this screen, we will move to make it cleaner, obviously.
About the Author
Biography: Analyst-Programmer in different languages. Tutor PREMIUM platforms recognized worldwide as CodigoFacilito. Editor Cristalab articles. My YouTube Channel is sponsored by the ANAYA and LaTostadora publisher. I like to explain in detail and give several examples for the avoidance of doubt.