First, we should clarify what is represented here. You write “If an item is moving right, it has positive force…” Usually, if an item is moving right, we would expect it to have a positive velocity in that direction, not necessarily a positive force.
If you are dragging a solid object on a solid surface, the “ideal” friction resisting the dragging motion is the force pushing the object into the surface (e.g., gravity) multiplied by the coefficient of friction (dependent on the materials involved). Dividing this force by the mass of the object produces an acceleration. In a situation where the object (including its mass), materials, and perpendicular force are not changing, you can calculate this acceleration once and treat it as a constant, a
.
In a continuous model, this acceleration decreases the velocity to zero smoothly. In your model, you are probably using discrete time steps, some amount of time t
. Thus, in each normal step, the velocity decreases by acceleration times the step time, a*t
. We can also calculate that and assign it to some variable, say d
.
A problem occurs because your discrete time step may overshoot the moment when velocity becomes zero. To deal with this, you might use code such as:
if (velocity > d)
velocity -= d; // Resist rightward velocity for a full time step.
else if (velocity < -d)
velocity += d; // Resist leftward velocity for a full time step.
else
velocity = 0; // Resist small velocity until it becomes zero.
Certainly one could use other models, and there are different physics for objects that are not solid. A simple model that some have mentioned in other answers and comments is diminishing velocity by multiplying by a value less than one. That example does not model the real-world physics of dragging a solid object. Whether it is suitable for your game may be an aesthetic choice. If you use it, your code might be simply:
velocity *= factor;
One problem with that is that some processors have very poor performance for subnormal numbers. The most common floating-point arithmetic used, IEEE-754, specifies an interval for very small numbers that are handled specially (to get certain mathematical properties that assist with proofs and predicting behavior). An expression such as velocity *= factor;
will drive numbers to be smaller and smaller, in the absence of any other changes, until they reach this subnormal interval. On vulnerable processors, this will cause the performance of a program to drop suddenly. This may of course ruin game physics. Some systems set a processor mode in which subnormal numbers are converted to zero. This avoids the performance problem but violates the IEEE-754 standard. It may be acceptable for game physics.