How to jump a game character realistically without using physics engine in corona sdk

Movement of free fall

Jumping is one of the main movements that you can’t think of a game character without it. Although it is very easy to write a basic jumping code sometimes you want your characters to jump more realistically. In this post i will write how you can do this without adding physics. If you need physics engine just for jumping then this post is absolutely for you.

As you know in a game world jumping is all about changing your characters position in y axis. To make a basic jump  all you need are  initial jump speed, initial position, maximum altitude values and a variable that keeps distance between initial position and current position.

Below there is a sample code for basic jump.

sample code for basic jump

Let’s take a look at sample code line by line. Between lines 1 and 9, first i localized content width and height by defining local variables W and H. Then i created a vector display object circle and named it “myCircle”.

“myCircle” has four variable. “groundLevel” is the variable that keeps the position of “myCircle” before it jumps. You know your character can be in different y positions and you should consider this position in your calculations. I used “groundLevel” but don’t say this is the best way to do it. You may use many different ways, just try and find your best solution.

“jumpSpeed” is the value to set how fast “myCircle” goes up.

“deltaY” keeps the distance between “groundLevel” and “myCircle”‘s y position and finally i defined “maxAltitude” variable in order to prevent “myCircle” from going to space.

At line 29 i added a tap event to the Runtime. When i tap screen, startJump function will be triggered and what it basically do is adding another event to Runtime but this time our event will be “enterframe” event and it will be responsible for moving “myCircle” .

Between lines 11 and 24, i defined moveMyCircle function. It is called in every frame.

Line 12 : Subtract jumpSpeed from the y position to make myCircle move upward

Line 13 : Add jumpSpeed to deltaY to keep how much distance myCircle took from the groundLevel

Line 15,16 : Check if deltaY is greater then maxAltitude. That means myCircle has reached its maximum altitude or not. If it has  then make jumpSpeed negative to make myCircle move downward

Line 19, 20 : Check if y  position is greater than groundLevel which means myCircle has landed or not. If it has then remove enterFrame event and make jumpSpeed positive again for the next jump.

You can see the result below.
Basic jump gif

Ok this looks like jumping but as you can see it is not realistic enough.

The reason it does not look realistic enough is because i used constant speed during the movement phase.
In real life you can’t have constant speed while you are jumping. When you go up your speed decrease and at some point it equals to zero. Then you start to go down and your speed increase again. What cause the change of speed is gravitational acceleration.It is the acceleration on an object caused by force of gravitation.

If we can add this gravitational acceleration in our calculations then we will have more realistic motion.
Gravitational acceleration effects the speed directly and we should consider this while writing our new code.

Below you can see the modified code lines
Codes lines for realistic jump

This time i have added a new local variable “gravity”. This is my gravitational acceleration in the game.

As you can see there is no more “maxAltitude” and “deltaY” because maximum altitude now will be determined automatically by the values of “gravity” and “jumpSpeed”.

Between lines 9 and 17 first subtract “gravity” from the “jumpSpeed” to find final speed of “myCircle”  While “myCircle” is going upward “gravity” and “jumpSpeed” has opposite directions (if speed has a directions then it is actually called as velocity but i will not go into physics details now). Since they have opposite directions gravity always decrease speed while an object going up.

At some point “jumpSpeed” equals to zero and after that it has the same direction with “gravity”. So “myCircle” starts to go downward faster and faster.

I have also added line 20 to “startJump” function because in the “moveMyCircle” function “jumpSpeed” is modified by gravity and before every jump i should make “jumpSpeed” equal to it’s initial value again.

And this is how it looks like
realistic jump gif

By changing “gravity” and “jumpSpeed” you can set the maximum  altitude that your game character should have.

I wish i could help you a little. Please don’t hesitate to ask me question in comments.

Advertisements

One thought on “How to jump a game character realistically without using physics engine in corona sdk

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s