blaze-2d
Version:
A fast and simple WebGL 2 2D game engine written in TypeScript
162 lines (161 loc) • 4.71 kB
TypeScript
import { vec2 } from "gl-matrix";
import { CollisionResult } from "./collider/collider";
import CollisionObject from "./collisionObject";
interface Edge {
p0: vec2;
p1: vec2;
e: vec2;
max: vec2;
normal?: vec2;
}
export interface ContactPoint {
point: vec2;
normal: vec2;
depth: number;
contactA?: vec2;
contactB?: vec2;
tangent?: vec2;
massNormal?: number;
massTangent?: number;
bias?: number;
impulseNormal?: number;
impulseTangent?: number;
impulseNormalPosition?: number;
}
/**
* Information about a collision which has occured between two objects.
*/
export default class Manifold {
/**
* An object in the collision.
*/
a: CollisionObject;
/**
* Another object in the collision.
*/
b: CollisionObject;
/**
* The penetration depth of the collision.
*/
depth: number;
/**
* The collision normal, should always point from **a** to **b**.
*/
normal: vec2;
/**
* The penetration vector, this is the collision normal scaled by the penetration depth.
*/
penetration: vec2;
/**
* Minimum restitution between **a** and **b**.
*/
epsilon: number;
/**
* static friction scalar.
*/
sf: number;
/**
* dynamic friction scalar.
*/
df: number;
/**
* The contact points of the collision.
*/
contactPoints: ContactPoint[];
/**
* store used edges [inc, ref]
*/
edges: Edge[];
positionImpulse: {
a: vec2;
b: vec2;
};
isDead: boolean;
/**
* Creates a {@link Manifold} describing a collision between **a** and **b** in detail.
*
* @param a First collision object
* @param b Second collision object
* @param collision {@link CollisionResult} from collision test
* @param gravity The collision world's gravity vector
* @param delta The time since the last update
*/
constructor(a: CollisionObject, b: CollisionObject, collision: CollisionResult, gravity: vec2, delta: number);
update(m: Manifold): void;
/**
* Merge this manifold with the given manifold and contact points.
*
* @param m The manifold to merge with
* @param contacts The manifold's new contact points
*/
private mergeManifold;
/**
* Compares two contact points using a distance heuristic to determine wether they should be considered the same contact.
*
* @param c1 A contact point
* @param c2 A contact point
* @returns If the 2 contact points match
*/
private compareContacts;
/**
* Marks the manifold as dead and decrements the objects involved's `totalContacts` counts.
*/
kill(): void;
/**
* Calculates the position impulse for objects `a` and `b` in the manifold.
*
* @see [MatterJS Position Solving](https://github.com/liabru/matter-js/blob/master/src/collision/Resolver.js)
*
* @param delta The time since the last update
*/
solvePositionImpulse(delta: number): void;
/**
* Precompute some additional information about the contact points for impulse resolution.
*
* Calculates and applies accumulative impulse.
*
* @param delta The time since the last udpate
*/
preStepImpulse(delta: number): void;
/**
* Calculates the contact points of the collision.
*
* @see [Dyn4j Contact Points](https://dyn4j.org/2011/11/contact-points-using-clipping/)
*
* @returns The points of contact for the collision
*/
private calculateContactPoints;
/**
* Finds the best edge of a {@link CollisionObject} in a given direction.
*
* The best edge is defined as the edge most perpendicular to the given direction.
*
* @param obj The collision object to calculate the edge for
* @param direction The direction in which to calculate the edge
* @returns The best edge of `obj` for the direction given
*/
private bestEdge;
/**
* Clips the edge points (p0, p1) if they are past **o** along the direction.
*
* @param p0 The first point to clip
* @param p1 The second point to clip
* @param direction The direction to clip in
* @param o The vector to clip past
*/
private clipPoints;
/**
* Translates the manifold's incident edge by the given vector.
*
* @param v The vector to translate by
*/
translateIncEdge(v: vec2): void;
/**
* Translates the provided edge by the given vector.
*
* @param edge The edge to translate
* @param v The vector to translate by
*/
private translateEdge;
}
export {};