# Jumping?

## 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 :)

##### Share on other sites
There is no glGravity() function, if you've been learning OpenGL for a year than you should know enough about it to recognise that it doesn't provide that functionality.

eSCHEn has provided a perfectly valid method of jumping, why don't you take it away and see if you can't figure out how to make it work? If you have the experience you say you have, it shouldn't take long to figure it out.

##### Share on other sites
you got to implement two variables:
player_pos_y and player_pos_y_change

so there is a variable that holds the amount of height-units you want to change the players y position every timeframe.
if the player presses the space-key, change it to lets say 20.
now add this variable every timeframe to the player_pos_y and decrease the change-variable by the gravity-value, lets say 5.
so the player moves up very fast at first, stops after a while and begins to move down immedeatly.
do this as long as the player is not on the ground, and you're done!
This is the easiest way to implement the jumping I know of.

hope I did not miss the most important thing ;-)

##### Share on other sites
Ok, now I just need to know the "if(space_was_released)" command, could anyone please help I just need this command to make it work

( this is how I'll do it )

if (keys[VK_SPACE])
{
if (max != 1)
{
p1y += 0.2f;
}
}

if (keys[VK_WAS_RELEASED?])
{
max = 1;
}

##### Share on other sites
nvm i found it!! thanks all for help ^^

if(!keys[VK_ESCAPE])
{
max = 1;
}
:D:D:D:D::D:D:D:D:D:D::D:D

##### Share on other sites
Windows sends both WM_KEYDOWN and WM_KEYUP commands... I suggest you use that. Escape isn't really meant to quit jumping...

##### Share on other sites
for real man you need to read a book man. it seem to me that you have no idea what you supposed to do. But i will be generous and give you a function that will do it for you here it is.

void PlayerJump(float delta)
{
/*We will assume we have a variable called
Gravity,Player_Y,Velocity,FallingLimit*/
Gravity=-9.81f //Gravity is Contant
Velocity+=Gravity*delta;
Player_Y+=Velocity;
if(Player_Y>FallingLimit)Player_Y=FallingLimit-1;
}

//Hope that gives and Idea on How you need to Implemented
if you do not know how to declare those Variable then you are not supposed to be writting games. By the way GLFloat is the same as float

##### Share on other sites
oops i wrote wrong, i ment VK_SPACE, not VK_ESCAPE ;) thanks anyway, ive already made it all myself :D

if (keys[VK_SPACE])
{
if(max != 1)
{
p1y += 0.2f;
}
}
if (!keys[VK_SPACE])
{
max = 1;
}
if (p1y >= 4.0f)
{
max = 1;
p1y -= 0.1f;
}

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628334
• Total Posts
2982147

• 9
• 24
• 9
• 9
• 13