CS121 Project 2
Messy details: errors, rolling, and stopping
Numerical inaccuracies are inevitable. For example, the point on a line (e.g. ball's path)
that is r units from a plane (e.g. triangle) may have real coordinates
that require infinite precision to represent. Representing the point
with finite precision introduces round-off error. You must anticipate and accomodate
these round-off errors.
In particular, if you reset the ball's position to the point of collision as
reported by the collision detection algorithm,
the ball may actually be intersecting the triangle.
This isn't a problem if the ball moves out of the triangle during the current
time step. But if there isn't much time left in the time step and/or
if the ball is moving roughly parallel to the plane, the ball may be
intersecting the triangle at the end of the time step. When gravity
is applied in the next time step the ball may move deeper in the plane
containing the triangle but no collision is be detected.
To avoid this problem you can (should)
-
Detect collisions when the ball is r-ε away from the plane; i.e.
when it is intersecting by some small amount
-
Move the ball's position ε away from the surface when a collision
is detected; i.e. if p is the position when the collision is reported
to occur, reposition the ball at p+εn, where n
is the surface normal (possibly a composite normal if multiple collisions
occur).
Note: use the same ε in both cases since otherwise you may move
the ball so it intersects some other triangle in a non-detectable way!
Rolling raises a variety of problems that have a variety of solutions.
In the abstract, when the ball is rolling it is constant contact with
a triangle. Collisions are constantly reported, reflecting the velocity
vector does not change it, and the ball never moves.
In practice, this doesn't happen, because of numerical inaccuracies.
In fact, if you use the detect/correct algorithm described above for errors in
the collision point, rolling is vastly simplified. When a collision is
detected you move the ball away from the surface so it is not colliding.
The ball then can move, roughly parallel to the surface, until another
collision occurs. Repeated collisions are inevitably since gravity is
added at the start of each time step. You can also detect rolling and
only add the component of gravity parallel to the surface but this enhancement
is not really necessary.
Note that ε (from the detect/correct algorithm) is
small so the movement of the ball away from the surface is unnoticably
when the velocity parallel to the surface is relatively large.
Two remaining issues with rolling are damping and stopping.
-
Damping: Since collisions are inelastic, you need to damp the velocity
when a collision occurs.
During rolling, collisions occur in short succession and overdamping may result
so the ball stops too quickly. There are a variety of ways
to solve this problems; e.g. you can make the damping factor depend on
the angle of incidence with the surface or you can make the damping factor
depend on time between collisions.
-
Stopping: The ball's velocity decays due to repeated applications of
the damping factor but it never reaches zero (except because of numerical
inaccuracies). When the velocity parallel to the surface is small,
the ball will appear to vibrate on the surface.
You need to stop the ball in this case; e.g. set its
velocity to zero and end the simulation.
On the other hand, you don't want to stop the ball when its velocity is
small because it is near the apex of its path while flying through
the air. To handle this, you can stop the ball when its change in
position is small across several successive time steps.