I'm developing a 2D platformer, although this question could probably apply to just about any 2D type physics; What is the equation for friction? I have sort of a hacky type of friction set up in my game currently, but I'm not satisfied with it because it doesn't account for all forces being applied to my character outside of whatever forces he's generating himself.
-
9\$\begingroup\$ speed *= 0.99; \$\endgroup\$– Markus von BroadyCommented Sep 28, 2012 at 21:45
-
\$\begingroup\$ This will never actually bring speed back down (or up) to 0 though? \$\endgroup\$– TheBroodianCommented Sep 28, 2012 at 21:58
-
3\$\begingroup\$ if ( speed < 0.01 ) speed = 0; \$\endgroup\$– Markus von BroadyCommented Sep 28, 2012 at 21:59
-
2\$\begingroup\$ Remember Coulomb's Law of Friction: Kinetic friction is independent of the sliding velocity. So applying friction by using the speed probably isn't the best way to do it. \$\endgroup\$– HouseCommented Sep 28, 2012 at 22:01
-
1\$\begingroup\$ @Gajoo Of course that's true. I'm sure a lot of games do it that way. I just wanted to make sure that it was know that it wouldn't be physically accurate and may have some weird results for things that have a wide range of velocities. I would also recommend Markus make it an alternative answer, as it's a good one. \$\endgroup\$– HouseCommented Sep 28, 2012 at 22:10
1 Answer
There's two types of friction, static friction and kinetic friction. Static friction is the friction that needs to be overcome to get something moving from rest. You'll notice when pushing a box across the floor, it can often take more force to get it moving than to keep it moving. Or you'll be applying force and it will jolt forward and you'll have to not push as hard. That jolt forward is the point when static friction is overcome and you're dealing with kinetic friction. Kinetic friction is almost always (always?) less then static friction.
Either way, friction is a force just like acceleration. It's calculated based on the normal force of the objects and coefficent of friction (µ).
This free body diagram shows a few forces.
- Green: The force of friction.
- Purple: The force being applied to move the object
- Blue: Gravity
- Red: The normal force, will equal gravity if the object is not sinking into the surface below.
So when the force of friction and the force being applied are equal, the object will not accelerate. So if it's stopped, it's not going anywhere and if it's moving it'll continue to move at that speed.
So you're going to want to collect some information to simulate friction.
- The types of materials, this is needed to get the coefficent of friction (µ).
- The normal force
Then you can simply calculate the force of friction with the following:
friction = µ • normalForce
When you're summing up your forces you include this force. If you have no other accelerations, then your friction is going to be the only force, and will slow your object down.