Momentum based movement
I am I charge of the physics of our game. The first part is 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 applied 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 enumerator 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 information along 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 (acceleration - friction) * mass * delta time (F = ma) until it reaches the player’s velocity * mass where it stays.
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 enumerator is set to decrease and the decreaseMomentum function is called in the physics component. The first thing the function does is calculate the resistant force of the momentum (F = muR) which equals friction * weight. It then reduces the momentum force by this amount until the momentum reaches 0.
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; } }