#### Archived

This topic is now archived and is closed to further replies.

# my sprites anti grav boots!!! help!!!

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

## Recommended Posts

im trying get the jumping part of my physics engine to work, the only problem is, when i set the sprite to jump (sets its y velocity to something like 10, or any number for that matter) the sprite instantly shoots to the top of the screen and never comes down. here is my code, whats the matter with it?

void cPhysics::ApplyPhysics(float &Xpos, float &Ypos, DWORD TimeDelta)
{

//if jumping

if(jumping)
{
//Terminal velocity

if(vVelocity > -100)
{
//Applies Gravity

vVelocity = vVelocity + (int)GRAVITY;

//Moves object

//got this equation somewhere on this form, what the hell does it mean (i had this problem before i used this equation, too)

Ypos = Ypos + TimeDelta * vVelocity + TimeDelta * TimeDelta * GRAVITY / 2;
}

//just used for now because there is no floor

if(Ypos > 400)
jumping = false;
}
}

upon starting the jump, the y velocity is set at 10. GRAVITY is set at -10. so it SHOULD be a very small jump. i call this function every frame, but i think its time independent, a little help? ---------------------- i code therefore i am. Aero DX - Coming to a bored Monitor near you! [edited by - Neen10do on May 4, 2003 10:48:18 PM]

##### Share on other sites
I think that is your problem: your code doesn''t depend on time. Where you have "If Jumping{}" you should change that to "If Jumped{}" or something along those lines. What I mean is, make a variable that is normally false, set it to true when jump is pressed, and then right at the beginning of your jump block of code, set it to false again. Something along those lines.

##### Share on other sites
the if(jumping) line is a boolean expression that is used to apply vertical velocity only when the player is jumping (for now anyway). i think my problem lies in the math. i dont take physics until next year so i dont know if im doing it wrong or not. anyone?

----------------------
i code therefore i am.

Aero DX - Coming to a bored Monitor near you!

##### Share on other sites
What do you mean by "when the player is jumping?" If you are increasing the velocity continuosly while the player is in the air, there''s no way to let him go down. You need to increment the velocity once, then let the gravity acceleration pull him down.

##### Share on other sites
the if(jumping) is to make sure these equations are only applied if the player has pressed the jump button, at some point in time. without it there, it will continously make him fall faster and faster. GRAVITY is a negitive constant, -9.8, so its actually subtracting from the velocity. ANYONE WHO KNOWS THE ANSWER?

----------------------
i code therefore i am.

Aero DX - Coming to a bored Monitor near you!

##### Share on other sites
a couple of suggestions:

are you absolutely sure it doesn't come back down the screen without you seeing it? (the difference of position could be enough to completely miss the screen) Consider for now adding a line in your "if(Ypos > 400)" statement that in addition to jumping = false, Ypos=400, that way if he's no longer jumping you can visually verify this.

The equation you used to update the Ypos is an equation you would use for a given initial velocity (not current velocity) and a change in time from the start of the motion (not each individual frame).

Let me suggest to you the following: update the velocity and position as follows

vVelocity = vVelocity + GRAVITY * Time Delta; // <-- I don't know why you had (int) here either, when it's -9.8
Ypos = Ypos + vVelocity * TimeDelta;

Also, only your velocity should be updated within your "terminal velocity" if statement, because the way you have it set up now, you'll only move the guy when he's not reached terminal velocity, which isn't good, because he should move, but with constant velocity. Perhaps that was also a problem because he was offscreen when terminal velocity was reached?

and assuming everything else in your code is fine, I think this might help

Elijah

[edited by - etaylor27 on May 6, 2003 1:29:03 PM]

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 12
• 15
• 9
• 14
• 10
• ### Forum Statistics

• Total Topics
632655
• Total Posts
3007675
• ### Who's Online (See full list)

There are no registered users currently online

×