Gravity's affect on velocity implemented with speed and direction.

Started by
3 comments, last by Fl00Fy 11 years ago

Sorry if the title's a bit confusing, had trouble trying to summarise the problem in a good way.

Anywho, it seems I may have coded myself into a bit of pickle. I'm trying to create a game which includes gravity and some stuff which is affected by it... I decided, when I started the project, that I would describe each objects velocity with two scalars: speed and direction (in radians, right being 0). It's worked out well so far, until I tried to implement gravity.

The problem is, I can't figure out how to account for gravity by giving the object a downward force. If this just happens to be a bad way to implement velocity, I would be alright with having to change the code to use a different method; but if there is a viable solution for this, I'd love to hear it.

Help of any kind would be appreciated.

Advertisement

Use a 2d vector for your velocity instead. You can convert your direction and speed into a vector easily using trig.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

My article may help to get started with vectors.

You can always use "pseudo" gravity as well, just by adding to the Y-position by an increasing gravity value:

void PhysicalObject::Update(double deltaTime)
{
  if (NotOnGround) {
    YPosition += GravityVelocity;
    GravityVelocity += GravityAcceleration*deltaTime;
  }
  else {
    GravityVelocity = 0;
  }
}

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Thanks for all the advice. I'll probably end up adding the vector implementation. It's about time I start using them to be honest. And the article was a great help, thanks.

This topic is closed to new replies.

Advertisement