6

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.

TheBroodian
  • 563
  • 5
  • 14
  • 9
  • This will never actually bring speed back down (or up) to 0 though? – TheBroodian Sep 28 '12 at 21:58
  • 3
    if ( speed < 0.01 ) speed = 0; – Markus von Broady Sep 28 '12 at 21:59
  • 2
    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. – House Sep 28 '12 at 22:01
  • @Byte56 what markus suggested can be used in most cases unless you want to simulate real physics. as long as gamers care both method either this one or the one you answered will simply look like each other. – Ali1S232 Sep 28 '12 at 22:08
  • 1
    @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. – House Sep 28 '12 at 22:10
  • Actually, looks like this was kind of asked before: http://gamedev.stackexchange.com/questions/4673/not-sure-how-to-handle-deceleration?rq=1 Though, they call it "deceleration" I think they mean friction. – House Sep 29 '12 at 00:46
  • Also Farseer Physics (http://farseerphysics.codeplex.com/) if you actually need realistic physics. Otherwise interpolation functions such as @MarkusvonBroady are the way to go. – ClassicThunder Sep 29 '12 at 01:21

1 Answers1

12

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 (µ).

enter image description here

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.

House
  • 73,224
  • 17
  • 184
  • 273