Nah, we're miscommunicating. Let me rephrase.
Get the idea of momentum and momentum angle out of your head. Those are flawed concepts for this problem.
You have 3 key elements:
1) a position
2) a velocity
3) a direction that the player is facing
'velocity' is similar to your 'momentum'. The key difference is that instead of just being an "overall" speed, it is two different speeds -- one along the x axis and one along the y axis. All you have to do to apply the velocity is add it to the player's position:
1 2 3 4
|
position += velocity; // done -- assuming both position and velocity are vectors
// or if using sf::Sprite to keep track of the position:
yoursprite.move( velocity );
|
Note that the velocity is completely independent of the facing direction. You can have a velocity of 1,0 (moving east) when the ship is facing due north, or you can have a velocity of -1,0 (moving west) when the ship is facing east... or whatever.
The only thing that modifies the velocity is
force.
---
quick example of the problem you're seeing:
Ship is moving west, player is facing east. Player applies force.
Desired result: ship slows down. Angle of movement does not change (ship still moving west, just at a slower rate)
With your momentum approach, this is very, very difficult to accomplish because you somehow have to recognize that the angle of movement is directly opposite the angle that the player is facing. For proper behavior here, m_MomentumAngle
must not change. It's almost like you have to treat this as a special case -- even when it really isn't.
But anyway... throwing away the momentum angle and using the velocity approach... all we have to do to accomplish this result is adjust the velocity by applying force in the direction the player is facing.
So let's say that we're moving at full speed (10) west. So our velocity is
-10,0
. Then the player faces due east and pushes forward.
We calculate the
force they applied by using our famous sin/cos pair. Given an accelleration rate of '1', this means the force they applied by moving due east is
1,0
. To apply this force, we just add it to the velocity.
velocity
-10,0
+ force
1,0
= new velocity
-9,0
.
Mission accomplished! We're still moving due west, but at a slower rate. As more force is applies, the ship will slow down and eventually reverse direction.
But the thing is it works with any angle. Say they're moving slightly north-west with a velocity of
-5.6,-1.2
. They point due east and push forward. Resulting in the same force
1,0
. After applying that force, we have a new velocity of
-4.6,-1.2
.
With continued application of that force, the ship will eventually start moving north-east instead of north-west.
So that's the concept of force vs. velocity. With that in mind, let's revisit my previous example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
// This is code that is performed when the player fires their engines ('W' pressed)
sf::Vector2f force; // we need to calculate the force to apply to the ship
double angle = angle_the_ship_is_facing * pi / 180;
force.x = cos( angle );
force.y = sin( angle );
force *= ship_accelleration_rate * your_time_scale;
// now we have our desired force. Apply it to our velocity:
ship_velocity += force; // <- it's as easy as this!
// only other thing we have to do is stop the player from moving too fast.
// we can do this by examining the overall rate of the velocity (by applying pythagorean
// theorum)
double totalvelocity = sqrt( (ship_velocity.x*ship_velocity.x) + (ship_velocity.y*ship_velocity.y) );
// here, 'totalvelocity' is equivilent to your 'momentum' value. It's the overall speed of the
// ship... without any indication of what direction it's moving towards.
if( totalvelocity > maximum_velocity ) // if it's moving faster than a given maximum speed
{
// we can slow the ship down by scaling down the velocity
velocity *= maximum_velocity / totalvelocity;
}
|