Public Group

# jump algorithm

## Recommended Posts

9 hours ago, phil67rpg said:

well joe I implemented your code but it still does not jump.

You would need to set initial velocity to go upwards. My example just shows free fall behaviour.

So set some positive value here:

float v = 0; // velocity

But the example was just to show the math, and the difference between closed form / integration.

Like others i think your issue is to understand how a interactive realtime game (or simulation) has to work. Same issue i had on C64.

In your code you do again a loop to calculate movement. But you do not want such a loop, because it misses the option of user interaction, and also the option anything unexpected could happen for other reasons, e.g. collisions. It also misses the option to sync to real time. (EDIT: So you can not render the sprite at every position it takes while performing the jump! Think of this!)

So you want to use the math stuff from shown loop as a single step function, together with other things like user input, sync to framerate, collision detection, etc. All this should be done once, one after another (order does not matter much), and you want to constantly repeat that process for each frame. (Which we call the game loop then - the outer loop of the whole program.)

As this requires some OS functions it's not possible to make a simple code snippet, but i try to make a simple example anyways:

	Main ()
{
int x = 0; int y = 0; // our game character positions
float realtime = OS.GetTime(); // lets assume OS would give us time as seconds, which it does not
float gametime = realtime;
float timestep = 1/60;
while (true) // game loop
{
/// sync to realtime (simplified - does not handle the case of game being too slow to catch up)
gametime += timestep;
while (OS.GetTime() < gametime)
{
// do nothing, so wait - not the best way to do that
}

/// process one step of physics (if this would be more advanced, we would use timestep for integration math):
if (OS.keyUP) y -= 1;
if (OS.keyDOWN) y += 1;
// same for x, eventually process collisions, AI, whatever else.

/// draw stuff
DrawSprite(x,y);
}
}


So that's the basic stuff you need to make work first. Only after that you should worry about realistic jump trajectories and such.

Edited by JoeJ

##### Share on other sites

While i totally agree with you, @JoeJ, i think this main loop style is better suited for an event processing library like glfw for example. I think, pls. correct me because i have never done this myself, that with glut you set up everything, the callbacks needed, configure the pipeline, and then call glutMainLoop(), leaving the control to that function.

Maybe somebody with more experience in the old way can clarify if i am wrong with this ...

Edit: not that i want to argue in favour of glut, don't get me wrong.

Edited by Green_Baron

##### Share on other sites
8 minutes ago, Green_Baron said:

I think, pls. correct me because i have never d﻿one this myself, that with glut you set up everything, the callbacks needed, configure the pipeline, and then call glutMainLoop(), leaving the control to that function.

Yeah, i have ignored details like the message based system Windows is using for example. And i also assume @phil67rpg uses some kind of framework like glfw himself, making things easier.

What i try to say is mostly why, for a real time game, it can't work to do multiple steps of physics simulation in a closed loop (usually). Each step of simulation has to be at least visualized (assuming simualtion rate == frame rate).

So i guess he's confused about this for once, and maybe also about the additional complexity coming from the need to have a realistic jump motion. If you make PacMan, you don't need to think about timesteps because velocity is always constant and there is no acceleration at all. If (KeyUp) x++ is enough. But with a more realistic game like Super Mario some simple form of physics simulation becomes necessary, using timestps, velocity and acceleration. Likely not super obvious to get all this right at once, if it's all new things.

##### Share on other sites

Yes, @phil67rpg, drop glut and do use glfw3, which contains a nice step by step tutorial on how to set up a main loop and the callbacks exactly in the way @JoeJsuggested.

This tutorial also has examples of how to include shader code, which caused confusion in an earlier thread. With the knowledge from the glfw tutorial you will be ready to dive deeper into things, including learnopengl.com and the super bible.

Just ask if you are confused now !

Edited by Green_Baron

##### Share on other sites

well  thanks  for all the help

##### Share on other sites
This topic is now closed to further replies.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 23
• 38
• 16
• 75