Game Development Reference
In-Depth Information
˙
where m is the body's mass and
p is its linear velocity. A similar expression holds for
the kinetic energy of a rotating rigid body:
2 m
2
1
2
i m | θ
| p
E k =
|
+
|
where i m is the moment of inertia about the axis of rotation of the body (i.e., it is a
scalar quantity) and θ is its angular velocity.
We could use the kinetic energy as the value for motion, but that would create
a problem with different masses: two identical objects would fall asleep at different
times depending on their mass. To avoid this we remove the masses from the expres-
sion to get
+| θ
=| ˙
2
2
motion
p
|
|
In code this looks like
currentMotion = velocity.scalarProduct(velocity) +
rotation.scalarProduct(rotation);
because the scalar product of two vectors is equivalent to their lengths multiplied
together and multiplied by the cosine of the angle between them. If we take the scalar
product of a vector with itself, the cosine is 1, and the result is just the square of the
vector's length.
Some developers use variations on this: they either add the two components to-
gether without squaring them or calculate the full kinetic energy and then divide by
the mass.
In either case this gives us a value for the motion of the object. The final stage
is to check whether this value is stable. We do this by keeping a record of the cur-
rent motion over several frames and seeing how much it changes. This can be neatly
tracked by a recency-weighted average (RWA), one of the most useful tools in my
programming repertoire.
A recency-weighted average is updated by
rwa = bias*rwa + (1-bias)*newValue;
It keeps an average of the last few values, with more recent values being more signifi-
cant. The bias parameter controls how much significance is given to previous values.
A bias of zero makes the RWA equal to the new value each time it is updated (i.e.,
there is no averaging at all). A bias of 1 ignores the new value altogether.
The RWA is an excellent device for smoothing input or for checking that an input
hasstabilized.Inourcasewehave
motion = bias*motion + (1-bias)*currentMotion;
Search Nedrilad ::




Custom Search