Sign in to follow this  
Jacob Roman

Jumping Physics - Fighting Game

Recommended Posts

I'm creating a fighting game that's gonna be Mortal Kombat vs Street Fighter, with graphics I ripped of from various arcade fighting games using Nebula & MAME. Anyways, I want the jumping physics to be based on a characters weight,gravity that's based on Earth gravity (9.8 is earth gravity) as well as the force that has been done to launch a person from the ground. That way there, the heavy people in the game don't jump as high, like in the REAL world, unless they have the force to do so. I need the maths for actions such as jumping straight up, and jumping forward/backward. And if you can, not only provide a formula, but a code example as well. I went to sites like www.physicsclassroom.com where they provided a 100 different formulas, but I got confused on what formula to use and how I was gonna use them, which is why a code example would be better. I tried everything I could but failed. Please help me. Thanks. [Edited by - Jacob Roman on September 22, 2004 5:01:30 AM]

Share this post


Link to post
Share on other sites
The formula is missing gravity. And a code example would help too. ;) Also I don't think that'll work cause if you have a person that's gonna jump straight up (Y), if you keep multiplying twice to that value, you end up going up, Up, UP, etc...unless somewhere in there is gonna be a negative number. The fighter has to go up, and eventually he'll slow down going up, and come back down. Sine and Cosine would help do this, but I'm having a hard time working with it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Jacob Roman
The formula is missing gravity. And a code example would help too. ;) Also I don't think that'll work cause if you have a person that's gonna jump straight up (Y), if you keep multiplying twice to that value, you end up going up, Up, UP, etc...unless somewhere in there is gonna be a negative number. The fighter has to go up, and eventually he'll slow down going up, and come back down. Sine and Cosine would help do this, but I'm having a hard time working with it.


not quite...

f = m*a is certainly very important here in its different forms. (f being gravity here)

f*dt = m*dv is also important.

basicly it goes like this:
when a character initiates a jump, he exerts a certain force on the ground for e certain amount of time, giving him an impulse f*t. as can be seen from the above formula, you can divide this by his weight to obtain the speed with which your character takes off.

at this point your charater is flying upwards. now apply f=m*a and f=m*-g (thus simply a=-g) to get the acceleration, and use dv = a*dt to obtain the change in speed. then update the position accoring to speed. if you do this correctly, the character will follow a parabolic path, first going up then going down.

Share this post


Link to post
Share on other sites
Thanks Eelco for a better description of the formula. When I get home from work & college, I give it a whirl in a test program. If I have problems, I'll let ya know ;). Oh by the way, I know (f = force), (m = mass), (a = acceleration) and (g = gravity), (but dv and dt?) Isn't that velocity and time? If it is, how can I use it for the sprites X and Y coordinates?

Share this post


Link to post
Share on other sites
Since you just want to look at the effect of gravity, you could just integrate the equation and use the following:

y - y0 = v0*t -1/2*g*t^2

where y0 and v0 are the initial position and speed at time t=0, and y is the position of your character at time t.
g is the acceleration due to gravity (~9.8 m/s^2).

Hope this helps.


Share this post


Link to post
Share on other sites
d/dt = derivative

The equation can be re-written as f = m * (dv/dt), which means that force is equal to mass times the derivative of the velocity with respect to time. The derivative of position is velocity, and the derivative of velocity is acceleration, hence:

f = ma = m * (dv/dt).

As for the rest, sorry, I can't help.

Edit: Fixed some of the terminology.

[Edited by - Del Snd of Thndr on September 22, 2004 5:31:43 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Still having a little trouble after using all those formulas. Can someone please show me a code example. Either in Visual Basic or C++, so I can understand how to implement it?

Share this post


Link to post
Share on other sites
This is what I have:

Private Declare Sub Sleep Lib "kernel32" (ByVal Milliseconds As Long)

Dim Y As Single

Private Sub Form_Activate()

DrawWidth = 10

Y = 450

Dim F As Single, M As Single, V As Single, T As Single

M = 10 / 2.2 'lbs to mass (kg)

V = -9.8

T = 0

Do

DoEvents

Cls

T = T + 1

F = M * (V / T)

'Print f

Y = Y + F

'Print Y

PSet (100, Y)

Sleep 16

If Y <= 0 Then Y = 0

If Y >= 450 Then

Y = 450

End If

Loop

End Sub



And the result is that it went up and slowed down, but continued to go up as it slowed down, and never came down. And the mass was messed up to. If it were heavier, it went up further as if it were lighter. Same goes with the gravity. Any other suggestions?

Share this post


Link to post
Share on other sites
Okay, firstly, dv/dt is not delta. It is a derivative.

As for your code, well, I didn't read all of it, but here is what I recommend something like this:


int grav = -9.8;
int time = 0;
int y_velocity = 20; //some random velocity...character defined
int y_initial = 0;
int y_final;

...

//on each frame
if(y_final > 0){
y_final = y_initial + y_velocity*time + 1/2*grav*time^2;
//pset(100, y_final);
time++;
if(y_final < 0) y_final = 0; //clamp to 0...or the floor
}





If you do this by hand, you realize that eventually y_final starts becoming negative, because 1/2*grav*time^2 will grow faster than y_velocity*time. However, if you set y_velocity initially high enough, the player will jump for a while.

So for different characters, just set their initial y_velocity to different levels to achieve different heights in their jump.

What you really want to do is give the player a struct that contains their y data, the current time offset, etcetera. Then, on every game frame, check if they are in the air (like player.state == PLAYER_IS_JUMPING). If they are, simply update their position with this formula.


What you were trying to do is mix up 2d kinematics with force. Which, you can do, but you were mixing up.

[Edited by - visage on September 26, 2004 9:31:54 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
if your jumping back or forward, all your doing is using the same formula for jumping straight up exept that the character is moving with a constant x speed until touching the ground or running into another player.

Share this post


Link to post
Share on other sites
Good call AP. When you get the jump key, poll to see if the player is pressing left or right. Then, in my if loop, use the same formula I supplied except for x.

Now, this will allow you to set some characters to jump higher and further faster. Also, you could use some sort of wind variable. Set it to 0 when there is no wind, and if there is wind (assuming X only...I really dont feel like getting into forces using grav and wind in the y...), just put it in the spot of 'grav' in the y formula. So, something like this:


int wind = 0;
int grav = -9.8;
int time = 0;
int x_velocity = 1;
int y_velocity = 20; //some random velocity...character defined
int x_initial = 0;
int y_initial = 0;
int x_final;
int y_final;

...

//on each frame
if(y_final > 0){
x_final = x_initial + x_velocity*time + 1/2*wind*time^2;
y_final = y_initial + y_velocity*time + 1/2*grav*time^2;

//pset(x_final, y_final);

time++;
if(y_final < 0) y_final = 0; //clamp to 0...or the floor
}

...
//here you could change wind...like wind = -5 would make the player jump slower to the right, and even possible go left!




Hope that helps

Share this post


Link to post
Share on other sites
Actually it's float Grav = -9.8 cause otherwise C++ or VB will round it off. I'll give it a whirl now... Ok it works, except I had to add this:

Private Declare Sub Sleep Lib "Kernel32" (ByVal Milliseconds As Long)

Private Sub Form_Activate()

'In VB, by default, Y+ goes down and Y- goes up from
'top-left corner.

Dim Gravity As Single
Dim Time As Single
Dim Y_Velocity As Single
Dim Y_Initial As Single
Dim Y_Final As Single

Dim Y As Long

DrawWidth = 10
ScaleMode = 3
AutoRedraw = True

Gravity = -9.8
Time = 0
Y_Velocity = 40
Y_Initial = 0
Y = 400

Do

DoEvents

Cls

Sleep 100

Y_Final = Y_Initial + (Y_Velocity * Time) + (0.5 * Gravity * (Time ^ 2))
Time = Time + 1

Y = Y - Y_Final

If Y <= 0 Then Y = 0

If Y >= 400 Then Y = 400

If Y_Final < 0 Then Y_Final = 0

PSet (100, Y)

Loop

End Sub

But mass isnt involved here. Do I multiply mass by the Y_Final hence the formula F = M * A, or do I multiply mass by gravity since gravity is the acceleration?

[Edited by - Jacob Roman on September 27, 2004 7:46:06 AM]

Share this post


Link to post
Share on other sites
Whoh wait a minute... this formula works

Y_Final = Y_Initial + (Y_Velocity * Time) + (0.5 * Mass * Gravity * (Time ^ 2)

Notice Mass is in there. Plus since mass is based on kg, all I would have to do to convert pounds (lbs) to mass (kg) is divide lbs/2.2 to convert it to kg's. And by changing the Velocity, I can create more force. Also by making it heavier, the person doesn't jump as high, unless more force(velocity) is done. Thanks for the formula. If there is a better way, let me know ;D !!!

Share this post


Link to post
Share on other sites
Oh and by the way, the jump is a bit too fast, especially at 60 FPS. Is there anyway I can control the speed of the movement? Like for example, instead of this:

Y
-----
0
10
20
40
80
160
320 'Maximum Y_Final value after a certain time.
160
80
40
20
10
0
which seems instantanious at 60 FPS, I can have the Y move slower:

Y
-----
0
.1
.2
...
10
10.1
10.2
...
320 'Maximum Y_Final value after a certain time.
...
10.2
10.1
10
...
.2
.1
0

That way there my fighters aren't jumping at warp speed.

[Edited by - Jacob Roman on September 27, 2004 8:10:23 AM]

Share this post


Link to post
Share on other sites
Mass should not enter into that equation. The equation is basically the result of integrating Newton's law (F=ma)...and for this case, the mass enters in on both sides of the equation and is cancelled.

Also, be careful about converting from lbs to kg...lbs is a measure of force while kg is a measure of mass. If a person has a mass of 50 kg, they have that mass on Earth, or on the Moon etc... This is not the case for weight...

Cheers,
Mike

Share this post


Link to post
Share on other sites
F = M * A seems right, but the thing is that the result has to be in a parabola like form. It has to slow down as it gets high enough and then come down. This formula has to be integrated with the sprites Y coordinate's as well, which is NOT shown. Not to mention the acceleration is not just gravity obviously. That formula I used was pretty close to what I wanted. Please give me the correct formula that's more than just F = M * A and integrate it with the sprites Y coordinates as well. Maybe to integrate that would probably be Y = Y + F or something like that. Also A (Acceleration) needs to be more than just "A" cause that should be the variable to help create a Parabola since obviously M (Mass) is a constant. I heard from other peoples replies that it maybe Velocity/Time but it didn't work for me. Probably cause I done it wrong or the formula isn't right.

[Edited by - Jacob Roman on September 27, 2004 2:26:41 PM]

Share this post


Link to post
Share on other sites
See for yourself:

Private Declare Sub Sleep Lib "Kernel32" (ByVal Milliseconds As Long)

Private Sub Form_Activate()

Dim Mass As Single
Dim Gravity As Single
Dim Time As Single
Dim Velocity As Single
Dim Force As Single

Dim Y As Long

DrawWidth = 10
ScaleMode = 3
AutoRedraw = True

Mass = 1
Gravity = -9.8
Time = 0
Velocity = 30
Y = 400

Do

DoEvents

Cls

Sleep 16

Time = Time + 1

Force = Mass * (Velocity / Time)

Y = Y - (Force)

If Y <= 0 Then Y = 0

If Y >= 400 Then Y = 400

If Force < 0 Then Force = 0

PSet (100, Y)

Loop

End Sub

It went up but didn't come down.

Share this post


Link to post
Share on other sites
Jacob,
It is quite obvious you have absolutely 0 experience with physics math. Therefore, us trying to explain extrememly complex topics online will be futile. Especially to someone trying to mix F=MA and Newtons laws...
So here is what I recommend.
Buy Physics for Game Developers...or a Physics SAT II study book.
Read about newton's laws and F=MA. After that, ask us some questions.

It sounds harsh, but not only are you wasting your own time, but you are wasting ours. Try to learn first, then ask questions about what you dont understand.

-visage

Share this post


Link to post
Share on other sites
I agree with visage. May be this flash tutorial will help you

http://www.rit.edu/~ndr0470/game_physics/

Also understanding of basic calculus is good too. You need to understand relation b/w Position->Velocity->Acceleration->Force which is easy to do.

Share this post


Link to post
Share on other sites
I agree with visage. May be this flash tutorial will help you

http://www.rit.edu/~ndr0470/game_physics/
You need to nderstand relation b/w Position->Velocity->Acceleration->Force which is easy to do.
Also understanding of basic calculus is good too.

Share this post


Link to post
Share on other sites
Ok heres the scoop. I do NOT have little or no experience in math. In fact, I'm better at math than you think. After programming for so many years (practically since I was 10 years old), working with logic, numbers, and math became no problem. Then 4 years ago, I bought my first 3D programming book, which REALLY challenged my skills. Afterwards, I bought more books based on 3D programming; non DirectX of course, which was harder to learn since DirectX did a lot of the complicated realtime math's and graphics for you. So I obtained a lot of info and experience on 3D geometry, quaternions, calculus, vectors, matrix math, linear algebra, working with normals and backface culling, affine and perspective correct tecture mapping, etc etc etc. I'm now 23 years old, working full time as a production welder, and in my 2nd term at Valencia Community College majoring in Computer Programming and Analysis. I have never taken a Calculus course, nor a Physics course. In fact I've just recently, like a few months ago, started learning physics on my own since I'm working on a game that's gonna need it. So I know some basic theory already. My problem was jumping based on Mass, Gravity, and Force. The formula is right (F = MA) but my problem may not be the math, but my code. You see in VB, positive Y increments downward. In math terms however, positive Y increments upward. So something could be backwards in my code for all I know, which is why it went up but didn't come down. That 2D game physics tutorial didn't help much, since I already knew a lot of that, but the website that it gave to me afterwards helped me even more since it provided detail descriptions and source code on working with F = MA. So I'm gonna work with that. Next term, Valencia is gonna have a Game Design degree program, along with UCF (University of Central Florida) so I may switch my degree. That way I can take courses game physics, AI, 3D programming, game theory, etc etc etc. I can't wait!!! Anyways, thanks for your help. And I apologize if I wasted your time, I just needed help in jumping physics that's all. So don't judge people if you don't know them. Just because someone has a problem in a particular thing in a math subject, doesn't mean they have little or no experience at it, right?

Share this post


Link to post
Share on other sites
You just admitted yourself you have no real class experience with physics math. Thats alright. But you are taking the right steps, analyzing where you think the problem is, and asking the right questions. Fundamentally, however, you are misunderstanding some concepts that would be better cleared up through reading than us trying to explain it to you.

That is all I was trying to say. Good luck, and hopefully if you have any more trouble we can be of greater service.

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