`Abstract`

accuracy_
ballNormal
ballObject
creator
detectedDistance_
detectedTime_
detectedVelocity_
distance
distanceTol_
elasticity_
estimate_
force
impact1
impact2
impulse
joint
mustHandle_
normal
normalBody
normalFixed
normalVelocity_
normal_dt
primaryBody
radius1
radius2
targetGap_
updateTime_
velocityTol_

bilateral
checkConsistent
closeEnough
contact
distanceToHalfGap
getAcceleration
getClassName
getConnector
getDetectedTime
getDistance
getElasticity
getEstimatedTime
getImpact1
getImpact2
getImpulse
getLateralVelocity
getNormalBody
getNormalVelocity
getPerpNormal
getPrimaryBody
getR1
getR2
getRelativeVelocity
getU1
getU2
getVelocity
hasBody
hasEdge
hasVertex
illegalState
isColliding
isTouching
needsHandling
setDetectedTime
setNeedsHandling
similarTo
toString
updateCollision
updateEstimatedTime

- new
Rigid (body, normalBody, joint): RigidBodyCollisionBody Collision #### Parameters

##### body: RigidBody

the 'primary' body which typically has a Vertex or Edge involved in the collision

##### normalBody: RigidBody

the 'normal' body which typically has an Edge involved in the collision that defines the normal vector for the collision

##### joint: boolean

whether this is a bilateral constraint which can both push and pull.

#### Returns RigidBodyCollision

`Private`

accuracy_accuracy_: number

Collision distance accuracy: when the collision distance is within accuracy of the target gap distance, then the collision is close enough to be able to handle it (apply an impulse).

ballNormal : boolean = false

true if the normal object's edge is curved

ballObject : boolean = false

true if the 'primary' object's edge is curved

creator: string = ''

for debugging, unique code tells where this was generated

`Private`

detecteddetectedDistance_ : number = NaN

distance between objects when first detected, pre-backup

`Private`

detecteddetectedTime_ : number = NaN

simulation time that collision was detected

`Private`

detecteddetectedVelocity_ : number = NaN

normal velocity when collision was detected, pre-backup.

distance: number = NaN

distance between objects; negative = penetration

`Protected`

distancedistanceTol_ : number

distance tolerance is used to decide when bodies are touching.

`Private`

elasticity_elasticity_: number

elasticity of this collision, from 0 to 1.

`Private`

estimate_estimate_: number = NaN

estimate of time when half-gap distance happens

force: number = NaN

amount of force applied at a contact point

point of impact, in global coords

second impact point needed for Rope because the impact points are far apart. OPTIONAL point of impact on normalBody, in global coords

impulse: number = NaN

amount of impulse applied during collision

joint: boolean

whether this is a bilateral constraint which can both push and pull

`Private`

mustmustHandle_ : boolean = false

Indicates this is a collision that needs to be handled

normal pointing outward from normalObj, in world coords

object corresponding to the normal (its edge defines the normal vector)

normalFixed : boolean

true = normal is constant

`Private`

normalnormalVelocity_ : number = NaN

relative normal velocity at impact point; negative=colliding, positive = separating. Cached value: it is invalid when NaN.

derivative of normal vector with respect to time

'primary' object whose corner or edge is colliding

radius1: number = NaN

radius of curvature at impact1, for primary body; negative means concave

radius2: number = NaN

radius of curvature at impact1, for normal body; negative means concave

`Private`

targettargetGap_ : number

desired target gap where collision is handled (we try to back up to time when collision distance is this amount). Can be zero for joints.

`Private`

updateupdateTime_ : number = NaN

time corresponding to last update

`Protected`

velocityvelocityTol_ : number

velocity tolerance used to determine if an object is in contact with another object. See ImpulseSim.getVelocityTol.

- bilateral(): boolean
Returns true if this represents a bilateral constraint which can both push and pull; for example a Joint between two objects.

#### Returns boolean

true if this is a bilateral constraint

- close
Enough (allowTiny): boolean Whether close enough to the point when this Collision can be handled. The

`allowTiny`

parameter exists because of cases where a small distance collision cannot be backed-up in time to get near to the preferred target 'half gap' distance.#### Parameters

##### allowTiny: boolean

regard as 'close enough' Collisions that have smaller distance than distance accuracy would normally allow

#### Returns boolean

true if close enough to the point when this Collision can be handled.

`Abstract`

get- get
Elasticity (): number Returns the elasticity used when calculating collisions; a value of 1.0 means perfect elasticity where the kinetic energy after collision is the same as before (extremely bouncy), while a value of 0 means no elasticity (no bounce). A collision uses the lesser elasticity value of the two bodies involved.

#### Returns number

elasticity used when calculating collisions, a number from 0 to 1.

- get
Impact2 (): null | Vector Returns point of impact on normal body, in global coords. For example, this is needed for Rope because the impact points are far apart. Often null when only Vertex.isEndPoint is needed.

#### Returns null | Vector

point of impact on normal body, in global coords, or null

- get
Normal (): RigidBodyBody Returns the normal body involved in the collision, which defines the normal vector. The classic situation is that a vertex on the primary body is colliding into an edge on the normal body, but there are many variations on this.

#### Returns RigidBody

the normal body involved in the collision

- get
Normal (): numberVelocity Returns the relative normal velocity based on current velocity of the bodies. Negative velocity means the objects moving towards each other, positive velocity means they are moving apart.

#### Returns number

relative normal velocity between the two bodies at the point of contact.

- get
Primary (): RigidBodyBody Returns the primary body involved in the collision. The primary body does not define the normal. The classic situation is that a vertex on the primary body is colliding into an edge on the normal body, but there are many variations on this.

#### Returns RigidBody

the primary body involved in the collision

- get
Relative (): VectorVelocity Returns the difference in velocity of the two impact points of the collision based on current velocity of the bodies.

`let V = velocity of center of mass (CM); let R = distance vector CM to contact point let w = angular velocity w x R = (0, 0, w) x (Rx, Ry, 0) = (-w Ry, w Rx, 0) velocity of corner = V + w x R = (Vx - w Ry, Vy + w Rx, 0) relative velocity = Vab = Va + wa x Ra - Vb - wb x Rb`

For curved edge we use the

`U`

vector (from center of mass to edge's circle center) instead of`R`

vector (from center of mass to point of impact). Because what matters is not the motion of the individual point but instead the entire curved edge. Consider that for a ball with center of mass at center of the circle, rotation doesn't change the distance at all.#### Returns Vector

the velocity vector of this collision

- get
U2 (): Vector Returns vector from center of mass of normal body to either point of impact or to center of circular edge in world coords. Uses the second impact point if appropriate.

#### Returns Vector

vector from center of mass of normal body to either point of impact or to center of circular edge, in world coords

- has
Body (body): boolean Whether this collision involves the given RigidBody

#### Parameters

##### body: RigidBody

the RigidBody of interest

#### Returns boolean

whether collision involves the given RigidBody

`Abstract`

has- has
Edge (edge): boolean Whether this collision involves the given edge. If given edge is null, then always returns false.

#### Parameters

##### edge: null | Edge

the Edge of interest

#### Returns boolean

whether collision involves the given Edge

`Abstract`

has- has
Vertex (v): boolean Whether this collision involves the given vertex

#### Parameters

##### v: Vertex

the Vertex of interest

#### Returns boolean

whether collision involves the given Vertex

- is
Colliding (): boolean Returns true if this represents a collision state, generally when two objects are interpenetrating. The collision search mechanism implemented by AdvanceStrategy.advance operates to set the simulation at a time very close to but just before any Collision is happening, see getEstimatedTime.

#### Returns boolean

`true`

if this represents a collision state

- set
Detected (time): voidTime Stores the time when this collision was detected, stores the current distance and velocity as the detected distance and detected velocity, and estimates when the collision occurred.

#### Parameters

##### time: number

when this collision is detected

#### Returns void

#### Throws

if the detected time has been previously set

- set
Needs (needsHandling): voidHandling Mark this Collision as one that

*needs handling*because it is has caused the collision engine to backup in time in order to resolve this Collision. This is useful because after backing up in time, a Collision may no longer report itself as isColliding.#### Parameters

##### needsHandling: boolean

true if this Collision needs to be resolved

#### Returns void

`Abstract`

similar- similar
To (c): boolean Returns whether this collision could be the same as another collision. Often there are several collisions found at a single location by the various collision detection mechanisms, and this is used when deciding which collision of those to keep.

#### Parameters

##### c: RigidBodyCollision

the other collision

#### Returns boolean

true if the two collisions are possibly the same collision

- update
Collision (time): void Updates the information in the collision to reflect current position and velocity of bodies. Changes the impact point to be the nearest point between the bodies (as long as this point is reasonably close to the original impact point). Then update the normal, R vectors, etc.

This is used when handling collisions because the collisions are found post-collision, but are handled pre-collision. Therefore, we need to update the information to correspond to the pre-collision arrangement of the bodies.

Doing this fixes inaccurate collisions; for example, a ball that hits a wall at an angle would wrongly acquire spin if the collision were not updated to the current pre-collision information.

Assumes that the bodies have been updated for their current location, by for example RigidBodySim.modifyObjects.

#### Parameters

##### time: number

the current simulation time

#### Returns void

`Private`

update- update
Estimated (time, doUpdate): voidTime Update the estimated time of collision using both pre-backup and post-backup information and a calculus model of constant acceleration.

`Derivation of the estimate: t1 = time after backup t2 = time before backup time interval of h = t2 - t1 d1 = distance at t1 = distance v1 = velocity at t1 = normalVelocity d2 = distance at t2 = detectedDistance_ v2 = velocity at t2 = detectedVelocity_ assume constant acceleration of a = (v2 - v1)/h In the following, t is 0 at t1. velocity v(t) = integral(a dt) = v1 + a t distance d(t) = integral(v1 + a t dt) = d1 + v1 t + a t^2/2 Now, find time corresponding to distance = targetGap targetGap = d1 + v1 t + (a/2) t^2 Quadratic equation in t 0 = (d1 - targetGap) + v1 t + (a/2) t^2 t = [-v1 +/- sqrt(v1^2 - 4 (a/2) (d1 - targetGap)) ]/(2 a/2) t = [-v1 +/- sqrt(v1^2 - 2 a (d1 - targetGap)) ]/a`

#### Parameters

##### time: number

##### doUpdate: boolean

#### Returns void

Generated using TypeDoc

RigidBodyCollision holds data related to a collision or resting contact between two RigidBodys. The data includes:

Other data concerns the geometry of the bodies in relation to the collision point – things like the curvature of the an edge, or distance from center of mass to the collision point. Some data exists only for certain sub-classes of RigidBodyCollision.

Most of the data in RigidBodyCollision is filled in after it is created, but some data is updated as the collision handling process proceeds. Many of the fields (properties) of RigidBodyCollision are 'package private' meaning that any code in the

`myphysicslab.lab.engine2D`

package can modify the field directly. This avoids having to make dozens of getter/setter methods for those fields.See explanations at 2D Physics Engine Overview.

## Terminology

The two RigidBodys involved in the collision are referred to as follows:

the

, has either a Vertex or Edge involved in the collision. For a Connector like Joint there is a connection point somewhere on the body instead of at a Vertex or Edge.primary bodythe

, has an Edge which defines the normal vector. For a Connector like Joint, the normal is defined by the Connector not by an Edge.normal bodyThe

is used to find the distance and velocity of the collision. The distance between the bodies is measured in the direction of the normal vector.normal vectorThe

is the velocity between the bodies at the point of collision, but only in the direction of the normal vector.normal relative velocityThe

goes from the center of mass to theR vector, on the primary body. Similarly, theimpact pointis on the normal body from its center of mass to the impact point.R2 vector## Distance of Collision

The point of collision, or 'impact point', is where the two bodies are touching or penetrating. This 'point' is actually two different points, one on the primary body and one on the normal body. Ideally at the moment of collision these are at the same point in space, but in practice they are always somewhat apart.

The distance between the two bodies is the distance between the two impact points in the direction of the normal vector. Mathematically we can define the distance as follows:

If distance is positive there is a gap between the objects. If distance is negative, then the objects are interpenetrating.

## Target Gap

When bodies are interpenetrating, the physics engine gets 'stuck'. To prevent this, we aim to back up in time to a moment just before the collision when the objects are separated by a small distance called the

target gap. See Seek to Half-Gap Distance for the full explanation.The

`Collision.closeEnough`

method returns true when the collision distance is close to the target gap distance, within the tolerance given by RigidBody.getAccuracy.## Collision Handling Notes

TO BE EDITTEDThe following is actually super useful for reading the collision handling code

A colliding contact that might be handled comes in several flavors. Any contact exists in one of these zones, depending on its distance

I also have some diagrams from around May 16, 2016 that makes the above very clear.

## Update State After Backing Up In Time

The collision handling scheme used by CollisionAdvance results in backing up in time from the post-collision state to the pre-collision state. This is done to avoid having RigidBodys being in an ambiguous illegal interpenetrating state.

A consequence of this is that the RigidBodyCollision we are handling will have its data from the near-future post-collision state. The method updateCollision updates the RigidBodyCollision to reflect the current pre-collision state after backing up in time.

The

`mustHandle`

flag remembers those RigidBodyCollisions that were penetrating in the post-collision state, before the backup in time occurred. This is needed because those RigidBodyCollisions might otherwise indicate that they do not need to be handled: they have negative distance (penetrating) in the post-collision state, but positive distance (non-penetrating) in the pre-collision state.## The U Vector

When a RigidBody has a curved edge involved in the collision, the

`U`

vector is vector from body's center of mass to center of the circular edge. For the primary body this is called the`U`

vector, for the normal body it is called the`U2`

vector.For curved edges we use the

`U`

and`U2`

vectors instead of the`R`

and`R2`

vectors.The

`U`

and`U2`

vectors are used in finding the contact force needed because a smooth curved edge works differently than a sharp pointed corner. The normal distance (and therefore normal velocity) between a straight edge and a circle is related to the movement of the circle's center – rotation of the circle about the center is irrelevant. This is different to a sharp corner where the movement of the point of the corner is what is important.This is relevant for finding contact forces which are applied over time. For an instantaneous collision impulse this is not important because the bodies immediately move apart.

See the paper Curved Edge Physics paper by Erik Neumann for modifications to contact forces when curved edges are involved.

## Equivalence of Using R or U Vector For Normal Velocity

Here we show that you get the same result whether using the R or U vector in getNormalVelocity.

Suppose you have a circular body striking a horizontal infinite mass floor; let the circle have an offset center of mass, so that U and R are different.

In the picture, you can see that

`rax = uax`

, because the normal at the impact point goes thru the center of the circle, and so both U and R have the same`x`

component. The situation would be the same when the normal is not`(0, 1)`

. In the general case, you are finding the length of R (or U) that is orthogonal to the normal, and again these are the same for U and R because the normal at the impact point goes right thru the center of the circle.TO DOguess: the only things really needed are normal and impact point (plus of course the two bodies). Wait, also things like ballObject, ballNormal.TO DOguess: some things like`r1, r2, u1, u2, radius1, radius2`

can all be calculated; they are only stored in the RBC for convenience to avoid re-calculating. (would it be better to do that calculating in one place?)