Optional
opt_name: stringname of this Subject
Protected
bods_The RigidBodys in this simulation.
Protected
forceThe ForceLaws in this simulation.
Private
potentialpotential energy offset
Private
recentWhile 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.
Protected
showWhether to add Forces to the SimList so they can be seen.
Protected
simThe SimList holds SimObjects so they can be made visible.
Protected
simSuggested size for the SimView. This is mainly for tests to communicate with TestViewerApp.
Protected
varsThe variables that determine the state of the simulation; there are six variables for each RigidBody, plus some others for time, energy, etc.
Static
ELASTICITY_Name of event broadcast from setElasticity.
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.
RigidBody to add to the simulation
Adds the ForceLaw to the list of ForceLaws operating in this simulation, if it is not already on the list.
the ForceLaw to add
if adding a second DampingLaw or GravityLaw
Adds the given Observer to this Subject's list of Observers, so that the Observer
will be notified of changes in this Subject. An Observer may call Subject.addObserver
during its observe
method.
the Observer to add
Adds the Parameter to the list of this Subject's available Parameters.
the Parameter to add
if a Parameter with the same name already exists.
Protected
applyApplies 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.
vector of rigid body accelerations
the Force to be applied
Notifies all Observers that this Subject has changed by calling observe on each Observer.
An Observer may call addObserver or removeObserver during its observe
method.
a SubjectEvent with information relating to the change
Notifies all Observers that the Parameter with the given name has changed by calling observe on each Observer.
the language-independent or English name of the Parameter that has changed
if there is no Parameter with the given name
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.
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().
name of the SimObject that is created
center of the circle
radius of the circle
Optional
expireTime: numberthe time when the DisplayObject will be removed; the default expireTime is 'now'.
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.
the current array of state variables (input),
corresponding to the state at getTime() + timeStep
array of change rates for each variable (output), all values are zero on entry.
the current time step (might be zero)
null
if the evaluation succeeds, otherwise an object relating to the
error that occurred. The change
array contains the output results.
Returns a RigidBody in this simulation by specifying its name or index in the list of RigidBodys.
index in list of RigidBodys or name of the RigidBody (either the English or language-independent version of the name)
the RigidBody with the given name or at the given position in the list of RigidBodys
if requesting a non-existing body.
Protected
getReturns whether broadcasting is enabled for this Subject. See setBroadcast.
whether broadcasting is enabled for this Subject
Returns the current EnergyInfo for this system.
an EnergyInfo object representing the current energy of this system.
Returns the ParameterBoolean with the given name.
the language-independent or English name of the ParameterBoolean
the ParameterBoolean with the given name
if there is no ParameterBoolean with the given name
Returns the ParameterNumber with the given name.
the language-independent or English name of the ParameterNumber
the ParameterNumber with the given name
if there is no ParameterNumber with the given name
Returns the ParameterString with the given name.
the language-independent or English name of the ParameterString
the ParameterString with the given name
if there is no ParameterString with the given name
Returns the suggested size for the SimView. This is mainly for tests to communicate with test/TestViewerApp.TestViewerApp.
suggested size for the SimView
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.
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'
Removes the RigidBody from the simulation and SimList, and removes the corresponding variables from the VarsList.
RigidBody to remove from the simulation
Removes the ForceLaw from the list of ForceLaws operating in this simulation.
the ForceLaw to remove
whether the ForceLaw was removed
Removes the Observer from this Subject's list of Observers. An Observer may
call removeObserver
during its observe
method.
the Observer to detach from list of Observers
Removes the Parameter from the list of this Subject's available Parameters.
the Parameter to remove
Sets the Simulation back to its initial conditions, see saveInitialState, and calls modifyObjects. Broadcasts event named 'RESET'.
Restores the Simulation state that was saved with saveState.
Saves the current variables and time as the initial state, so that this initial state can be restored with reset. Broadcasts event named 'INITIAL_STATE_SAVED'.
Saves the current state of the Simulation, so that we can back up to this state later on. The state is defined mainly by the set of Simulation variables, see getVarsList, but can include other data. This state is typically used for collision detection as the before collision state, see CollisionSim.findCollisions.
Protected
setSets 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.
whether this Subject should broadcast events
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.
elasticity to set on all RigidBodys, a number from 0 to 1.
if there are no RigidBodys
Sets the suggested size for the SimView. This is mainly for tests to communicate with test/TestViewerApp.TestViewerApp.
the suggested size for the SimView
Sets the Terminal object that this simulation can print data into.
the Terminal object that this simulation can print data into.
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'}}
a minimal string representation of this object.
Generated using TypeDoc
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:
2D Physics Engine Overview
The math and physics underlying RigidBodySim, ImpulseSim and ContactSim are described on the myPhysicsLab website.
Parameters Created
SHOW_FORCES
, see setShowForcesEvents Broadcast
All the Parameters are broadcast when their values change. In addition:
ELASTICITY_SET
, see setElasticity.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:
x
horizontal world coords position of center of massx'
horizontal velocity of center of mass. AKAvx
y
vertical world coords position of center of massy'
vertical velocity of center of mass. AKAvy
w
angle of rotation from body coordinates in radians with positive rotation being counter-clockwise. Calledw
, becausew
looks like the greek letter ω (omega) which is often used for angles in math.w'
angular velocity. AKAvw
.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:
Variables at the beginning of the VarsList:
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.