UNPKG

blaze-2d

Version:

A fast and simple WebGL 2 2D game engine written in TypeScript

116 lines (115 loc) 3.57 kB
import { mat2, vec2 } from "gl-matrix"; import CollisionObject from "../collisionObject"; export interface ConstraintOptions { a: CollisionObject; anchorA: vec2; b: CollisionObject; anchorB: vec2; } /** * Represents a distance constraint between two bodies or a body and a point. * * The constraint points are in local space of their body. * * If the constraint is between a body and a point then the given point is in world space. */ export default abstract class Constraint { a: CollisionObject; anchorA: vec2; rotA: number; b: CollisionObject; anchorB: vec2; rotB: number; point: vec2; errorBias: number; maxBias: number; maxForce: number; /** * Creates a new {@link Constraint} between two bodies. * * @param a The first body * @param b The second body */ constructor(a: CollisionObject, b: CollisionObject); /** * Creates a new {@link Constraint} with the given options. * * @param opts The constraint options */ constructor(opts: ConstraintOptions); /** * Creates a new {@link Constraint} between a body and a point. * * @param a The body to constrain * @param point A point in world space */ constructor(a: CollisionObject, point: vec2); /** * Prepares for constraint solving. * * @param dt The time since the last update */ abstract preSolve(dt: number): void; /** * Solves the constraint. * * @param dt The time since the last update */ abstract solve(dt: number): void; /** * Performs post solve operations. * * @param dt The time since the last update */ abstract postSolve(dt: number): void; /** * Determines wether or not the constraint is between a body and a point. * * @returns Wether or not the constraint is between a body and a point. */ isBodyToPoint(): boolean; protected applyImpulses(impulse: number, direction: vec2): void; protected applyImpulses(impulse: vec2): void; /** * Updates the constraint's anchor points to match the rotations of their bodies. */ protected updateAnchors(): void; /** * Calculate the relative velocity of the bodies in the constraint. * * @param pointA The anchor point on body a * @param pointB The anchor point on body b * @returns The relative velocity between body a and b or body a and the anchor point */ protected calculateRelativeVelocity(pointA: vec2, pointB: vec2): vec2; /** * Calculates the velocity bias coefficient. * * @param dt The time since the last update */ protected biasCoef(dt: number): number; /** * Computes the k scalar value for the given body. * * @param body The body to calculate for * @param r The anchor point on the body * @param n The constraint's delta normal * @returns The k scalar value for the given body */ protected kScalarBody(body: CollisionObject, r: vec2, n: vec2): number; /** * Computes the k scalar value for the constraint. * * @param n The constraint's delta normal * @returns The k scalar value for the constraint */ protected kScalar(n: vec2): number; /** * Computes the k tensor matrix for the constraint. * * @see [Chipmunk2D kTensor](https://github.com/slembcke/Chipmunk2D/blob/master/include/chipmunk/chipmunk_private.h#L229) * * @returns k tensor matrix */ protected kTensor(): mat2; }