# Class CollideSpringSim

Simulation of one to three blocks moving freely in one dimension, with springs attached to the blocks, and walls on either end.

## Variables

1. time
2. kinetic energy
3. potential energy
4. total energy
5. block 1 position
6. block 1 velocity
7. block 2 position
8. block 2 velocity
9. block 3 position
10. block 3 velocity

## History

This was originally created to investigate collision schemes for the rigid body simulation, like serial or simultaneous collision handling. One idea for how to handle collisions is to insert springs at each collision point, and I tried a version of this in RigidBodySim. But the collisions were not behaving the way I expected. So I made this simulation to check how a simplified collision with stiff springs behaves. Indeed, this simulation shows that these spring based collisions are far from the 'ideal' collision behavior. That 'ideal' collision behavior includes things like:

1. if block A hits stationary block B of equal mass, then block A should be stationary, and block B is moving with same velocity.

2. if block A hits stationary blocks B and C (where B and C are motionless in resting contact), then block C should be the only one moving after the collision and A and B should be in resting contact.

My conclusion was that spring forces were not the right way to go for handling collisions in the rigid body simulation. However, I've learned some things from this simulation since then:

• extremely short springs (short rest length) requires very high stiffness. Otherwise, the blocks just pass thru. I think it is because a spring takes some time/distance for the force to operate and if moving too fast for the length then there is not enough time/distance. You also have to use short time step.

• high stiffness (60) with short times step (0.001) and a small gap (0.1) between blocks/springs somewhat satisfies the ideal collision behaviors described above. This is with low mass of 0.1, and no damping.

## To Do

Add AdaptiveStepSolver to choices for DiffEqSolver; does it work better in terms of having constant energy? Otherwise, the stiff springs seem to give energy fluctuations with regular step size of 0.025; if you reduce to a very small time step of 0.0005, then the energy is fairly constant. But that's wasteful of using many tiny steps during most of the time when the springs aren't engaging.

## Properties

dragIdx_: number = -1

index of the block being dragged, or -1 when no drag is happening

dragSpring_: null | Spring = null

the spring dragging the block

initialState_: null | number[] = null

Initial values.

mouse_: PointMass

object that represents mouse position while dragging. Note that we don't add the mouse object to the SimList and therefore don't make a DisplayObject for it.

START_MIDDLE: 0 = 0

Constant that indicates 'start in middle' configuration.

START_ON_WALL: 1 = 1

Constant that indicates 'start on wall' configuration.

## Methods

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

#### Returns void

• Adds the Parameter to the list of this Subject's available Parameters.

#### Throws

if a Parameter with the same name already exists.

• Notifies all Observers that the Parameter with the given name has changed by calling observe on each Observer.

#### Parameters

• ##### name: string

the language-independent or English name of the Parameter that has changed

#### Throws

if there is no Parameter with the given name

• Rebuilds the simulation from scratch with the given number of blocks, starting position, and gaps.

#### Parameters

• ##### numBlocks: number

number of moveable blocks to make

• ##### startPosition: number

starting position of blocks: 0 = middle, 1 = on-wall

• ##### startGap: number

gap between objects in starting position

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

• 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

• Returns the Parameter with the given name.

#### Parameters

• ##### name: string

the language-independent or English name of the Parameter

#### Returns Parameter

the Parameter with the given name

#### Throws

if there is no Parameter with the given name

• Returns the ParameterBoolean with the given name.

#### Parameters

• ##### name: string

the language-independent or English name of the ParameterBoolean

#### Returns ParameterBoolean

the ParameterBoolean with the given name

#### Throws

if there is no ParameterBoolean with the given name

• Returns the ParameterNumber with the given name.

#### Parameters

• ##### name: string

the language-independent or English name of the ParameterNumber

#### Returns ParameterNumber

the ParameterNumber with the given name

#### Throws

if there is no ParameterNumber with the given name

• Returns the ParameterString with the given name.

#### Parameters

• ##### name: string

the language-independent or English name of the ParameterString

#### Returns ParameterString

the ParameterString with the given name

#### Throws

if there is no ParameterString with the given name

• Returns the current Simulation time.

#### Returns number

the current Simulation time.

#### Throws

if there is no time variable for the simulation

#### Returns string

• 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 string[]

• 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

• Removes the Observer from this Subject's list of Observers. An Observer may call `removeObserver` during its `observe` method.

#### Parameters

• ##### observer: Observer

the Observer to detach from list of Observers

#### Returns void

• Removes the Parameter from the list of this Subject's available Parameters.

#### Parameters

• ##### parameter: Parameter

the Parameter to remove

#### 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

• Set damping

damping

#### Returns void

• Set spring resting length

#### Parameters

• ##### value: number

spring resting length

#### Returns void

• Set mass of atoms

#### Parameters

• ##### value: number

mass of atoms

#### Returns void

• Set spring damping

#### Parameters

• ##### value: number

spring damping

#### Returns void

• Sets spring stiffness

#### Parameters

• ##### value: number

spring stiffness

#### Returns void

• Sets the Terminal object that this simulation can print data into.

#### Parameters

• ##### terminal: null | Terminal

the Terminal object that this simulation can print data into.

#### Returns void

• Sets the VarsList for this simulation.

#### Parameters

• ##### varsList: VarsList

the VarsList to use in this simulation

#### 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