Jumping?

Started by
15 comments, last by Noxir 18 years, 10 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?
Advertisement
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
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;
}

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

This topic is closed to new replies.

Advertisement