Jumping Physics - Fighting Game

Started by
37 comments, last by VThornheart 19 years, 6 months ago
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
Advertisement
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.
3D Side-Scroller game demo Project-X2 "playable"Lashkar: A 3D Game & Simulation Project demo @ lashkar.berlios.de
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.
3D Side-Scroller game demo Project-X2 "playable"Lashkar: A 3D Game & Simulation Project demo @ lashkar.berlios.de
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?
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.
The cool thing about computer games is that you aren't restricted by the confines of reality. Many games create an environment which seems real; however these games are only *approximating* reality. That being said, I'm going to assume that your game does only needs a convincing approximation of jumping.

Every object that can jump and fall will need one variable to represent it's velocity in the up/down direction, I'll call it vy. When the object is grounded, it's vy will be zero. When your character jumps, you will set his vy to a number that indicates how fast it is moving up at first. This number is up to you and will dictate how high it jumps, also. Now, for each game cycle you will subtract from vy the gravity (9.8) in your case. Then you will add vy to the object's actual y position. After enough repititions, vy will approach zero then become negative. Once vy becomes negative your object will start moving down. Each repetition thereafter will cause your object to move down faster and faster. You will need to have some way of checking when the object has touched the ground. Once he has touched the ground you simply set vy back to zero:

' For the example, I'm omitting everything except what is' absolutely essential to make a convincing jump' Some constants about our environmentPrivate Const Gravity = 9.8' You could change jump velocity for different fighters to' make them better or worse jumpers. You will have to ' experiment to find the best value for thisPrivate Const JumpVelocity = 100Private Type FIGHTER    ' Object's world y position    y As Single    ' Object's velocity in the y direction.    ' The value is the distance travelled in one game cycle    vy As Single    ' Object's height    height As SingleEnd TypePrivate Sub MainLoop()    For Each GameCycle        For Each Fighter            ' Apply jumping pseudo-physics            DoJumping Fighter            ' Now we apply the fighter's vy to his actual            ' y position            Fighter.y = Fighter.y + Fighter.vy        Next Fighter    Next GameCycleEnd Sub Private Sub DoJumping(f As FIGHTER)    ' Check to see if the fighter is on the ground     If FighterIsGrounded( f ) Then        ' Fighter is on the ground, so he can jump        If JumpButtonIsPressed() Then            ' He is jumping, so increase his up velocity            f.vy = JumpVelocity        End If    Else                   ' Fighter is in the air - apply gravity        f.vy = f.vy - Gravity        ' Check to see if the fighter has hit the ground.        ' I don't know how you will check, but for a simple        ' object that fits in a box I would just check to        ' see if the bottom of the box has a lower y        ' coordinate than the surface of the floor.        ' I'm assuming that f.y is the top-left corner         ' of the fighter's box, and that y values increase        ' starting from the top of the screen.        If f.y - f.height <= floor.surface_y Then            ' It has hit the floor. Stop the fall            f.vy = 0                        ' The object probably won't hit the floor exactly            ' so we need to put him exactly on the floor            ' manually.            f.y = floor.surface_y + f.height        End If    End IfEnd Sub


If you were to use the values that I provided for Gravity and JumpVelocity and print vy and y for every game cycle, then you might get something like this:

y: 0          vy: 0         ' not movingy: 0          vy: 100       ' just pressed jump buttony: 100        vy: 90.2      ' first passy: 190.2      vy: 80.39999y: 270.6      vy: 70.59999y: 341.2      vy: 60.79999y: 401.9999   vy: 50.99999y: 452.9999   vy: 41.19999y: 494.1999   vy: 31.39999y: 525.5999   vy: 21.59999y: 547.1999   vy: 11.79999y: 558.9999   vy: 1.999994  ' reached the topy: 560.9999   vy: -7.800006 ' started fallingy: 553.1999   vy: -17.60001y: 535.5999   vy: -27.40001y: 508.1999   vy: -37.2y: 470.9998   vy: -47y: 423.9998   vy: -56.8y: 367.1998   vy: -66.60001y: 300.5998   vy: -76.40001y: 224.1998   vy: -86.20001y: 137.9998   vy: -96.00002y: 41.99979   vy: -105.8' here we've hit the ground. notice that it stopped at zero' instead of the -63.8 that we would have gotten if we' had added vy again.y: 0          vy: -115.6    ' hit the ground.


Looking at these numbers, I'd say that 9.8 really isn't the number you'd want to use. I think having a smaller jump velocity, say 5 and a smaller gravity constant, say 0.5 would give you better results.

[Edited by - smr on September 28, 2004 2:08:39 PM]
I decided to write up some more example code for you. Its a little convoluted, but I am sure you can pick out what you need.

#include <iostream>using namespace std;const double GRAVITY = -9.8;class fighter{    private:        std::string name;        double y_initial;        double y_final;        double y_velocity;        double time;        bool isJumping;        bool firstJump;            public:        fighter(){             name = "";            y_initial = 0;            y_final = 0;            y_velocity = 0;            time = 0;            isJumping = false;            firstJump = false;        }        ~fighter(){}                void jump(){            isJumping = true;            firstJump = true;            time = 1;        }            void Update(){            if(isJumping == true){               if(y_final > 0 || firstJump == true){                  /*                   * This is where you should look                   * This is the equation you probably need                   * NOTICE: I make time frame based                   *         You could make it actually                   *         time based.                   *                   */                  y_final = y_initial + y_velocity*time + (.5)*(GRAVITY)*(time*time);                  time++;                  firstJump = false;               }                 else{                  isJumping = false;                  y_initial = 0;                  y_final = 0;                  time = 0;                  }                }            //do other update stuff here                return;        }           std::string returnName(){                return name;            }            double returnYPos(){                return y_final;            }                          void setName(std::string s){                name = s;                return;            }                     void setYVelocity(double v){                y_velocity = v;                return;        }        };int main(){    fighter Ryu;    fighter Han;        Ryu.setName("Ryu");    Han.setName("Han");    Ryu.setYVelocity(50);    Han.setYVelocity(25);        cout << "Creating two fighters..." << endl;    cout << "And they jump..." << endl;    Ryu.jump();    Han.jump();        //game loop    do{       //output some info for you       cout << Ryu.returnName() << ": " << (Ryu.returnYPos() > 0 ? Ryu.returnYPos() : 0) << (Ryu.returnYPos() >= 10 ? "\t" : "\t\t") << Han.returnName() << ": " << (Han.returnYPos() > 0 ? Han.returnYPos() : 0) << endl;       //update our fighters       Ryu.Update();       Han.Update();       } while(Ryu.returnYPos() > 0 || Han.returnYPos() > 0);        cout << "Both have landed." << endl;            system("PAUSE");    return 1;}    



Yeah. It compiles and outputs where the fighters currently "are" in their jumps. To really put this in a game, all you would have to do is do somehting like "ifKeyDown(JUMP){ player.jump(); }" and then just add other update code to player.Update(). (note: player is my fake instance of fighter)
Actually since my game is gonna be based on an engine, it's gonna have it to where it'll load a fighters .dat file and graphics .bmp file. The dat file will contain all of the coordinates of every animation state, all of the collision coodinates for every frame, physics, info, and maybe some AI as well, which is gonna be difficult to impliment on a separate file. I'm also gonna have it to where you can create your own fighter. And all the information will be saved into the dat file. It's gonna be sweet by the time I get done but it's also gonna be awhile. I have to juggle work and school, along with time with my girlfriend as well.
I wish I had more people helping me program this. I'm practically doing this by myself.
Oh Jacob, what you might be missing is the fact that you need an initial force as well... you were saying that the arc is not parabolic...

What you should do is give the jumping character an upward initial force (but only at the moment of jumping), and then allow the gravitational force to slow down the acceleration from that jump and then reverse the effect, bringing it back down. Basically, apply that "jumping" force to get your initial acceleration, and then use the equations mentioned earlier to subtract from that acceleration until the jumping object reaches the ground again. If you're doing that (basically using the equations correctly), you should be getting the arc you need.

Now, as for the speed of it, that involves tweaking the values of both (A) the initial jumping force and (B) the gravitational constant. Depending on the size of your characters, you might need to find a larger value for g (the gravitational constant), as if you're interpreting 9.8 as pixels/second^2, characters that aren't proportional in size to that will seem to be falling at a much slower rate. And, of course, if you don't apply enough of an initial jumping force, the thing might hardly get off the ground before gravity compensates and pulls it back to earth again.

Hope that helps!
-Vendal Thornheart=) Programming for a better tomorrow... well,for a better simulated tomorrow. ;)

This topic is closed to new replies.

Advertisement