@rpgjs/physic
Version:
A deterministic 2D top-down physics library for RPG, sandbox and MMO games
71 lines • 2.76 kB
TypeScript
import { AABB } from '../core/math/AABB';
import { Vector2 } from '../core/math/Vector2';
import { Entity } from '../physics/Entity';
import { Collider, CollisionInfo, ContactPoint } from './Collider';
import { Ray, RaycastHit } from './Ray';
/**
* Configuration for polygon colliders
*/
export interface PolygonConfig {
/** Vertices for a single convex polygon (local space) */
vertices?: Vector2[];
/** If true, `vertices` are guaranteed convex and counter-clockwise */
isConvex?: boolean;
/**
* Convex parts for concave shapes (list of convex polygons, local space)
* If provided, `vertices` is ignored.
*/
parts?: Vector2[][];
}
/**
* Weak registry to attach polygon configurations to entities
*/
export declare const entityToPolygonConfig: WeakMap<Entity, PolygonConfig>;
/**
* Assigns a polygon collider configuration to an existing entity.
* The entity will use this collider when `createCollider` is invoked.
*
* @param entity - Target entity
* @param config - Polygon configuration
* @example
* ```typescript
* const e = new Entity({ position: { x: 0, y: 0 } });
* assignPolygonCollider(e, { vertices: [new Vector2(-1, -1), new Vector2(1, -1), new Vector2(1, 1), new Vector2(-1, 1)], isConvex: true });
* ```
*/
export declare function assignPolygonCollider(entity: Entity, config: PolygonConfig): void;
/**
* Polygon collider implementation (convex via SAT; concave via convex parts)
*
* Design notes (in English):
* - Vertices are defined in local space (centered on entity.position) and rotated by entity.rotation at query time.
* - Concave shapes are supported by providing pre-decomposed convex `parts`. The collider iterates pairs of convex parts.
* - Narrow-phase uses SAT (Separating Axis Theorem) on all candidate axes from both polygons.
* - Contact point is approximated from the overlap normal and polygon centroid for performance and stability.
*
* @example
* ```typescript
* const e = new Entity({ position: { x: 0, y: 0 } });
* assignPolygonCollider(e, { vertices: [new Vector2(0, 0), new Vector2(2, 0), new Vector2(1, 2)], isConvex: true });
* const collider = new PolygonCollider(e);
* ```
*/
export declare class PolygonCollider implements Collider {
private entity;
private convexParts;
constructor(entity: Entity);
getEntity(): Entity;
getBounds(): AABB;
getContactPoints(other: Collider): ContactPoint[];
testCollision(other: Collider): CollisionInfo | null;
private testPolygonAABB;
private testPolygonCircle;
private testPolygonPolygon;
/**
* @inheritdoc
*/
raycast(ray: Ray): RaycastHit | null;
private rayCastSegment;
private getWorldParts;
}
//# sourceMappingURL=PolygonCollider.d.ts.map