C++ game: jumping trajectory

Programming, for all ages and all languages.
Post Reply
RJ
Posts: 7
Joined: Tue Sep 22, 2009 11:51 am

C++ game: jumping trajectory

Post by RJ »

I have had this problem for a while, and I have tried just about everything. I know that trajectory is a parabola, but I can't figure out how to implement this into a game by moving the character along a parabolic path. Can anyone help me with this? I am using DarkGDK and Visual C++.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: C++ game: jumping trajectory

Post by Combuster »

Doesn't the library have physics you can use?

Otherwise, you can do the basic maths yourself:
velocity increases linearly over time in the direction of gravity (usually y). So you can just add a constant value to velocity in each frame to create a parabolic path.

or in physics formulae:
s = 0.5at²
v = at
or
s = 0.5at²+vt when the starting velocity isn't zero.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
RJ
Posts: 7
Joined: Tue Sep 22, 2009 11:51 am

Re: C++ game: jumping trajectory

Post by RJ »

I do have the formulas and all the math down, im good at that stuff. I was asking how I could loop the movement of the character along the parabolic curve. I can't figure out how to model that movement in code.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: C++ game: jumping trajectory

Post by NickJohnson »

RJ wrote:I can't figure out how to model that movement in code.
Combuster wrote:So you can just add a constant value to velocity in each frame to create a parabolic path.
I'm not sure how that isn't an answer...

Although, I personally would store the character's state as two component velocity vectors and a scalar for total mechanical energy. That way, you could make it move according to the vectors, then reevaluate it's velocity based on how much of the energy is kinetic (because you how much is gravitational potential). You could use that to add other potential energy sources and damping and realistically implement springs, etc.
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: C++ game: jumping trajectory

Post by Firestryke31 »

Here's what I would probably do, though I just packaged it in a struct for ease of reading. It would all probably be integrated into the object's physics code if I were doing it. If you're working in 3D the basic concept is the same, you just do it with the Z axis too.

Code: Select all

struct Movement
{
  float xPos, yPos;
  float xVel, yVel;
  float xAccel, yAccel;

  // timeStep is used for when rendering and logic get out of sync.
  // timeStep = renderFrameTime / logicFrameTime (often, logicFrameTime is fixed at 60);
  // 1.0 == locked render and logic, < 1 == more rendered frames, > 1 == more logic frames
  // It's a ratio, so it should never reach 0 or less
  void update(float timeStep)
  {
    xVel += xAccel * timeStep;
    yVel += yAccel * timeStep;
    xPos += xVel * timeStep;
    yPos += yVel * timeStep;
  }
};
Then, to jump, just set yVel to the jump speed and yAccel to gravity. When the char hits the ground set both back to 0. The nice thing about using acceleration to control velocity is that you can get nice, smooth starts and stops, and it works everywhere.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
RJ
Posts: 7
Joined: Tue Sep 22, 2009 11:51 am

Re: C++ game: jumping trajectory

Post by RJ »

Thanks! I had to mod it a little but it works now as a good preliminary. I was thinking too hard I should have tried a simpler approach like yours #-o The timestep was the best part I knew my game needed some sort of game time counter because of choppy game play when facing the middle of the map and superfast gameplay when facing away from the graphics, but I couldn't think of how to implement something like that.
User avatar
Thomas
Member
Member
Posts: 281
Joined: Thu Jun 04, 2009 11:12 pm

Re: C++ game: jumping trajectory

Post by Thomas »

Physics of motion in 2 dimension using Netwon's laws

I assume that there is no air friction and g = ( GM / R^2 ) , where G = universal gravitational constant and R is the radius of the earth and M is the mass of the earth . { The above also can be derived using Newton's laws }

Consider that a point mass is released from the gorund with a velocity v at an angle thetha. I am taking the point from it released as origin .

resolving the velocity vector into 2 components
speed_x = v * cos ( theta)
speed_y = v * sin (theta )



Displactemt_y = v*sin(theta) *t - 0.5 * g * t ^ 2 { Only force in the -y direction is the gravitational force }

Displacement_x = v * cos ( theta ) * t { We assume that there is no force along the the x direction }

where t is the time taken

therefore
t = displacemnt_x / ( v * cos (theta) )

substituting t in the displacement_y equation

displacement_y = displacement_x * tan(theta) - 0.5 * g * (displacement_x ) ^2 / (v* cos(theta) )^ 2

For simplicity , displacement_x = X , displacement_y = Y
Y = X * tan (theta) - 0.5 * g * ( X ^2) / ( v* cos ( theta) )^2

This equation represents the 2 d projectile motion equation of particle . This equation can be used in many ways to solve problems .

It is easy to show that the above equation represents a parabola .

The general equation of a parabola with origin as its vertex is ( facing downward , a is a constant )
x ^2 = - 4 *a * y

The parametric equation of a parabola is x = 2*a*t and y = -a*t^2 where t is a parameter .

Like shown above the parametric equation is
X = v * cos ( theta ) * t
Y = v*sin(theta) *t - 0.5 * g * t ^ 2

You can now plot both x & y to simulate parabolic motion .

-- Thomas
Post Reply