I have a simple physics simulation I'm creating whilst following the Nature of Code tutorials on YouTube by Daniel Shiffman, at the moment I've just started learning about friction and have had a go at implementing in it my project.
But for some reason, applying friction doesn't work when placed directly in the update method. To elaborate, when I apply friction by holding down the space bar rather than just doing it every frame, the friction works as instead.
Another thing to note is that when I apply friction directly in the update method, the objects don't appear on the screen.
Below is the loop I have that iterates through all the rigid bodies in the scene:
foreach (var body in this.bodies)
{
Vector2 wind = new Vector2(0.01f, 0.0f);;
Vector2 gravity = new Vector2(0.0f, 0.1f * body.Mass);
// Setup friction
float c = 0.05f;
var friction = body.Velocity;
friction *= -1;
friction.Normalize();
friction *= c;
if (Keyboard.GetState().IsKeyDown(Keys.Space))
{
// If I move this call to where I'm applying
// all the other forces, the objects dissappear.
// But if I apply force by pressing the space bar
// the force is applied correctly.
// Apply friction
body.ApplyForce(friction);
}
// If I apply friction out here, it doesn't work
// also, friction when printed to the screen is
// NaN.
// Apply wind and gravity
body.ApplyForce(wind);
body.ApplyForce(gravity);
body.Update();
}
and also, here is the source code that I'm following along with (essentially copying) in the video tutorials here
I feel as though I've supplied enough information, but if you need to know anything else please don't hesitate to do ask.