# Jumping?

This topic is 4974 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey I'm working on a single player game and I'm totally newbie at OpenGL. Could anyone tell me how to make the player jump?

##### Share on other sites
jump in what 3d or 2d... its just increasing the y value over time and decrease it when it reaches its peak, you might want to make an equation to calculate how far you will want to jump, to decrease the y increase since you slow down completely before you fall back down

##### Share on other sites
Well, I already tried this, but I don't know how to make the character get down again after some secounds (now you can just hold down space and fly trough the map) I need some timing function, already tried Sleep() but seems not working on OpenGL >.<

if (keys[VK_SPACE])
{
p1y += 2.0f;
}
if(p1y >= 2.0f)
{
p1y = 0.0f;
}

##### Share on other sites
A way of providing jumping is to use velocity and simple gravity. Suppose you have a position for your character and you also have a velocity component as well. Every time you update the character increase its vertical position by the velocity multiplied by the elapsed time since the last update, for example:
gravity = -9.81f;velocity   += gravity * elapsed_time_since_last_frame;position.y += velocity;if (velocity < 0.0f)  velocity = 0.0f;

When you want to jump, just set the velocity to a positive value:
if (keys[VK_SPACE])  if (canjump)  {    velocity = 30.0f;    canjump  = false;  }

This will provide an instantaneous jump and a nice smooth deceleration.

For a timing function, look into timeGetTime() or QueryPerformanceFrequency()/QueryPerformanceCounter(), assuming you're on a Windows system that is.

Hope some of that helped.

##### Share on other sites
How I declare the gravity thingy? GLfloat gravity = 0.0f;?
And what command I use?
Make_gravity(gravity)? Please explain better :) I'm a noob yea

##### Share on other sites
I think you're confusing some things. OpenGL is NOT a GameEngine so it won't make your character jump for you. It can pretty much do anything if you want to display a jump, but you have to do the logic behind it yourself.

##### Share on other sites
I don't wanna sound cruel, but if you do not know how to create a float variable, you really should consider reading some books on general programming before you start working with games, graphics, and the like.

##### Share on other sites
Haha, I know that, there is still some stuff that works like a "GameEngine"
This exists:
GLfloat rotatex = 0.0f;
glRotatef(rotatex, 0.0f, 0.0f);
So why not this?:
GLfloat g = 0.0f;
glGravity(g);

Anyway, couldn't some nice people help me with a jumping code cause I'm in need of it

##### Share on other sites
I seriously suggest you read some tutorials about general programming and then about opengl to get an image what opengl even is. GL = graphic library. It has no idea about physics or collision detection and response.

##### Share on other sites
Dude, if you aint posting a jumping code, then don't post here.
@Darkwing: HA! I've learned C++ in 3 years and OpenGL in 1 year. Don't treat me like a 100% núúb cause I'm only 20% núúb :)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 31
• 16
• 11
• 10
• ### Forum Statistics

• Total Topics
634118
• Total Posts
3015607
×