0
\$\begingroup\$

I'm coding the physics for a little platformer I'm making but I'm not sure how to design a friction calculation that does what I expect. When running along a platform I want the player's character to pick up speed but be prevented from going too fast due to friction counteracting their movement. Then, if they stop running I want them to continue to run for a little bit proportional to their speed (if they're going slow they should stop almost instantly).

void Update(V2!float forces, Elapsed time){
    position += (velocity * time.deltaS);
    forces -= 0.5 * velocity^3; //friction???
    velocity += (forces * time.deltaS);
}

The forces variable contains anything accelerating the object such as gravity, or acceleration from the player hitting a directional key, etc. What keeps happening is that whatever I try to plug in as friction doesn't bring the object its moving to a nice clean stop. It will look like it's stopped but then move a pixel or two after that because its velocity, while very small, doesn't hit zero for several seconds.

I can fix this with a bunch of conditional statements, but if there's a simple solution I'd much rather do that!

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Did you try using a different power for the velocity or a combination of powers? Power 3 means that for small speed the friction is almost zero, eg 0.1^3=0.001. Drag from air flow is normally ~v^2 for higher and ~v for low speed so you could use a linear combination av^2+bv. Sliding friction is even independent of speed, so you could add an extra constant which will dominate the friction for low v.

\$\endgroup\$
6
  • \$\begingroup\$ Same issue even with different powers and a combination. What would a constant that dominates at low speeds consist of/look like? \$\endgroup\$
    – NovaCrist
    Commented Mar 16, 2015 at 8:34
  • \$\begingroup\$ Something like 42*(v<0?+1:-1), pick your favorite number instead of 42. OK, it has a conditional in it, but this is what nature would do. But thinking of it, in reality nature has another conditional testing for v=0 (static friction) so this gets complicated. Still I wonder why it is not working with the linear term even after a second or so. Can you give some numbers for a typical case, like initial speed, time.delta and time and "physical" distance to real standstill? \$\endgroup\$
    – Holger
    Commented Mar 16, 2015 at 9:52
  • \$\begingroup\$ time.delta is around 0.0002-0.0005 seconds as it's just a tick of the game, they should = 1 if added up over one second. With a, b, and c set to 0 there's no drag. When they're at 1 My speed caps out to velocity = 13 (pixels/s^2) (when providing +200 force) but I still have the same problem. When c = 42 it doesn't keep sliding, but it stops way too abruptly... So I think fiddling with the values I can get what I want through trial an error? forces -= ( a * velocity^2 ) + ( b * velocity ) + c; \$\endgroup\$
    – NovaCrist
    Commented Mar 16, 2015 at 11:19
  • \$\begingroup\$ Say you have force-=b*v and from user input you get a force f. The velocity should grow to v=f/b, because then the total force is be zero. Say you switch off all external forces while going at speed v. After time 4/b the velocity has fallen to 2% of v, ie it stops. And till then the thing moves by v/b pixels. For example take b=5 and force 100. Then the maximum speed should be 20 pix per sec (position is in pixels, right?). It should stop after 0.8s = 4 pixels. If this is what you observe you can fine tune the parameters. If its way off, then we should look for the problem elsewhere. \$\endgroup\$
    – Holger
    Commented Mar 16, 2015 at 21:53
  • \$\begingroup\$ Plugging in those numbers... yep, I get those results! But the third and fourth pixel it moves by are spread out so it looks like it's come to a stop after the first two, and then a slightly delayed movement occurs, and then finally a very delayed movement occurs. I think I'm going to use this rechneronline.de/function-graphs to figure out a curve that's steep in the right areas to get the effect I want. Thanks for the help! \$\endgroup\$
    – NovaCrist
    Commented Mar 17, 2015 at 9:51

You must log in to answer this question.

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