c++ - Is there a more elegant/clever/brief way to bring an unknown number closer to 0? -
basically i'm working out drag in game physics. sideways motion needs reduced (as in brought closer 0, not brought negative), don't know direction it's travelling in, don't know whether add drag or subtract drag.
(if item moving right, has positive force, moving left, negative force)
right i'm doing this:
if (objects[i]->force.x > 0) objects[i]->force.x -= drag; else objects[i]->force.x += drag; which works fine, feeling there's bound sleeker way this.
it couldnt hurt find way ensure doesn't go past 0
if trying emulate friction, easiest way, know velocity , use mathematical model, or differential equation approximate it.
dx -c * -- dt basically, force of friction proportional velocity , in opposite direction. thus, can calculate force of friction (or drag) follows:
objects[i]->force = -drag_coefficient * objects[i]->velocity if not know velocity, gets difficult results, may have force pushing right, while object moving left (therefore apply drag wrong direction).
edit:
actually, not kinds of friction depend on magnitude (speed) of velocity, dry friction referred taking direction of velocity account:
force = -drag_coefficient * sign(velocity) however computationally becomes unstable velocity reaches 0, overshoots , oscillates , forth.
another model 1 wet friction, 1 used in first example:
force = -drag_coefficient * velocity this means object running faster, slowed down more friction. think example of force of air, running, faster running, more air trying slow down.
neither of models above 100% accurate, game should more enough
Comments
Post a Comment