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 *= 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

            // If I apply friction out here, it doesn't work
            // also, friction when printed to the screen is
            // NaN.

            // Apply wind and gravity


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.


1 Answer 1


When velocity is 0, friction becomes a zero vector and you can't normalize it, that's why it becomes NaN

  • \$\begingroup\$ Thank you, I was able to resolve this issue quickly by taking a look at processing's PVector source code, it seems that the normalize function is a little different to the mono game counter part. \$\endgroup\$ Apr 11, 2018 at 10:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .