42 videos
238 minutes
We will add support for jumps. For this Box2D offers the possibility of creating two types of forces in our bodies. Some are real forces and others are impulses. For example, I create a method called jump, which will be then going to use at certain times, for example by touching the screen or cause a collision to detect whether to continue jumping because the screen is still down,
which will occupy calling a method minijoeBody: apply ... there are many things. I commented that there is a difference between force and momentum. The strength is a variation of the speed is progressively over time, for example to change the speed as time passes. While impulses change the speed bump. In that case as going to force that drives more interest, but in this case to make the jump
I'm interested in the speed change shock to the player jump and do not eat what is before, so I'll use Impulse. This is important to distinguish all methods there. Sometimes we have to spend a vector, and sometimes we have to go floating components. This is so that if we have a table ready and we move on now, but if we do not move on the components, and that Box2D what you want with this is
prevent vectors have to instantiate some methods that is not appropriate, for example in the render or update, where instantiate something 60 times per second will make the Java garbage collector basically pull smoke. So simply pass the components as a float will suffice. We see a pattern that always repeated: there is always a parameter called force, impulse, forceX, Forcey and always one called Point.
Thus besides telling the magnitude of the force of momentum that we apply, we can tell you where you want to apply, so that we can ask Box2D to implement it on an edge of the object or the center, for which we also have some special methods as toCenter, to apply to the center. We also have a parameter called wake, with which we may ask you wake up or reactivate an object that has previously in sleep mode,
energy saving mode sounds a bit wrong. To not consume CPU cycles, I told you so. If you are asleep, as it somehow, it 'wakes up' to apply that force, because if not it can not apply either. In my case I will apply a linear momentum, and as I have no vector I'll do floating parameters. I'll do horizontally with value 0 for not move horizontally, but vertically want a force of 20 N,
to move it upwards. As for position, center. But how I only know him from the center I'm going to ask. For this Body has a parameter, getPosition (), which gives us the position. And I keep it in a Vector2. It is important to distinguish that this Vector2 is not instantiated, this is already instantiated so it is safe. All that will be saved here is the reference. What we must not do is basically new. new is bad, what makes each time
end the method the garbage collector have more and more things to do. I pass the components x, y as parameters in Point, and finally I tell him awake. And with that I have defined the method jump. It will be nicer then but this works, and to prove it, we'll do that every time the player touches the screen with your finger, jump. For example, Gdx.input.justTouched (), if true, we jump. This is getting ugly because it is here.
Theirs would have it in different objects. For example, EntityBody, EntitySuelo; but it is all variables have here, and we will clean later. Now, when you press, the player will jump. And it works well. If I run it, you see, the body falls, but if you touch the screen, the body jumps. And jumps, in fact, at a pace that still need to be calibrated when the player to move to that we ensure that we do not eat what we have ahead to make the jump but it is progress.
While it is true that behaves somewhat erratic, because I can do right now to jump all the time, and I avoid this to happen. To avoid a better way is to make sure that I can detect when you hit the ground to make only then will check whether to jump or not and so the player does not have freedom to constantly jump. Let's see how we can implement collisions.
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.