[Physics] Friction (ice physics)

Hi there.
I’m been trying to create some nice ice physics for the project I’m working on (yes, the one still in my signature).
Something like this:


(start at 4:20)

I’ve tried a variety of things. I am aware that I can just apply force to achieve a similar effect, but I want to make a general piece of code for this that only uses linear velocity (and grabs friction from the platform properties). My most successful method was using “steps”. I split the magnitude of the velocity vector, and added it to the objects current velocity every frame (so it would start slow, and then get faster; an arithmetic sequence). However; I’m not quite sure how to limit the speed, and how to go about actually making the object stop.

Anybody have a good way of achieving this?

Making sure an object doesn’t exceed some maximum speed is very simple.

vel = obj.getLinearVelocity().magnitude
speed = vel.magnitude
if speed > MAX_SPEED:
    obj.setLinearVelocity(vel.normalized() * MAX_SPEED)

A simple and effective way for slowing down an object is just to reduce its speed by some percentage of its current velocity based on the friction of the surface its on every frame.

vel = obj.getLinearVelocity()
vel *= FRICTION# where 0 <= FRICTION<= 1 (0 would stop the object instantly, and 1 would be no friction)
obj.setLinearVelocity(vel)

Mobious’ method of friction was the way I used to go, but I’ve since changed to subtracting a constant amount from the object’s velocity (as the faster you go, the longer it should take to slow down).


vel = obj.worldLinearVelocity
vel.magnitude -= obj['friction'] if vel.magnitude >= obj['friction'] else vel.magnitude

Ahh, perfect, I think it’s working. I was having a lot of trouble with stopping… Thanks SolarLune and Mobious. Just took a little bit of logic. I actually used both of those ideas at some point during my tests but for some reason never thought of putting them together.

You guys can test the file if you want. WASD to move. Friction levels can be changed on the platform itself (properties).
nate_setupv01.blend (482 KB)
I like dividing the velocity instead of subtracting a constant. I may change it later if subtracting proves to be better.

Oh, and SolarLune, if you want to see your platform script in action, just change the rotation and movement on the platform properties as well.