Coding Math: Episode 13 - Friction

preview_player
Показать описание
Continuing the basic physics for programmers series, this time we look at friction, two ways to accomplish it, one right, one wrong, and the pros and cons of each.

Рекомендации по теме
Комментарии
Автор

To get friction you could also make a vector that is the velocity normalized then multiply it with some value and subtract it from the velocity vector. vel -= normalized_vel*1.5f;

vertexbyte
Автор

You can optimize the first friction method slightly by comparing lengths without using Math.sqrt(). Effectively you are comparing length^2 rather than length, but the relationships >, =, and < remain equivalent.

Technomancr
Автор

Presumably method 2 is trickier to deal with if you are using delta times to make your game fram rate independent?

Arganoid
Автор

Interesting, thx. You've made an error though : if k is the friction factor and dt the step time then
Vx = Vx - k * Vx* dt   and
 Vy = Vy - k * Vy * dt
is the right velocity formula : no need for cos/sin/atan or to approximate the friction. Cheers !

TheVinchebleu
Автор

I think you can get correct friction with only one `sqrt` by doing this:
v' = max{ (|v|-f)/|v|, 0 } · v
where f is the magnitude of the friction vector.
Still not as efficient as the incorrect but good enough version, of course. Also you need to check that the current velocity is non-zero.

I'm not used to JavaScript, and mainly use Common Lisp, so sorry for the code that's probably less readable for most people, but here is a CL implementation either way (assuming usage of rtg-math):
```lisp
(defun friction (velocity friction)
(let ((v (v2:length velocity)))
(if (v2:0p v)
velocity
(v2:*s velocity (max (- v friction)
0.0))))
```

thomasbartscher
Автор

is a "vector" any different from just making an object, for example drag = {x:0.04, y:0.1} ? I've never heard of a javascript vector..

justsomedude
Автор

You got my subs.. nice tutorials.
Thanks a lot...

hmzfast
Автор

Memoization for the win. I only re-calculate length/angle on set...() functions.

TechLord
Автор

I love how you wrote the Wikipedia url

VictorRibeiroJV
Автор

If the precentage-way to do is unreal, can't we just make the precentage Exponential?

jojbajohan
Автор

isnt friction the *max* speed value at x axis ?

giancarloandrebravoabanto
Автор

nice 😜 | dear god | tks | 😯 dear | love it | 😉 |

alexjames