blaze-2d
Version:
A fast and simple WebGL 2 2D game engine written in TypeScript
185 lines (184 loc) • 5.55 kB
TypeScript
import { vec2 } from "gl-matrix";
import Object2D from "../object2d";
/**
* Represents an object in 2D world space that can experience physics.
*/
export default class PhysicsObject extends Object2D {
/**
* The gravitational froce applied to the object.
*/
gravity: vec2;
/**
* Mass of object in kg.
*
* When set to 0 the object's mass is effectively infinite.
*/
private mass;
/**
* The inverse of the object's mass (1 / mass).
*/
private inverseMass;
/**
* Elasticity/bounciness
*
* This value should be between 0 and 1 for best results.
*/
restitution: number;
/**
* Coefficient of static friction.
*
* This value should be between 0 and 1 for best results.
*
* @see [Static And Kinetic Friction](https://www.geeksforgeeks.org/static-and-kinetic-friction/)
*/
staticFriction: number;
/**
* Coefficient of dynamic/kinetic friction.
*
* This value should be between 0 and 1 for best results.
*
* @see [Static And Kinetic Friction](https://www.geeksforgeeks.org/static-and-kinetic-friction/)
*/
dynamicFriction: number;
/**
* The net force applied to the object in newtons.
*/
force: vec2;
/**
* The object's velocity in world space units (metres).
*/
velocity: vec2;
/**
* The damping applied to the object's linear velocity every physics update.
*/
airFriction: number;
/**
* The object's torque in newtons, can be thought of as rotational force.
*/
torque: number;
/**
* The object's angular velocity in radians per second.
*/
angularVelocity: number;
/**
* The object's moment of inertia (resistance to rotation).
*
* When set to 0 the object's inertia is effectively infinite.
*/
private inertia;
/**
* The inverse of the object's inertia (1 / inertia).
*/
private inverseInertia;
/**
* The damping applied to the object's angular velocity every physics update.
*/
angularDamping: number;
/**
* Wether or not the object should take gravity from the physics world.
*/
takesGravity: boolean;
/**
* Wether or not the object's x position is locked.
*
* When true the object's x position will not be affected by collisions or dynamics.
*/
lockXAxis: boolean;
/**
* Wether or not the object's y position is locked.
*
* When true the object's y position will not be affected by collisions or dynamics.
*/
lockYAxis: boolean;
/**
* Wether or not the object can rotate.
*
* When true the object's rotation will not be affected by collisions or dynamics.
*/
lockRotation: boolean;
/**
* Creates a {@link PhysicsObject} with a mass and restitution.
*
* @param mass The mass of the object
* @param restitution The restituion (bounciness) of the object
*/
constructor(mass?: number, restitution?: number);
protected setupEvents(): void;
/**
* Adds a force to the object's current total force vector.
*
* If no contact point is provided then the force will be applied at the
* object's centre, generating 0 torque.
*
* @param force The force vector to apply
* @param contact The local position on the body to apply the force at
*/
applyForce(force: vec2, contact?: vec2): void;
private forceVec;
/**
* Applies a force to the object at an angle.
*
* The direction vector is calculated from the unit y+ vector.
*
* If no contact point is provided then the force will be applied at the
* object's centre, generating 0 torque.
*
* @param force The force to apply in newtons
* @param angle The angle at which to apply the force in radians (world space)
* @param contact The local position on the body to apply the force at
*/
applyForceAtAngle(force: number, angle: number, contact?: vec2): void;
/**
* Applies an impulse to the physics object.
*
* This will apply an instantaneous change to the object's angular and linear velocity.
*
* @param impulse The impulse to apply
* @param contactVector A vector from the object's center to the contact point
*/
applyImpulse(impulse: vec2, contactVector: vec2): void;
/**
* Sets the mass of the object in kg.
*
* Also computes the inverse mass.
*
* **NOTE: Setting the mass to 0 will act as infinite mass.**
*
* @param mass The objects new mass
*/
setMass(mass: number): void;
/**
* Gets the mass of the object.
*
* @returns The mass of the object
*/
getMass(): number;
/**
* Gets the inverse mass of the object (1 / mass).
*
* @returns The inverse mass of the object
*/
getInverseMass(): number;
/**
* Sets the moment of inertia of the object.
*
* Also computes the inverse inertia.
*
* **NOTE: Setting the inertia to 0 will act as infinite inertia.**
*
* @param inertia The objects new inertia
*/
setInertia(inertia: number): void;
/**
* Gets the moment of inertia of the object.
*
* @returns The inertia of the object
*/
getInertia(): number;
/**
* Gets the inverse moment of inertia of the object (1 / inertia).
*
* @returns The inverse inertia of the object
*/
getInverseInertia(): number;
}