Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/excaliburjs/Excalibur. 🎮 Your friendly TypeScript 2D game engine for the web 🗡️ excaliburjs.com
excalibur excaliburjs game-development game-engine game-framework gamedev games html5-canvas typescript
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295import { BoundingBox } from '../bounding-box';import { CollisionJumpTable } from './collision-jump-table';import type { CollisionContact } from '../detection/collision-contact';import { PolygonCollider } from './polygon-collider';import { EdgeCollider } from './edge-collider';
import { Projection } from '../../math/projection';import type { LineSegment } from '../../math/line-segment';import { Vector } from '../../math/vector';import type { Ray } from '../../math/ray';import { Color } from '../../color';import { Collider } from './collider';
import { ClosestLineJumpTable } from './closest-line-jump-table';import type { ExcaliburGraphicsContext } from '../../graphics/context/excalibur-graphics-context';import type { Transform } from '../../math/transform';import { AffineMatrix } from '../../math/affine-matrix';import { BodyComponent } from '../index';import type { RayCastHit } from '../detection/ray-cast-hit';import { approximatelyEqual } from '../../math/util';
export interface CircleColliderOptions { /** * Optional pixel offset to shift the circle relative to the collider, by default (0, 0). */ offset?: Vector; /** * Required radius of the circle */ radius: number;}
/** * This is a circle collider for the excalibur rigid body physics simulation */export class CircleCollider extends Collider { /** * Position of the circle relative to the collider, by default (0, 0). */ public offset: Vector = Vector.Zero;
private _globalMatrix: AffineMatrix = AffineMatrix.identity();
public get worldPos(): Vector { return this._globalMatrix.getPosition(); }
private _naturalRadius: number;
private _radius: number | undefined; /** * Get the radius of the circle */ public get radius(): number { if (this._radius) { return this._radius; } const tx = this._transform; const scale = tx?.globalScale ?? Vector.One; // This is a trade off, the alternative is retooling circles to support ellipse collisions return (this._radius = this._naturalRadius * Math.min(scale.x, scale.y)); }
/** * Set the radius of the circle */ public set radius(val: number) { const tx = this._transform; const scale = tx?.globalScale ?? Vector.One; // This is a trade off, the alternative is retooling circles to support ellipse collisions this._naturalRadius = val / Math.min(scale.x, scale.y); this._localBoundsDirty = true; this._radius = val; }
private _transform: Transform;
constructor(options: CircleColliderOptions) { super(); this.offset = options.offset || Vector.Zero; this.radius = options.radius || 0; this._globalMatrix.translate(this.offset.x, this.offset.y); }
/** * Returns a clone of this shape, not associated with any collider */ public clone(): CircleCollider { return new CircleCollider({ offset: this.offset.clone(), radius: this.radius }); }
/** * Get the center of the collider in world coordinates */ public get center(): Vector { return this._globalMatrix.getPosition(); }
/** * Tests if a point is contained in this collider */ public contains(point: Vector): boolean { const pos = this._transform?.pos ?? this.offset; const distance = pos.distance(point); if (distance <= this.radius) { return true; } return false; }
/** * Casts a ray at the Circle collider and returns the nearest point of collision * @param ray */ public rayCast(ray: Ray, max: number = Infinity): RayCastHit | null { // https://en.wikipedia.org/wiki/Intersection_(geometry)#A_line_and_a_circle const c = this.center; const dir = ray.dir; const orig = ray.pos;
const u = c.sub(orig);
const u1 = dir.scale(u.dot(dir)); const u2 = u.sub(u1);
const d = u2.magnitude;
if (d > this.radius) { return null; } else { // tangent case let toi = 0; if (approximatelyEqual(d, this.radius, 0.0001)) { toi = -dir.dot(orig.sub(c)); if (toi > 0 && toi < max) { const point = ray.getPoint(toi); return { point, normal: point.sub(c).normalize(), collider: this, body: this.owner?.get(BodyComponent), distance: toi } satisfies RayCastHit; } return null; } else { // two point const discriminant = Math.sqrt(Math.pow(dir.dot(orig.sub(c)), 2) - Math.pow(orig.sub(c).distance(), 2) + Math.pow(this.radius, 2));
const toi1 = -dir.dot(orig.sub(c)) + discriminant; const toi2 = -dir.dot(orig.sub(c)) - discriminant;
const positiveToi: number[] = []; if (toi1 >= 0) { positiveToi.push(toi1); }
if (toi2 >= 0) { positiveToi.push(toi2); }
const minToi = Math.min(...positiveToi); if (minToi <= max) { const point = ray.getPoint(minToi); return { point, normal: point.sub(c).normalize(), collider: this, body: this.owner?.get(BodyComponent), distance: minToi } satisfies RayCastHit; } return null; } } }
public getClosestLineBetween(shape: Collider): LineSegment { if (shape instanceof CircleCollider) { return ClosestLineJumpTable.CircleCircleClosestLine(this, shape); } else if (shape instanceof PolygonCollider) { return ClosestLineJumpTable.PolygonCircleClosestLine(shape, this).flip(); } else if (shape instanceof EdgeCollider) { return ClosestLineJumpTable.CircleEdgeClosestLine(this, shape).flip(); } else { throw new Error(`Polygon could not collide with unknown CollisionShape ${typeof shape}`); } }
/** * @inheritdoc */ public collide(collider: Collider): CollisionContact[] { if (collider instanceof CircleCollider) { return CollisionJumpTable.CollideCircleCircle(this, collider); } else if (collider instanceof PolygonCollider) { return CollisionJumpTable.CollideCirclePolygon(this, collider); } else if (collider instanceof EdgeCollider) { return CollisionJumpTable.CollideCircleEdge(this, collider); } else { throw new Error(`Circle could not collide with unknown CollisionShape ${typeof collider}`); } }
/** * Find the point on the collider furthest in the direction specified */ public getFurthestPoint(direction: Vector): Vector { return this.center.add(direction.normalize().scale(this.radius)); }
/** * Find the local point on the shape in the direction specified * @param direction */ public getFurthestLocalPoint(direction: Vector): Vector { const dir = direction.normalize(); return dir.scale(this.radius); }
/** * Get the axis aligned bounding box for the circle collider in world coordinates */ public get bounds(): BoundingBox { return this.localBounds.transform(this._globalMatrix); }
private _localBoundsDirty = true; private _localBounds: BoundingBox; /** * Get the axis aligned bounding box for the circle collider in local coordinates */ public get localBounds(): BoundingBox { if (this._localBoundsDirty) { this._localBounds = new BoundingBox(-this._naturalRadius, -this._naturalRadius, +this._naturalRadius, +this._naturalRadius); this._localBoundsDirty = false; } return this._localBounds; }
/** * Get axis not implemented on circles, since there are infinite axis in a circle */ public get axes(): Vector[] { return []; }
/** * Returns the moment of inertia of a circle given it's mass * https://en.wikipedia.org/wiki/List_of_moments_of_inertia */ public getInertia(mass: number): number { return (mass * this.radius * this.radius) / 2; }
/* istanbul ignore next */ public update(transform: Transform): void { this._transform = transform; const globalMat = transform.matrix ?? this._globalMatrix; globalMat.clone(this._globalMatrix); this._globalMatrix.translate(this.offset.x, this.offset.y); this._radius = undefined; }
/** * Project the circle along a specified axis */ public project(axis: Vector): Projection { const scalars = []; const point = this.center; const dotProduct = point.dot(axis); scalars.push(dotProduct); scalars.push(dotProduct + this.radius); scalars.push(dotProduct - this.radius); return new Projection(Math.min.apply(Math, scalars), Math.max.apply(Math, scalars)); }
public debug(ex: ExcaliburGraphicsContext, color: Color, options?: { lineWidth: number }) { const { lineWidth } = { ...{ lineWidth: 1 }, ...options }; const tx = this._transform; const scale = tx?.globalScale ?? Vector.One; const rotation = tx?.globalRotation ?? 0; const pos = tx?.globalPos ?? Vector.Zero; ex.save(); ex.translate(pos.x, pos.y); ex.rotate(rotation); ex.scale(scale.x, scale.y); ex.debug.drawCircle(this.offset ?? Vector.Zero, this._naturalRadius, Color.Transparent, color, lineWidth); ex.restore(); }}