Simulation of rigid body movement with external forces like gravity or springs, but no collisions or contact forces. RigidBodys will pass thru each other unless you use the ImpulseSim or ContactSim sub-class.

The AdvanceStrategy tells the DiffEqSolver to advance the simulation. The DiffEqSolver advances the simulation by calling evaluate to calculate rates of change in each of the simulation variables. The DiffEqSolver then uses an algorithm like Runge-Kutta to integrate forward over a small time step to reach the new simulation state. Within evaluate(), the forces operate by modifying the rate of change of each variable.

More information:

Parameters Created

Events Broadcast

All the Parameters are broadcast when their values change. In addition:

RigidBodys

RigidBodySim maintains a list of RigidBody's which are currently part of the simulation. RigidBodys can be added or removed while the simulation is running. Each RigidBody is added to the SimList (or removed when the RigidBody is removed).

ForceLaws

RigidBodySim maintains a list of ForceLaw's which are each given an opportunity to apply their force to RigidBodys during evaluate(). Some ForceLaws such as GravityLaw and DampingLaw are set up so that they observe the SimList and can therefore apply their force to every RigidBody.

Variables

Variables are stored in a VarsList. Each RigidBody gets a set of six contiguous variables that describe its current position, angle, and velocity. The variables are laid out as follows:

  1. x horizontal world coords position of center of mass
  2. x' horizontal velocity of center of mass. AKA vx
  3. y vertical world coords position of center of mass
  4. y' vertical velocity of center of mass. AKA vy
  5. w angle of rotation from body coordinates in radians with positive rotation being counter-clockwise. Called w, because w looks like the greek letter ω (omega) which is often used for angles in math.
  6. w' angular velocity. AKA vw.

The starting index of a RigidBody's variables is given by RigidBody.getVarsIndex. To find a particular variable, add the appropriate offset from the enum RB. For example, to find the angular velocity of a RigidBody:

const idx = body.getVarsIndex();
return vars[idx + RB.VW_];

Variables at the beginning of the VarsList:

  • time
  • kinetic energy
  • potential energy
  • total energy

The set of RigidBodys can change over time via addBody and removeBody. Therefore the set of variables can change accordingly. Removing a RigidBody results in its 6 variables each being marked with the reserved name deleted and those slots in the VarsList are then available for later reuse. Adding a RigidBody either extends the length of the VarsList or reuses some previously deleted slots of variables. But the 6 variables allocated for a RigidBody are guaranteed to be contiguous.

FunctionVariables can be added to a VarsList. Their position in the VarsList remains constant after they are allocated.

Hierarchy (view full)

Implements

Constructors

Properties

bods_: RigidBody[] = []

The RigidBodys in this simulation.

forceLaws_: ForceLaw[] = []

The ForceLaws in this simulation.

potentialOffset_: number = 0

potential energy offset

recentState_: null | number[] = null

While stepping forward in time, stores the previous values of the simulation state variables, so that we can back up in time if a collision is encountered.

showForces_: boolean = false

Whether to add Forces to the SimList so they can be seen.

simList_: SimList = ...

The SimList holds SimObjects so they can be made visible.

simRect_: null | DoubleRect = null

Suggested size for the SimView. This is mainly for tests to communicate with TestViewerApp.

varsList_: VarsList

The variables that determine the state of the simulation; there are six variables for each RigidBody, plus some others for time, energy, etc.

ELASTICITY_SET: string = 'ELASTICITY_SET'

Name of event broadcast from setElasticity.

Methods

  • Add the RigidBody to the simulation and SimList, and add a set of 6 variables for the RigidBody to the VarsList.

    Using FunctionVariable's ensures that the variables on the VarsList have the same values as the RigidBody's (because the FunctionVariables retrieve and store their values in the RigidBody's). There is no need for a separate step to coordinate between the VarsList and the RigidBody's, they are automatically in sync.

    Parameters

    • body: RigidBody

      RigidBody to add to the simulation

    Returns void

  • Adds the ForceLaw to the list of ForceLaws operating in this simulation, if it is not already on the list.

    Parameters

    Returns void

    Throws

    if adding a second DampingLaw or GravityLaw

  • Applies the Force by modifying the array representing rate of change of each variable. The Force specifies which RigidBody it works on so we can figure out which variable rates to modify. If showForces_ is true, adds the Force to the SimList with an immediate expiration time.

    Parameters

    • change: number[]

      vector of rigid body accelerations

    • force: Force

      the Force to be applied

    Returns void

  • Removes all RigidBodys, ForceLaws, most Variables, and clears the SimList. This is used in applications to build a new configuration of RigidBodys. This should give essentially the same state that you would get from making a new RigidBodySim, except for parameters (like gravity) that may have been changed.

    The alternative is to create a new RigidBodySim; that would be 'cleaner' but then you must unhook the old RigidBodySim from all the various user controls and graph and such, and hook up the new one.

    Returns void

  • Creates a PointMass which is displayed as a circle, and adds it to the SimList, for debugging only. The expiration time on temporary SimObjects is set to 'now', so that they are removed right away during the next call to advance().

    Parameters

    • name: string

      name of the SimObject that is created

    • center: GenericVector

      center of the circle

    • radius: number

      radius of the circle

    • Optional expireTime: number

      the time when the DisplayObject will be removed; the default expireTime is 'now'.

    Returns void

  • Creates a ConcreteLine and adds it to the SimList, for debugging only. The expiration time on temporary SimObjects is set to 'now', so that they are removed right away during the next call to advance().

    Parameters

    • name: string

      name of the SimObject that is created

    • pa: Vector

      starting point of the line

    • pb: Vector

      ending point of the line

    • Optional expireTime: number

      the time when the DisplayObject will be removed; the default expireTime is 'now'.

    Returns void

  • 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.

  • Returns string showing current variables of each RigidBody, for debugging.

    Returns string

    string showing current variables of each RigidBody, for debugging.

  • Returns a RigidBody in this simulation by specifying its name or index in the list of RigidBodys.

    Parameters

    • numOrName: string | number

      index in list of RigidBodys or name of the RigidBody (either the English or language-independent version of the name)

    Returns RigidBody

    the RigidBody with the given name or at the given position in the list of RigidBodys

    Throws

    if requesting a non-existing body.

  • Whether to add Forces to the SimList so they can be seen.

    Returns boolean

    whether to add Forces to the SimList so they can be seen

  • Prints the message to console, preceded by the current simulation time. Draws the time in green, the message in black; you can add colors in the message by adding more '%c' symbols in the message string and pass additional colors.

    Parameters

    • message: string

      message to print, optionally with '%c' where color changes are desired

    • Rest ...colors: string[]

      CSS color or background strings, to change the color in the message at points in the message marked by the string '%c'

    Returns void

  • Removes the RigidBody from the simulation and SimList, and removes the corresponding variables from the VarsList.

    Parameters

    • body: RigidBody

      RigidBody to remove from the simulation

    Returns void

  • Removes the ForceLaw from the list of ForceLaws operating in this simulation.

    Parameters

    • forceLaw: ForceLaw

      the ForceLaw to remove

    Returns boolean

    whether the ForceLaw was removed

  • 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 the elasticity of all RigidBodys to this value. Elasticity is used when calculating collisions; a value of 1.0 means perfectly elastic where the kinetic energy after collision is the same as before (extremely bouncy), while a value of 0 means no elasticity (no bounce).

    Broadcasts an 'ELASTICITY_SET' event.

    Parameters

    • value: number

      elasticity to set on all RigidBodys, a number from 0 to 1.

    Returns void

    Throws

    if there are no RigidBodys

  • Sets whether to add Forces to the SimList so they can be seen.

    Parameters

    • value: boolean

      whether to add Forces to the SimList so they can be seen

    Returns void

  • 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