Advertisement

Jumping?

Started by June 09, 2005 03:43 AM
15 comments, last by Noxir 19 years, 8 months ago
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?
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;
}

Advertisement
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.
--
Cheers,
Darren Clark
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

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.
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
Advertisement
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.
You should never let your fears become the boundaries of your dreams.
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 :)
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.
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 ;-)

This topic is closed to new replies.

Advertisement