Sign in to follow this  
Noxir

Jumping?

Recommended Posts

Noxir    100
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Noxir    100
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 this post


Link to post
Share on other sites
eSCHEn    394
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 this post


Link to post
Share on other sites
Noxir    100
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 this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
pammatt    211
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 this post


Link to post
Share on other sites
Noxir    100
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 this post


Link to post
Share on other sites
_DarkWIng_    602
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 this post


Link to post
Share on other sites
Noxir    100
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 this post


Link to post
Share on other sites
Ging    181
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 this post


Link to post
Share on other sites
SnAkE    122
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 this post


Link to post
Share on other sites
Noxir    100
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 this post


Link to post
Share on other sites
BornToCode    1185
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 this post


Link to post
Share on other sites
Noxir    100
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;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this