Page 1 of 1

C++ game: jumping trajectory

Posted: Tue Oct 13, 2009 12:34 pm
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++.

Re: C++ game: jumping trajectory

Posted: Tue Oct 13, 2009 1:44 pm
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.

Re: C++ game: jumping trajectory

Posted: Tue Oct 13, 2009 4:16 pm
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.

Re: C++ game: jumping trajectory

Posted: Tue Oct 13, 2009 6:05 pm
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.

Re: C++ game: jumping trajectory

Posted: Tue Oct 13, 2009 7:52 pm
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.

Re: C++ game: jumping trajectory

Posted: Thu Oct 15, 2009 11:03 am
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.

Re: C++ game: jumping trajectory

Posted: Sat Oct 17, 2009 12:31 pm
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