Abstract
the 'primary' body which typically has a Vertex or Edge involved in the collision
the 'normal' body which typically has an Edge involved in the collision that defines the normal vector for the collision
whether this is a bilateral constraint which can both push and pull.
Private
accuracy_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).
true if the normal object's edge is curved
true if the 'primary' object's edge is curved
for debugging, unique code tells where this was generated
Private
detecteddistance between objects when first detected, pre-backup
Private
detectedsimulation time that collision was detected
Private
detectednormal velocity when collision was detected, pre-backup.
distance between objects; negative = penetration
Protected
distancedistance tolerance is used to decide when bodies are touching.
Private
elasticity_elasticity of this collision, from 0 to 1.
Private
estimate_estimate of time when half-gap distance happens
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
amount of impulse applied during collision
whether this is a bilateral constraint which can both push and pull
Private
mustIndicates 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)
true = normal is constant
Private
normalrelative 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
radius of curvature at impact1, for primary body; negative means concave
radius of curvature at impact1, for normal body; negative means concave
Private
targetdesired 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
updatetime corresponding to last update
Protected
velocityvelocity tolerance used to determine if an object is in contact with another object. See ImpulseSim.getVelocityTol.
Returns true if this represents a bilateral constraint which can both push and pull; for example a Joint between two objects.
true if this is a bilateral constraint
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.
regard as 'close enough' Collisions that have smaller distance than distance accuracy would normally allow
true if close enough to the point when this Collision can be handled.
Abstract
getReturns 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.
elasticity used when calculating collisions, a number from 0 to 1.
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.
point of impact on normal body, in global coords, or null
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.
the normal body involved in the collision
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.
relative normal velocity between the two bodies at the point of contact.
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.
the primary body involved in the collision
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.
the velocity vector of this collision
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.
vector from center of mass of normal body to either point of impact or to center of circular edge, in world coords
Whether this collision involves the given RigidBody
the RigidBody of interest
whether collision involves the given RigidBody
Abstract
hasWhether this collision involves the given edge. If given edge is null, then always returns false.
the Edge of interest
whether collision involves the given Edge
Abstract
hasWhether this collision involves the given vertex
the Vertex of interest
whether collision involves the given Vertex
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.
true
if this represents a collision state
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.
when this collision is detected
if the detected time has been previously set
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.
true if this Collision needs to be resolved
Abstract
similarReturns 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.
the other collision
true if the two collisions are possibly the same collision
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.
the current simulation time
Private
updateUpdate 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
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 primary body, 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.
the normal body, has an Edge which defines the normal vector. For a Connector like Joint, the normal is defined by the Connector not by an Edge.
The normal vector 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.
The normal relative velocity is the velocity between the bodies at the point of collision, but only in the direction of the normal vector.
The R vector goes from the center of mass to the impact point, on the primary body. Similarly, the R2 vector is on the normal body from its center of mass to the impact point.
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 EDITTED
The 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 theU
vector, for the normal body it is called theU2
vector.For curved edges we use the
U
andU2
vectors instead of theR
andR2
vectors.The
U
andU2
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 samex
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 DO guess: the only things really needed are normal and impact point (plus of course the two bodies). Wait, also things like ballObject, ballNormal.
TO DO guess: 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?)