# Interface RigidBody

A 2D rigid body with a specified geometry that can experience collisions and contact forces. A RigidBody handles the geometry calculations for intersections and collisions, as well as energy and momentum calculations.

### Non-colliding RigidBodys

There are cases where RigidBodys should not collide with each other. For example, when there is a Joint between two RigidBodys. See addNonCollide and doesNotCollide

The Polygon class has a way of specifying a subset of Edges which do not collide with another RigidBody. See Polygon.setNonCollideEdge.

TO DO how is initialize() method used? It is not private anymore!!!

TO DO getLocalCenterOfMass() should not exist; only used in DisplayShape.

TO DO make sure all these methods and fields are really used and useful.

TO DO testCollisionVertex and testContactVertex could perhaps be made private methods of Polygon; do they need to be methods on RigidBody?

interface RigidBody {
alignTo(p_body, p_world, opt_angle?): void;
bodyToWorld(p_body): Vector;
bodyToWorldTransform(): AffineTransform;
checkCollision(c, body, time): void;
createCanvasPath(context): void;
doesNotCollide(body): boolean;
eraseOldCoords(): void;
getAccuracy(): number;
getAngle(): number;
getAngularVelocity(): number;
getBottomBody(): number;
getBottomWorld(): number;
getBoundsBody(): DoubleRect;
getBoundsWorld(): DoubleRect;
getCenterOfMass(): Vector;
getCentroidBody(): Vector;
getCentroidWorld(): Vector;
getChanged(): boolean;
getDistanceTol(): number;
getDragPoints(): Vector[];
getEdges(): Edge[];
getElasticity(): number;
getExpireTime(): number;
getHeight(): number;
getKineticEnergy(): number;
getLeftBody(): number;
getLeftWorld(): number;
getMass(): number;
getMinHeight(): number;
getName(opt_localized?): string;
getOldCoords(): null | LocalCoords;
getPosition(): Vector;
getRightBody(): number;
getRightWorld(): number;
getSpecialNormalWorld(): null | Vector;
getTopBody(): number;
getTopWorld(): number;
getVarName(index, localized): string;
getVarsIndex(): number;
getVelocity(p_body?): Vector;
getVelocityTol(): number;
getVertexes_(): Vertex[];
getVerticesBody(): Vector[];
getWidth(): number;
getZeroEnergyLevel(): null | number;
isMassObject(): boolean;
momentum(): number[];
nameEquals(name): boolean;
nonCollideEdge(edge): boolean;
printAll(): void;
probablyPointInside(p_body): boolean;
removeNonCollide(bodies): void;
rotateBodyToWorld(v_body): Vector;
rotateWorldToBody(v_world): Vector;
rotationalEnergy(): number;
saveOldCoords(): void;
setAccuracy(value): void;
setAngle(angle): void;
setAngularVelocity(angular_velocity): void;
setCenterOfMass(center): void;
setChanged(): void;
setDistanceTol(value): void;
setDragPoints(dragPts): void;
setElasticity(value): void;
setExpireTime(time): void;
setMass(mass): void;
setMinHeight(minHeight): void;
setPosition(loc_world, angle?): void;
setPositionX(value): void;
setPositionY(value): void;
setVarsIndex(index): void;
setVelocity(velocity_world, angular_velocity?): void;
setVelocityTol(value): void;
setVelocityX(value): void;
setVelocityY(value): void;
setZeroEnergyLevel(height?): void;
similar(obj, opt_tolerance?): boolean;
toStringShort(): string;
translationalEnergy(): number;
worldToBody(p_world): Vector;
}

## Methods

• Adds to set of RigidBodys that do not collide with this body. No collisions or contacts are generated between this body and the given bodies. See doesNotCollide.

#### Parameters

• ##### bodies: RigidBody[]

array of RigidBodys that should not be collided with

#### Returns void

• Moves this body so that a certain point on this body is aligned to the given world coordinates location; optionally sets the angle of this body by rotating around the center of mass `angle` radians counter-clockwise from the body coordinates orientation.

#### Parameters

• ##### p_body: GenericVector

the point on this body to be aligned to the new `p_world` location, in body coordinates

• ##### p_world: GenericVector

the world coordinates location to move the specified `p_body` point to

• ##### `Optional`opt_angle: number

the angle in radians to rotate this body counter-clockwise from 'body coordinates' orientation; if not specified then leave the angle as is.

#### Throws

if this MassObject is immoveable

• Returns the world coordinates of the given body coordinates point, based on current position of this object.

#### Parameters

• ##### p_body: GenericVector

the point, in body coordinates

#### Returns Vector

the point in world coordinates

• Checks if this RigidBody has a collision or contact with another RigidBody, if so adds a new RigidBodyCollision to the list of collisions.

#### Parameters

• ##### c: RigidBodyCollision[]

the list of collisions to add to

• ##### body: RigidBody

the RigidBody to check for collisions with

• ##### time: number

current simulation time

#### Returns void

• Creates the JavaScript Canvas path that represents the shape of this object in the given Canvas context using body coordinates. Note that this calls `CanvasRenderingContext2D.beginPath()` which discards any currently defined path and begins a new one. Also, this concept of 'path' is different from the path of Edges that makes up a Polygon (though the Canvas path is created from those Polygon paths).

#### Parameters

• ##### context: CanvasRenderingContext2D

the CanvasRenderingContext2D to create the path in

#### Returns void

• Returns true if this body does not collide with the given body. See addNonCollide.

#### Parameters

• ##### body: RigidBody

the RigidBody of interest

#### Returns boolean

true if this body does not collide with the given body

• Returns the collision distance accuracy, a fraction between zero and one; when the collision distance is within `accuracy * targetGap` of the target gap distance, then the collision is considered close enough to handle (apply an impulse).

#### Returns number

the collision accuracy, a fraction between 0 (exclusive) and 1 (inclusive)

• Returns the counter-clockwise angle of rotation of this body about its center of mass, in radians, relative to 'body coordinates' orientation.

#### Returns number

the counter-clockwise angle of rotation of this body about its center of mass, in radians, relative to 'body coordinates' orientation

• Returns angular velocity of rotation of this body about its center of mass.

#### Returns number

the angular velocity, in radians/second, with positive meaning counter-clockwise rotation about the body's center of mass

• Returns vertical coordinate of bottom-most point of this body, in body coordinates

#### Returns number

vertical coordinate of bottom-most point of this body, in body coordinates

• Returns vertical coordinate of bottom-most point of this body, based on its current position and orientation, in world coordinates. This is approximate when the body has curved edges; because this looks at all the Vertex's of the body, and curved edges have a series of 'decorated' Vertexes on them which don't capture the exact nature of the geometric curve.

#### Returns number

vertical coordinate of bottom-most point of this body, in world coordinates

• Returns whether this SimObject has changed, and sets the state to "unchanged".

#### Returns boolean

whether this SimObject has changed

• Returns distance tolerance used to determine if this RigidBody is in contact with another RigidBody.

#### Returns number

distance tolerance used to determine if this RigidBody is in contact with another RigidBody

• Returns the locations in body coordinates where a mouse can drag this object

#### Returns Vector[]

the locations in body coordinates where a mouse can drag this object

• Returns the list of edges of this body. WARNING do not alter this list.

#### Returns Edge[]

the list of edges of this body.

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

• Returns the expiration time, when this SimObject should be removed from the SimList. This is intended for temporary SimObjects that illustrate, for example, contact forces or collisions.

#### Returns number

the expiration time, in time frame of the Simulation clock

• Returns horizontal coordinate of left-most point of this body, in body coordinates

#### Returns number

horizontal coordinate of left-most point of this body, in body coordinates

• Returns vertical coordinate of left-most point of this body, based on its current position and orientation, in world coordinates. This is approximate when the body has curved edges; because this looks at all the Vertex's of the body, and curved edges have a series of 'decorated' Vertexes on them which don't capture the exact nature of the geometric curve.

#### Returns number

vertical coordinate of left-most point of this body, in world coordinates

• Returns the minimum height that this body's center of gravity can reach, used for potential energy calculations. Put another way: this is how low the center of gravity of this body can be when resting on the ground, and the ground is at height zero.

#### Returns number

the minimum height this body can reach.

• Name of this SimObject, either the language-independent name for scripting purposes or the localized name for display to user.

The language-independent name should be the same as the English version but capitalized and with spaces and dashes replaced by underscore, see Util.toName, nameEquals.

The name should give an idea of the role of the SimObject in the simulation. This allows us to to treat an object in a special way depending on its name. For example, we might use the name to decide what type of DisplayObject to create to represent the SimObject.

#### Parameters

• ##### `Optional`opt_localized: boolean

`true` means return the localized version of the name; default is `false` which means return the language independent name.

#### Returns string

name of this SimObject

• Returns horizontal coordinate of right-most point of this body, in body coordinates

#### Returns number

horizontal coordinate of right-most point of this body, in body coordinates

• Returns vertical coordinate of right-most point of this body, based on its current position and orientation, in world coordinates. This is approximate when the body has curved edges; because this looks at all the Vertex's of the body, and curved edges have a series of 'decorated' Vertexes on them which don't capture the exact nature of the geometric curve.

#### Returns number

vertical coordinate of right-most point of this body, in world coordinates

• The normal vector (if any) used in the special edge proximity test. When a special edge has been specified, that Edge that takes priority for collision handling, as in a wall object, and this method returns the normal vector to use for special proximity testing. Otherwise this returns `null` and regular proximity testing is done. A normal is a unit-length Vector that is perpendicular to an Edge. This maintains a cache to avoid computational costs. See Special Edge for Proximity Testing

#### Returns null | Vector

normal vector for special edge, in world coordinates, or null when there is no special edge

• Returns vertical coordinate of top-most point of this body, in body coordinates

#### Returns number

vertical coordinate of top-most point of this body, in body coordinates

• Returns vertical coordinate of top-most point of this body, based on its current position and orientation, in world coordinates. This is approximate when the body has curved edges; because this looks at all the Vertex's of the body, and curved edges have a series of 'decorated' Vertexes on them which don't capture the exact nature of the geometric curve.

#### Returns number

vertical coordinate of top-most point of this body, in world coordinates

• Returns the name of the specified variable. For more understandable code, you can use the enum RB instead of these index numbers.

#### Parameters

• ##### index: number

which variable name is desired: 0 = x-position, 1 = x-velocity, 2 = y-position, 3 = y-velocity, 4 = angle, 5 = angular velocity

• ##### localized: boolean

whether to return localized variable name

#### Returns string

the name of the specified variable for this particular body

• Returns the index into the VarsList for this RigidBody's first variable (the x-position). The VarsList contains 6 variables for each RigidBody,

``````0. x-position,
1. x-velocity,
2. y-position,
3. y-velocity,
4. angle,
5. angular velocity
``````

For more understandable code, you can use the enum RB instead of these index numbers.

#### Returns number

the index of the x-position in the VarsList for this body; or `-1` if this body is not in the VarsList.

• Returns velocity of the given point on this object. The point is specified in body coordinates, but the velocity is in world coordinates.

#### Parameters

• ##### `Optional`p_body: GenericVector

the point to find the velocity of, in body coordinates; if undefined, then center of mass is used

#### Returns Vector

the velocity of the given point, in world coordinates

• Returns velocity tolerance used to determine if this RigidBody is in contact with another RigidBody.

Velocity tolerance is set on each RigidBody, but we expect it to be the same for all RigidBodys. ImpulseSim 'owns' the velocity tolerance, it is merely passed along to the RigidBody because it is needed during collision finding and RigidBody has no way of finding ImpulseSim.

Note however that because Scrim is immutable, it always returns zero for velocity tolerance. In this case, use the velocity tolerance of the other non-Scrim RigidBody involved in the collision.

#### Returns number

velocity tolerance used to determine if this RigidBody is in contact with another RigidBody

• Returns the list of Vertexes of this body, for engine2D package use only.

#### Returns Vertex[]

the list of Vertexes of this body.

• Returns the locations of vertices that define a bounding area, in body coordinates. The vertices might lie outside of the body: for example a circle could have vertices at corners of a bounding box.

#### Returns Vector[]

locations of the vertices in body coordinates

• Returns the vertical coordinate where the body has zero potential gravitational energy under standard constant gravity when the body's center of mass is at this vertical coordinate.

#### Returns null | number

the vertical world coordinate where this body has zero potential energy; or null to use the default zero energy level

• Returns moment of inertia about center of mass. This measures how much force is needed to rotate the body about the center of mass. Note that this is the number set via setMomentAboutCM multiplied by the mass of the body.

#### Returns number

moment of inertia about center of mass

• Returns the linear and angular momentum of this body. Angular momentum about a fixed point in space is defined as

``````I_cm vw k + r x m v_cm
``````

where:

``````I_cm = moment about center of mass
vw = angular velocity
k = unit z vector,
r = vector from a fixed point to the center of mass (cm)
m = mass
v_cm = velocity of center of mass
``````

cross product in the plane is `(ax,ay,0) x (bx,by,0) = k(ax by - ay bx)` so we get

``````I_cm w + m (rx vy - ry vx)
``````

take the fixed point to be the origin `(0,0)`, so `(rx,ry)` is center of mass.

#### Returns number[]

the momentum of this body as array containing horizontal, vertical and angular momentum in that order.

• Whether this SimObject has the given name, adjusting for transformation to the language-independent form of the name, as is done by Util.toName.

#### Parameters

• ##### name: string

the English or language-independent version of the name

#### Returns boolean

whether this SimObject has the given name (adjusted to language-independent form)

• Whether this RigidBody cannot collide with an Edge or Vertex of another RigidBody. Returns `true` when passing `null` for the Edge.

#### Parameters

• ##### edge: null | Edge

an Edge of another body, or `null`

#### Returns boolean

true if this body cannot collide with the given Edge; returns true if `edge` is null.

• Prints all edges and Vertexes to console for debugging.

#### Returns void

• Returns true if the given body coords point is probably inside this polygon.

WARNING* For debugging only. Does not work for complex (non-convex) shapes.

#### Parameters

• ##### p_body: Vector

the point in body coords

#### Returns boolean

true if the given body coords point is probably inside this polygon

• Removes from set of RigidBodys that do not collide with this body.

#### Parameters

• ##### bodies: RigidBody[]

array of RigidBodys that should be collided with

#### Returns void

• Rotates a body coordinates vector to its orientation in world coordinates. The vector goes from the origin (0, 0), to the given point in body coordinates. The vector is rotated about the origin by the current angle of this body.

#### Parameters

• ##### v_body: GenericVector

the vector, in body coordinates

#### Returns Vector

the rotated vector in world coordinates.

• Rotates a world coordinates vector to its orientation in body coordinates, the inverse of rotateBodyToWorld method. The vector goes from the origin `(0, 0)`, to the given point in world coordinates. The vector is rotated about the origin by the opposite of the current angle of this body.

#### Parameters

• ##### v_world: GenericVector

the the vector to be rotated, in world coordinates

#### Returns Vector

the rotated vector in body coordinates.

• Returns the rotational energy of this body. Defined to be: `0.5 * momentAboutCM * vw^2` where `vw = ` angular velocity.

#### Returns number

the rotational energy of this body

• Makes an internal copy of the geometry of this RigidBody, which is used for future collision checking. This copy is a record of the last location of this object, so that collision checking can determine how the object moved over the last time step. For example, a small object moving at high velocity can pass through a narrow object in a single time step; there is then no interpenetration of the two objects, but if you use the previous position of the small fast object you can see that it has passed through the narrow object. See getOldCoords, eraseOldCoords.

#### Returns void

• Sets the collision distance accuracy, a fraction between zero and one; when the collision distance is within `accuracy * targetGap` of the target gap distance, then the collision is considered close enough to handle (apply an impulse).

#### Parameters

• ##### value: number

how close in distance to be in order to handle a collision

#### Throws

if value is out of the range 0 to 1, or is exactly zero

• Sets the angle in radians of counter-clockwise rotation of this object around its center of mass. Angle zero draws the object in the same orientation as in body coordinates. Angle `Math.PI/2` rotates the body clockwise 90 degrees from its body coordinates orientation.

#### Parameters

• ##### angle: number

the angle in radians to rotate this object counter-clockwise about its center of mass from 'body coordinates' orientation

#### Returns void

• Sets angular velocity of rotation of this body about its center of mass.

#### Parameters

• ##### angular_velocity: number

the angular velocity, in radians/second, with positive meaning counter-clockwise rotation about the body's center of mass

#### Returns void

• Sets location of center of mass, in body coordinates.

#### Parameters

• ##### center: GenericVector

the position of the center of mass, in body coordinates.

#### Returns void

• Sets distance tolerance to use to determine if this RigidBody is in contact with another RigidBody.

#### Parameters

• ##### value: number

distance tolerance to use to determine if this RigidBody is in contact with another RigidBody

#### Returns void

• Sets the locations where a mouse can drag this object, in body coordinates.

#### Parameters

• ##### dragPts: Vector[]

the locations where a mouse can drag this object, in body coordinates

#### Returns void

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

#### Parameters

• ##### value: number

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

#### Returns void

• Sets the expiration time, when this SimObject should be removed from the SimList. This is intended for temporary SimObjects that illustrate, for example, contact forces or collisions.

#### Parameters

• ##### time: number

the expiration time, in time frame of the Simulation clock

#### Returns void

• Set the mass of this MassObject.

Note that when setting the mass on a MassObject you should also set accordingly the moment of inertia about center of mass, see setMomentAboutCM.

#### Parameters

• ##### mass: number

the mass of this MassObject

#### Returns void

• Sets the minimum height that this body can reach, used for potential energy calculations. That is, how low can the center of gravity of this body go?

#### Parameters

• ##### minHeight: number

the minimum height that this body can reach.

#### Returns void

• Sets the moment of inertia about the center of mass for this body divided by the mass of this body. The moment of inertia, `Icm`, measures how much force is needed to rotate the body about the center of mass. `Icm` depends on the shape of the object and how mass is distributed. For a thin rectangular plate:

``````Icm = mass * (width^2 + height^2) / 12
``````

For a thin circular plate:

``````Icm = mass * radius^2 / 2
``````

Note that momentAboutCM returns the number specified here multiplied by the mass of the body.

#### Parameters

• ##### moment: number

the moment of inertia about the center of mass for this body divided by the mass of this body

#### Returns void

• Moves this body so that the center of mass is at the given world coordinates location; rotates this body counter-clockwise about center of mass from 'body coordinates' orientation by the given angle in radians.

#### Parameters

• ##### loc_world: GenericVector

the location in world coordinates

• ##### `Optional`angle: number

the angle in radians to rotate this body counter-clockwise from 'body coordinates' orientation; if undefined then angle is not changed

#### Returns void

• Moves this body so that the center of mass is at the given world coordinates X (horizontal) location.

#### Parameters

• ##### value: number

the horizontal location in world coordinates

#### Returns void

• Moves this body so that the center of mass is at the given world coordinates Y (vertical) location.

#### Parameters

• ##### value: number

the vertical location in world coordinates

#### Returns void

• Sets the index into the VarsList for this RigidBody's first variable (the x-position). The VarsList contains 6 variables for each RigidBody,

``````0. x-position,
1. x-velocity,
2. y-position,
3. y-velocity,
4. angle,
5. angular velocity
``````

For more understandable code, you can use the enum RB instead of these index numbers.

#### Parameters

• ##### index: number

the index of the x-position in the VarsList for this RigidBody; or `-1` if this RigidBody is not in the VarsList.

#### Returns void

• Set the linear velocity of this objects's center of mass, and (optional) angular velocity of rotation about the objects's center of mass.

#### Parameters

• ##### velocity_world: GenericVector

the velocity in world coordinates/second

• ##### `Optional`angular_velocity: number

the angular velocity, in radians/second, with positive meaning counter-clockwise rotation about the body's center of mass; if undefined, then angular velocity is not changed

#### Returns void

• Sets velocity tolerance to use to determine if this RigidBody is in contact with another RigidBody

#### Parameters

• ##### value: number

velocity tolerance to use to determine if this RigidBody is in contact with another RigidBody

#### Returns void

• Set the linear horizontal velocity of this objects's center of mass.

#### Parameters

• ##### value: number

the horizontal velocity in world coordinates

#### Returns void

• Set the linear vertical velocity of this objects's center of mass.

#### Parameters

• ##### value: number

the vertical velocity in world coordinates

#### Returns void

• Sets the vertical coordinate where the body has zero potential gravitational energy under standard constant gravity when the body's center of mass is at this vertical coordinate.

#### Parameters

• ##### `Optional`height: number

the vertical world coordinate where this body has zero potential energy; `NaN` means to use default level; `undefined` means use the body's current vertical location is used

#### Returns void

• Returns true if the given SimObject is similar to this SimObject for display purposes. SimObjects are similar when they are the same type and nearly the same size and location. Mainly used when showing forces - to avoid adding too many objects to the display. See SimList.getSimilar.

#### Parameters

• ##### obj: SimObject

the SimObject to compare to

• ##### `Optional`opt_tolerance: number

the amount the object components can differ by

#### Returns boolean

true if this SimObject is similar to `obj` for display purposes

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

• Returns the translational energy of this body. Defined to be: `0.5*mass*(vx^2 + vy^2)` where `vx = ` horizontal velocity, and `vy = ` vertical velocity.

#### Returns number

the translational energy of this body

• Returns the body coordinates of the given world coordinates point, based on current position of this object.

#### Parameters

• ##### p_world: GenericVector

the point, in world coordinates

#### Returns Vector

the point in body coordinates

Generated using TypeDoc