Simulation of a ball moving on a track, where the ball can fly off the track.

See https://www.myphysicslab.com/RollerFlight.html for additional explanations of the math involved here.

Radial Acceleration

This version of the roller coaster simulation has the ball jump off the track when appropriate.

The acceleration for uniform circular motion is a = v^2/r.

Suppose the ball is going over a hill. The minimum radial acceleration to keep the ball on the track is v^2/r, where r = radius of curvature of the track (at the point where the ball currently is).

The actual acceleration normal to the track is given by the component of gravity (and other forces, eg. spring) that are normal to the track. If the actual acceleration is less than the minimum acceleration then the ball leaves the track.

Radius of Curvature

The radius of curvature of the track is given by reciprocal of kappa

kappa = |d phi / d s|
phi = slope angle of curve = taninverse(dy/dx)
s = arc length.

Another way to get it is:

           |d^2 y / d x^2|
kappa =  --------------------
         (1 + (dy/dx)^2)^(3/2)

On Track vs. Off Track

There is a different set of variables and equations depending on whether the ball is on or off the track. We keep a 'track/free mode' flag in the VarsList:

vars[6] = track or free mode (1 or 0 respectively)

That Variable broadcasts itself whenever it changes.

On the track, the equations are similar to those of RollerDoubleSim; we have 2 variables:

vars[0] = p = distance along track
vars[1] = v = velocity on track

When the ball leaves the track, the equations are similar to those of Spring2DSim; we have 4 variables:

vars[2] = Ux = x position
vars[3] = Uy = y position
vars[4] = Vx = x velocity
vars[5] = Vy = y velocity

Storing track or free mode in vars[6] is useful for backing up in time; it allows the vars to hold the complete state.

Equations of Motion

See RollerDoubleSim and Spring2DSim for derivations.

On the track:

let k = slope of curve. Then sin(theta) = k/sqrt(1+k^2)
let b = damping constant
let g = gravity
v' = - g sin(theta) - (b/m) v= - g k/sqrt(1+k^2) - (b/m) v

Off the track:

let U = position of mass, V = velocity vector
vars[2] = Ux
vars[3] = Uy
vars[4] = Vx
vars[5] = Vy
vars[6] = sim_mode (0=TRACK_MODE, 1=FREE_MODE)

Without a spring force, the off-the-track equations of motion are:

Ux' = Vx
Uy' = Vy
Vx' = 0
Vy' = -g

Collisions

To detect collisions: see if the ball is below the track. We require that the track has no loops, so each x has a single point of the track above or below it. To handle the collision, we reflect the velocity in the tangent of the curve.

Math in handleCollisions

From vector algebra:

Let B = (1,k) be vector of line with slope k
Let A = (vx, vy) be vector of velocity
        (A·B)
Let C = ----- B  = component of A in B direction
        (B·B)

Let N = A - C = component of A normal to B

Then the reflection of A across the line B = C - N

But we multiply the normal by the elasticity e, so
result = C - e*N

TO DO Add to docs the mathematica file 'roller.nb' where curves like Hump and others were worked out.

TO DO To have a track with loops and vertical lines we need to have a way to determine what is inside or outside, or which direction the track inhibits movement. Need to deal with track sections that are vertical lines (infinite slope) & straight lines (infinite radius).

Hierarchy (view full)

Implements

Constructors

Properties

collisionFunction_: null | ((c, t) => void) = null

Function to print collisions, or null to turn off printing collisions.

Type declaration

debugPaint_: null | (() => void) = null

Function to paint canvases, for debugging. If defined, this will be called within moveObjects() so you can see the simulation state after each time step (you will need to arrange your debugger to pause after each invocation of debugPaint_ to see the state).

Type declaration

    • (): void
    • Returns void

elasticity_: number = 0.8

bounciness of ball when colliding with track

initialState_: null | number[] = null

Initial values.

lowestPoint_: number

lowest possible y coordinate of path

potentialOffset_: number = 0

potential energy offset

stickiness_: number = 0.1

stickiness of track, determines when ball jumps from free flight onto track

xHigh_: number

range of x values of the path

xLow_: number

range of x values of the path

Methods

  • Defines the differential equations of this ODESim; for an input set of variables, returns the current rate of change for each variable (the first derivative of each variable with respect to time).

    The timeStep is the time since the state variables were last fully calculated, which can be and often is zero. The current time can be regarded as getTime() + timeStep. The input variables correspond to the Simulation state at that time. Note that timeStep is different from the time step used to advance the Simulation (as in AdvanceStrategy.advance). The timeStep is typically used when finding collisions in CollisionSim.findCollisions.

    Parameters

    • vars: number[]

      the current array of state variables (input), corresponding to the state at getTime() + timeStep

    • change: number[]

      array of change rates for each variable (output), all values are zero on entry.

    • _timeStep: number

      the current time step (might be zero)

    Returns null | object

    null if the evaluation succeeds, otherwise an object relating to the error that occurred. The change array contains the output results.

  • Finds collisions based on the passed in state variables. Can rely on modifyObjects having been called prior, with this set of state variables. Uses the state saved by saveState as the 'before' state for comparison.

    The list of collisions that are passed in can potentially have collisions from the near future that were found previously. The CollisionSim should avoid adding collisions that are duplicates of those already on the list.

    Parameters

    • collisions: RollerCollision[]

      the list of collisions to add to

    • vars: number[]

      the current array of state variables

    • _stepSize: number

      the size of the current time step, in seconds

    Returns void

  • Called at the end of a mouse drag operation, performs whatever action is appropriate. Only called if startDrag returned true.

    Parameters

    • _simObject: null | SimObject

      the SimObject being dragged, or null if no SimObject was found

    • _location: Vector

      the location of the mouse in simulation coordinates of the SimView where simObject was found, or in the focus view if simObject is null.

    • _offset: Vector

      distance from the initial object position to the mouse location at start of drag.

    Returns void

  • Adjusts the simulation state based on the given Collisions. For example, this might reverse the velocities of objects colliding against a wall. The simulation state is contained in the vars array of state variables from getVarsList.

    Note that these Collisions will typically be from the very near future; CollisionAdvance backs up to just before the moment of collision before handling Collisions.

    Parameters

    • collisions: RollerCollision[]

      the list of current collisions

    • Optional opt_totals: CollisionTotals

      CollisionTotals object to update with number of collisions handled

    Returns boolean

    true if was able to handle the collision, changing state of simulation.

  • Called when a key is pressed or released, performs whatever action is appropriate for that event.

    Parameters

    • _evt: KeyboardEvent

      the KeyboardEvent that happened

    • _pressed: boolean

      true means this is a key-down event; false means a key-up event

    • _modifiers: ModifierKeys

      the modifier keys down during event

    Returns void

  • Called at each movement during a mouse drag, performs whatever action is appropriate. Only called if startDrag returned true. The SimObject being moved is passed in, along with the current mouse position, in simulation coordinates, and an offset calculated at the start of the drag.

    Setting the SimObject position to (x - offsetX, y - offsetY) will move the SimObject smoothly along with the mouse movement.

    Parameters

    • simObject: null | SimObject

      the SimObject being dragged, or null if no SimObject was found

    • location: Vector

      the location of the mouse in simulation coordinates of the SimView where simObject was found, or in the focus view if simObject is null.

    • offset: Vector

      distance from the initial object position (from DisplayObject.getPosition) to the mouse location at start of drag.

    Returns void

  • Sets whether this Subject will broadcast events, typically used to temporarily disable broadcasting. Intended to be used in situations where a subclass overrides a method that broadcasts an event. This allows the subclass to prevent the superclass broadcasting that event, so that the subclass can broadcast the event when the method is completed.

    Parameters

    • value: boolean

      whether this Subject should broadcast events

    Returns boolean

    the previous value

  • Sets a function for printing collisions. The function is called whenever a collision occurs. The function takes two variables: a RollerCollision and a Terminal. This can be defined from within the Terminal by the user. Here is an example usage

    sim.setCollisionFunction(function(c,t) {
    const s = c.getDetectedTime().toFixed(2)+"\t"
    +c.getImpulse().toFixed(2)+"\t"
    +c.getPathPoint().getX().toFixed(2)+"\t"
    +c.getPathPoint().getY().toFixed(2);
    t.println(s);
    })

    Parameters

    • f: null | ((c, t) => void)

      the function to print collisions, or null to turn off printing collisions

    Returns void

  • For debugging, specify a function that will paint canvases, so that you can see the simulation while stepping thru with debugger.

    Parameters

    • fn: null | (() => void)

      function that will paint canvases

    Returns void

  • Called at the start of a mouse drag. The nearest dragable SimObject is passed in, along with mouse position in simulation coordinates. If no dragable SimObject was found, null is passed for the first argument. If the EventHandler does not recognize the SimObject then it should return false.

    Parameters

    • simObject: null | SimObject

      the SimObject that is nearest to the mouse drag coordinates, or null if no SimObject was found

    • _location: Vector

      the location of the mouse in simulation coordinates of the SimView where simObject was found, or in the focus view if simObject is null.

    • _offset: Vector

      distance from the initial object position (from DisplayObject.getPosition) to the mouse location at start of drag

    • _dragBody: null | Vector

      location of 'drag point' on the SimObject in body coordinates of the SimObject; this is where for example a spring will be attached on the SimObject when dragging; or null when no SimObject was found

    • _modifiers: ModifierKeys

      the modifier keys down during event

    Returns boolean

    true if the EventHandler will handle dragging the SimObject

  • Returns a minimal string representation of this object, usually giving just identity information like the class name and name of the object.

    For an object whose main purpose is to represent another Printable object, it is recommended to include the result of calling toStringShort on that other object. For example, calling toStringShort() on a DisplayShape might return something like this:

    DisplayShape{polygon:Polygon{'chain3'}}
    

    Returns string

    a minimal string representation of this object.

Generated using TypeDoc