Momentum based movement

Momentum based movement

Jan 20, 2019. | By: Evan Diamond
2 mins

I am I charge of the physics of our game. The first part being a basic implementation of momentum based movement.

For any kind of sprite movement in my previous projects I would just update the position of the sprite with a velocity * delta time. Now however, since we wished for our game to have blocks with different amounts of friction this basic system had to be updated with momentum being applyed to the player not just velocity.

Momentum Increase

The player moves left and right with the A and D keys. When the user presses either of these keys. A momentum change enum is set to increase and the player’s velocity is set to the direction they wished to move in (-x for left and +x for right). This infomation aslong with the player’s acceleration and the friction of the block the player is on (just 1,1 since no implementation yet) is then given to the increaseMomentum function in the player’s physics component. The player’s momentum force is then increased by (accelertation - friction) * mass * delta time (F = ma) until it reaches the player’s velocity * mass where it stays.

Code

void PhysicsComponent::increaseMomentum(Vector2 velocity, Vector2 acceleration, Vector2 friction, float dt_sec)
{
  if (velocity.getX() > 0)
  {
    if (mometum_force.getX() < velocity.getX() * mass)
    {
      mometum_force.setX(mometum_force.getX() + (acceleration.getX() - friction.getX()) * mass * dt_sec);
    }
    else
    {
      mometum_force.setX(velocity.getX() * mass);
      momentum_change = ParameterChange::MAINTAIN;
    }
  }
  else if (velocity.getX() < 0)
  {
    if (mometum_force.getX() > velocity.getX() * mass)
    {
      mometum_force.setX(mometum_force.getX() - (acceleration.getX() - friction.getX()) * dt_sec);
    }
    else
    {
      mometum_force.setX(velocity.getX() * mass);
      momentum_change = ParameterChange::MAINTAIN;
    }
  }
}

Momentum Decrease

When the user releases a or d to stop moving the momentum change enum is set to decrease and the decreaseMomentum function is called in the physics component. The first thing the function does is calculate the resitant force of the momentum (F = muR) which equals friction * weight. It then reduces the momentum force by this amount untill the momentum reaches 0.

Code

void PhysicsComponent::decreaseMomentumForce(Vector2 colliding_friction, Vector2 velocity, float dt_sec, float glbl_grav_dir)
{
  Vector2 resistant_force = colliding_friction * std::abs(getWeight(glbl_grav_dir)) * dt_sec;
  resistant_force.setX(resistant_force.getX() * static_cast<float>(velocity.getPolarityX()));
  resistant_force.setY(resistant_force.getX() * static_cast<float>(velocity.getPolarityY()));

  if (velocity.getX() > 0)
  {
    mometum_force.getX() - resistant_force.getX() < 0
      ? mometum_force.setX(0)
      : mometum_force.setX(mometum_force.getX() - resistant_force.getX());
  }
  else if (velocity.getX() < 0)
  {
    mometum_force.getX() - resistant_force.getX() > 0
      ? mometum_force.setX(0)
      : mometum_force.setX(mometum_force.getX() - resistant_force.getX());
  }

  if (mometum_force.getX() == 0)
  {
    momentum_change = ParameterChange::MAINTAIN;
  }
}

Since momentum force is applied to the player position the player still moves even after the releasing a or d thus causing the sliding effect.

About

Gamers developing new games.

Social Links

Our Bunker

University of the West of England,
Frenchay Campus, Bristol, BS16 1QY,
United Kingdom.