I’m learning how to use the XNA framework in C#, and I’m struggling with a basic error when calculating a basic form of friction.
The code for calculating the friction is:
//Check X deadzone movement
if (Velocity.X < 0.1f && Velocity.X > -0.1f)
{
Velocity.X = 0f;
}
else
{
//Friction Calculation
if (Velocity.X > 0) Velocity.X -= Friction;
if (Velocity.X < 0) Velocity.X += Friction;
}
//Check Y deadzone movement
if (Velocity.Y < 0.1f && Velocity.Y > -0.1f)
{
Velocity.Y = 0f;
}
else
{
//Friction Calculation
if (Velocity.Y > 0) Velocity.Y -= Friction;
if (Velocity.Y < 0) Velocity.Y += Friction;
}
Where
Friction = 0.2f;`
My issue, is whenever I move the player to the right or down (positive directions), the velocity “sticks” and it is not being affected by friction. It stays at a constant value of around 0.4, too high to be affected by my deadzone. This doesn’t happen on negative velocites.
This seems like a simple error in my logic, but I can’t seem to figure it out, any help?
Your logic will immediately reverse the change in friction for values between .1 and .2. Try adding ‘else’ so you don’t immediately reverse the change.