Optional
opt_name: stringname of this as a Subject
Function to print collisions, or null to turn off printing collisions.
Private
debugFunction 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).
Protected
initialInitial values.
Private
addAdds 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.
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
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.
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.
the list of collisions to add to
the current array of state variables
the size of the current time step, in seconds
Called at the end of a mouse drag operation, performs whatever action is
appropriate. Only called if startDrag returned true
.
the SimObject being dragged, or null
if no SimObject was found
the location of the mouse in
simulation coordinates of the SimView where simObject
was found, or in the
focus view
if simObject
is null
.
distance from the initial object position to the mouse location at start of drag.
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
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.
the list of current collisions
Optional
opt_totals: CollisionTotalsCollisionTotals object to update with number of collisions handled
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.
the KeyboardEvent that happened
true
means this is a key-down event; false
means a key-up event
the modifier keys down during event
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.
the SimObject being dragged, or null
if no SimObject was found
the location of the mouse in
simulation coordinates of the SimView where simObject
was found, or in the
focus view
if simObject
is null
.
distance from the initial object position (from DisplayObject.getPosition) to the mouse location at start of drag.
Private
moveRemoves 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 a function for printing collisions. The function is called whenever a collision occurs. The function takes two variables: a BlockCollision 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.rightBlock_.getPosition().getX().toFixed(2)+"\t"
+c.leftBlock_.getName()+"\t"
+c.rightBlock_.getName();
t.println(s);
})
the function to print collisions,
or null
to turn off printing collisions
Sets the Terminal object that this simulation can print data into.
the Terminal object that this simulation can print data into.
Protected
setSets the VarsList for this simulation.
the VarsList to use in this simulation
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
.
the SimObject that is nearest to the mouse drag coordinates,
or null
if no SimObject was found
the location of the mouse in
simulation coordinates of the SimView where simObject
was found, or in the
focus view
if simObject
is null
.
distance from the initial object position (from DisplayObject.getPosition) to the mouse location at start of drag
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
the modifier keys down during event
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'}}
a minimal string representation of this object.
Generated using TypeDoc
Simulation of two blocks moving horizontally between walls, with collisions between the blocks and walls. One of the blocks is connected to a wall with a spring. The order of objects left to right is: left wall, block 1, block 2, right wall.
Variables and Parameters
Origin at right edge of left wall. Variables:
text vars[0] = block 1 position vars[1] = block 1 velocity vars[2] = block 2 position vars[3] = block 2 velocity
Parameters:text R = rest length S1 = left end of spring S2 = right end of spring (same as x?) len = current length of spring = x - S1.getX() L = how much spring is stretched from rest length L = len - R = x - S1.getX() - R k = spring constant b = damping constant
Equations Of MotionSee also https://www.myphysicslab.com/collideSpring.html.
Spring force and damping look like this:
text F = -k L -b v = -k (x - S1.getX() - R) - b v = m v'
So equations of motion are:text x' = v v' = -(k/m)(x - S1.getX() - R) -(b/m) v
Collision HandlingWhen colliding with a wall, just reverse the velocity.
When the two blocks collide, adjust the velocities of particles as follows:
To find new velocity of block 1, find the velocity in the center of mass frame and reflect it. This works out to
-v1 + 2 vcm
. Here's the derivation:Add back
vcm
to get to laboratory frame:Same derivation applies for block 2.
To Do
Elasticity parameter.