Show:
Module: p5.play
Parent Module: p5.play

Collision Shape Base Class

We have a set of collision shapes available that all conform to a simple interface so that they can be checked against one another using the Separating Axis Theorem.

This base class implements all the required methods for a collision shape and can be used as a collision point with no changes. Other shapes should inherit from this and override most methods.

Constructor

p5.CollisionShape
(
  • center
  • rotation
)

Defined in lib/p5.play.js:4932

Parameters:

  • [center] p5.Vector optional

    (zero if omitted)

  • [rotation] Number optional

    (zero if omitted)

Methods

_getCandidateAxes () Array.
protected

Defined in lib/p5.play.js:5288

Compute candidate separating axes relative to another object. Override this method in subclasses to implement collision behavior.

Returns:

Array.
_getMinRadius
(
  • axis
)
Number
protected

Defined in lib/p5.play.js:5311

Get the shape's minimum radius on any axis for tunneling checks.

Parameters:

Returns:

Number
_getRadiusOnAxis
(
  • axis
)
Number
protected

Defined in lib/p5.play.js:5299

Get this shape's radius (half-width of its projection) along the given axis. Override this method in subclasses to implement collision behavior.

Parameters:

Returns:

Number
_onTransformChanged ()
protected

Defined in lib/p5.play.js:5142

Recalculate cached properties, relevant vectors, etc. when at least one of the shape's transforms changes. The base CollisionShape (and PointCollider) only need to recompute the shape's center, but other shapes may need to override this method and do additional recomputation.

collide
(
  • other
)
p5.Vector

Defined in lib/p5.play.js:5170

Compute the smallest movement needed to move this collision shape out of another collision shape. If the shapes are not overlapping, returns a zero vector to indicate that no displacement is necessary.

Parameters:

Returns:

getBoundingBox () Top: number, bottom: number, left: number, right: number, width: number, height: number

Defined in lib/p5.play.js:5349

Get world-space axis-aligned bounds information for this collision shape. Used primarily for the quadtree.

Returns:

Top: number, bottom: number, left: number, right: number, width: number, height: number:

}

overlap
(
  • other
)
Boolean

Defined in lib/p5.play.js:5243

Check whether this shape overlaps another.

Parameters:

Returns:

Boolean
setParentTransform
(
  • parent
)

Defined in lib/p5.play.js:5118

Update this collider's parent transform, which will in turn adjust its position, rotation and scale in world-space and recompute cached values if necessary. If a Sprite is passed as the 'parent' then a new transform will be computed from the sprite's position/rotation/scale and used.

Parameters:

updateFromSprite
(
  • sprite
)

Defined in lib/p5.play.js:5106

Update this collider based on the properties of a parent Sprite. Descendant classes should override this method to adopt the dimensions of the sprite if getsDimensionsFromSprite is true.

Parameters:

Properties

_center

p5.Vector private

Defined in lib/p5.play.js:4975

The center of the collision shape in world-space.

_localTransform

p5.Transform2D protected

Defined in lib/p5.play.js:4949

Transform of this shape relative to its parent. If there is no parent, this is pretty much the world-space transform. This should stay consistent with _offset, _rotation and _scale properties.

_offset

p5.Vector private

Defined in lib/p5.play.js:4983

The center of the collision shape in local-space; also, the offset of the collision shape's center from its parent sprite's center.

_parentTransform

p5.Transform2D protected

Defined in lib/p5.play.js:4965

Transform of whatever parent object (probably a sprite) this shape is associated with. If this is a free-floating shape, the parent transform will remain an identity matrix.

_rotation

Number private

Defined in lib/p5.play.js:4992

Rotation in radians in local space (relative to parent). Note that this will only be meaningful for shapes that can rotate, i.e. Oriented Bounding Boxes

_scale

p5.Vector private

Defined in lib/p5.play.js:5002

Scale X and Y in local space. Note that this will only be meaningful for shapes that have dimensions (e.g. not for point colliders)

center

p5.Vector

Defined in lib/p5.play.js:5023

The center of the collision shape in world-space. Note: You can set this property with a value in world-space, but it will actually modify the collision shape's local transform.

getsDimensionsFromSprite

Boolean

Defined in lib/p5.play.js:5011

If true, when calling updateFromSprite this collider will adopt the base dimensions of the sprite in addition to adopting its transform. If false, only the transform (position/rotation/scale) will be adopted.

offset

p5.Vector

Defined in lib/p5.play.js:5043

The center of the collision shape in local-space - if this collider is owned by a sprite, the offset of the collider center from the sprite center.

rotation

Number

Defined in lib/p5.play.js:5062

The local-space rotation of the collider, in radians.

scale

p5.Vector

Defined in lib/p5.play.js:5082

The local-space scale of the collider

WORLD_AXES

Array. const static

Defined in lib/p5.play.js:5338

X_AXIS

p5.Vector const static

Defined in lib/p5.play.js:5322

Y_AXIS

p5.Vector const static

Defined in lib/p5.play.js:5330

Static Methods

p5.CollisionShape._getCanididateAxesForShapes
(
  • shape1
  • shape2
)
Array.
private

Defined in lib/p5.play.js:5254

Parameters:

Returns:

Array.