This physics-based simulation shows a ball on a roller coaster where the ball can jump off the track. When the ball is on the track, it is colored blue; when in free flight it is colored red. The ball collides with the track when it is in free flight. A spring can be connected to the ball.
Drag the ball with your mouse to change the starting position. Try changing gravity, damping or spring stiffness. The spring is activated when the spring stiffness is non-zero. You can also drag the square anchor point for the spring.
The math behind the simulation is shown below. Also available: source code, documentation and how to customize.
This simulation is based on the Roller Coaster with Spring and uses all of the physics described there. The main difference is that we check for when the ball should jump off the track into free flight. The key to when this happens lies in the formula for acceleration with uniform circular motion, which is
|a = v2/r
This is the acceleration, a , needed to keep a projectile moving in a circle of radius r with velocity v . The direction of the acceleration is towards the center of the circle. So, moving faster in a given circle means you need more acceleration to stay on the same circle. Conversely, moving in a larger circle means you need less acceleration.
Suppose the ball is moving with velocity v over a hill shaped like a circle with radius r . The minimum acceleration (towards the center of the circle) needed to keep the ball on the hill is v2/r . If at any moment the acceleration (towards the center) is less than this, then the ball will fly off the hill.
To apply this to a general curve other than a circle, we need the notion of radius of curvature at a point p on the curve. Let φ = the angle of the tangent to the curve at p . We can express φ as
φ = arctan(dy⁄dx)
where dy⁄dx is the slope of the curve at p . We define the curvature κ as the rate of change of the tangent angle φ as we move along the curve.
|κ = dφ⁄ds
where s = arc length along the curve. The radius of curvature r is the reciprocal of the curvature:
r = 1⁄κ
We can estimate this using the table representing the curve. Here's how: for a given point p we estimate the slope a short distance δ on either side of p . Equation (2) is then approximated by
|arctan(slope at p+δ) − arctan(slope at p−δ)
The radius of curvature r is then the reciprocal of this. And this radius of curvature is what we need to apply equation (1). We know that the ball will stay on the curve as long as the acceleration normal to the curve is greater than v2/r .
In the Simple Roller Coaster we developed an expression for the gravity force on the ball. In the Roller Coaster with Spring we developed an expression for the spring force on the ball. In those cases, we used the component of the force that was parallel to the track. Here, we want to know the component of the force that is perpendicular (normal) to the track. To determine the acceleration normal (perpendicular) to the curve we use the same calculations except that now we use sin θ instead of cos θ .
Fgravity = m g sin θ
Fspring = c sin θ (√(sx2 + sy2) − R)
(Please see those pages for the definition of these symbols.) Since we already know cos θ from the Roller Coaster with Spring simulation we can easily find sin θ from
sin θ = √(1 − cos2θ)
Now we can find the acceleration normal to the curve from
Fgravity + Fspring = m a
We then compare the acceleration to that given by equation (1) to determine whether the ball should leave the track. To stay on the track going over a hill, we must have a > v2/r . On the other hand, to stay on the track when going through a valley we need a < v2/r . (We are glossing over some details of how to determine the correct sign of the acceleration).
When the ball is on the track, the simulation is controlled by the differential equations given in the Roller Coaster with Spring. In this case there are two variables:
When we detect that the ball should leave the track, we switch the controlling differential equations to those of "free flight". There are then four variables:
The differential equations for free flight are the same as those given for the 2-Dimensional Spring simulation.
While the ball is in free flight we need to handle collisions with the track. After
a collision is detected (because the ball is below the track) we back up and use a
binary search algorithm to get the simulation very close to the time of collision. The
Colliding Blocks simulation has some
more about collision handling in general.
Once the simulation has been run to very close to the time of collision, we need to adjust the velocity of the ball so that it bounces off the track. Define the following vectors (vectors are indicated by bold type):
Some elementary vector algebra gives us
|A · B
|B · B
N = A − C
|R = C − N
The elasticity parameter determines how bouncy the ball is. Let e = elasticity. A perfectly elastic ball will have elasticity e = 1.0 and will bounce forever. An elasticity of e = 0.2 would indicate a rather "dead" ball that doesn't bounce very much. To model this behavior we multiply N in equation (4) by the elasticity to get
R = C − e N
This represents the new velocity that we assign to the ball at the time of collision.
Eventually, the ball will make smaller and smaller bounces. In real life, there comes a time when the ball is back in continuous contact with the ground. To determine whether the ball should "jump back onto the track" and be controlled by the "on the track" set of differential equations, we compare the magnitude of the normal vector e N to the magnitude of the total velocity vector. The test involves the stickiness parameter and looks like this:
|< stickiness) then jump back onto track
If the normal velocity e N is small enough we decide the ball should be back on the track. The stickiness is a parameter on the order of 0.1 that can be changed in the simulation controls.
The figure at left shows a typical situation where multiple collisions occur over one simulation time period because the ball goes into a tight curve at high speed.
In the case shown at left, the collision occurred as we were trying to advance the simulation a small time step (perhaps 1⁄10 of a second) from a certain time t0 to the new current time t1 . Now that the collision has been handled, we attempt to run the simulation up to the current time t1 .
If there are no new collisions detected, then this simulation step is done. However if a new collision is detected, we begin the entire collision handling process over again. We will eventually reach time t1 even if we have to handle multiple collisions along the way.
Unlike the other roller coaster simulations, this one does not have the option to use various tracks. The reason is that having a track that doesn't loop simplified the code considerably. For example, to determine whether there is a collision, we only test whether the ball is below the track. With a looped track there would be more complicated criteria for deciding if the ball has collided with the track, such as checking if it is inside or outside the track.
This web page was first published April 2002.