i am pretty new to c++ and dark gdk. im trying to make a simple platform game to start off. i have the basic controls down, the animations, and collision. but i am having much trouble finding how to create the effects of gravity. please help.

Recommended Answers

All 4 Replies

What exactly do you mean?

If you have an object moving, and you want gravity to account for its movement, then
you can just do this :

const float GRAVITY = -9.8;
ball.x += ball.velocity.x * dt;
ball.y += ball.velocity.y + GRAVITY * dt;

Those follow from basic Newtonian equation.

Shouldn't this be:
ball.y += ball.velocity.y + GRAVITY * dt;
this:
ball.y += ball.velocity.y*dt + (GRAVITY*dt*dt)/2

Gravity should be treated as a (physics)vector. In a 2-D scenario the Gravity vector will have 2 components. X & Y.

struct LineVector
{
  float x;
  float y;
};

LineVector Gravity;
Gravity.x = +0.0f;  //Since gravity acts only in vertical direction
Gravity.y = -9.8f;

Going by Newton's equations in a non-inertial frame:
S = ut + (at^2)/2 , S :displacement, u :initial velocity, t :time
They will split into X-Y with each component contributing in displacement. And the net displacement will be:
sqrt( Sx^2 + Sy^2 ) Where Sx : is X component of Displacement & similarly for Sy.
& deviation will be tangent^-1(Sy/Sx)
Speaking in simpler terms:

ball.x += ball.velocity.x*dt + (GRAVITY.x*dt*dt)/2
ball.y += ball.velocity.y*dt + (GRAVITY.y*dt*dt)/2
/* You can either move the ball using Cartesian coordinates or polar. */

Thanks for the replies but this wont work. I'm looking for something that would create more of a parabola for the ball to follow.

Parabolic trajectory is nothing but a velocity vector under the influence of non-perpendicular deceleration(gravity).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.