From 4830ce50cf35647a1a3173a26ff731b372297e4a Mon Sep 17 00:00:00 2001 From: Ali Shakiba Date: Wed, 20 Dec 2023 22:31:20 -0800 Subject: [PATCH] use Vec2Value interface in the api --- src/dynamics/Body.ts | 22 ++++++++++---------- src/dynamics/Joint.ts | 4 ++-- src/dynamics/World.ts | 47 +++++++++++++++++++++++++------------------ 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/dynamics/Body.ts b/src/dynamics/Body.ts index b2974c66..640e155b 100644 --- a/src/dynamics/Body.ts +++ b/src/dynamics/Body.ts @@ -62,7 +62,7 @@ export interface BodyDef { * The world position of the body. Avoid creating bodies at the * origin since this can lead to many overlapping shapes. */ - position?: Vec2; + position?: Vec2Value; /** * The world angle of the body in radians. */ @@ -70,7 +70,7 @@ export interface BodyDef { /** * The linear velocity of the body's origin in world co-ordinates. */ - linearVelocity?: Vec2; + linearVelocity?: Vec2Value; angularVelocity?: number; /** * Linear damping is use to reduce the linear velocity. The @@ -573,7 +573,7 @@ export class Body { * @param position The world position of the body's local origin. * @param angle The world rotation in radians. */ - setTransform(position: Vec2, angle: number): void { + setTransform(position: Vec2Value, angle: number): void { _ASSERT && console.assert(this.isWorldLocked() == false); if (this.isWorldLocked() == true) { return; @@ -623,7 +623,7 @@ export class Body { return this.m_xf.p; } - setPosition(p: Vec2): void { + setPosition(p: Vec2Value): void { this.setTransform(p, this.m_sweep.a); } @@ -666,7 +666,7 @@ export class Body { * * @param worldPoint A point in world coordinates. */ - getLinearVelocityFromWorldPoint(worldPoint: Vec2): Vec2 { + getLinearVelocityFromWorldPoint(worldPoint: Vec2Value): Vec2 { const localCenter = Vec2.sub(worldPoint, this.m_sweep.c); return Vec2.add(this.m_linearVelocity, Vec2.crossNumVec2(this.m_angularVelocity, localCenter)); @@ -677,7 +677,7 @@ export class Body { * * @param localPoint A point in local coordinates. */ - getLinearVelocityFromLocalPoint(localPoint: Vec2): Vec2 { + getLinearVelocityFromLocalPoint(localPoint: Vec2Value): Vec2 { return this.getLinearVelocityFromWorldPoint(this.getWorldPoint(localPoint)); } @@ -686,7 +686,7 @@ export class Body { * * @param v The new linear velocity of the center of mass. */ - setLinearVelocity(v: Vec2): void { + setLinearVelocity(v: Vec2Value): void { if (this.m_type == STATIC) { return; } @@ -902,7 +902,7 @@ export class Body { * @param point The world position of the point of application. * @param wake Also wake up the body */ - applyForce(force: Vec2, point: Vec2, wake: boolean = true): void { + applyForce(force: Vec2Value, point: Vec2Value, wake: boolean = true): void { if (this.m_type != DYNAMIC) { return; } @@ -964,7 +964,7 @@ export class Body { * @param point The world position of the point of application. * @param wake Also wake up the body */ - applyLinearImpulse(impulse: Vec2, point: Vec2, wake: boolean = true): void { + applyLinearImpulse(impulse: Vec2Value, point: Vec2Value, wake: boolean = true): void { if (this.m_type != DYNAMIC) { return; } @@ -1150,14 +1150,14 @@ export class Body { /** * Get the corresponding world point of a local point. */ - getWorldPoint(localPoint: Vec2): Vec2 { + getWorldPoint(localPoint: Vec2Value): Vec2 { return Transform.mulVec2(this.m_xf, localPoint); } /** * Get the corresponding world vector of a local vector. */ - getWorldVector(localVector: Vec2): Vec2 { + getWorldVector(localVector: Vec2Value): Vec2 { return Rot.mulVec2(this.m_xf.q, localVector); } diff --git a/src/dynamics/Joint.ts b/src/dynamics/Joint.ts index e66665d1..cd962afb 100644 --- a/src/dynamics/Joint.ts +++ b/src/dynamics/Joint.ts @@ -22,7 +22,7 @@ * SOFTWARE. */ -import type { Vec2 } from '../common/Vec2'; +import type { Vec2, Vec2Value } from '../common/Vec2'; import type { Body } from './Body'; import { TimeStep } from "./Solver"; @@ -200,7 +200,7 @@ export abstract class Joint { /** * Shift the origin for any points stored in world coordinates. */ - shiftOrigin(newOrigin: Vec2): void {} + shiftOrigin(newOrigin: Vec2Value): void {} abstract initVelocityConstraints(step: TimeStep): void; diff --git a/src/dynamics/World.ts b/src/dynamics/World.ts index 5695de39..b814b04e 100644 --- a/src/dynamics/World.ts +++ b/src/dynamics/World.ts @@ -23,7 +23,7 @@ */ import { options } from '../util/options'; -import { Vec2 } from '../common/Vec2'; +import { Vec2, Vec2Value } from '../common/Vec2'; import { BroadPhase } from '../collision/BroadPhase'; import { Solver, ContactImpulse, TimeStep } from './Solver'; import { Body, BodyDef } from './Body'; @@ -38,28 +38,33 @@ import { Manifold } from "../collision/Manifold"; /** @internal */ const _CONSTRUCTOR_FACTORY = typeof CONSTRUCTOR_FACTORY === 'undefined' ? false : CONSTRUCTOR_FACTORY; -/** - * @prop gravity [{ x : 0, y : 0}] - * @prop allowSleep [true] - * @prop warmStarting [true] - * @prop continuousPhysics [true] - * @prop subStepping [false] - * @prop blockSolve [true] - * @prop velocityIterations [8] For the velocity constraint solver. - * @prop positionIterations [3] For the position constraint solver. - */ export interface WorldDef { + /** [default: { x : 0, y : 0}] */ gravity?: Vec2; + + /** [default: true] */ allowSleep?: boolean; + + /** [default: true] */ warmStarting?: boolean; + + /** [default: true] */ continuousPhysics?: boolean; + + /** [default: false] */ subStepping?: boolean; + + /** [default: true] */ blockSolve?: boolean; + + /** @internal [8] For the velocity constraint solver. */ velocityIterations?: number; + + /** @internal [3] For the position constraint solver. */ positionIterations?: number; } -/** @internal */ const WorldDefDefault: WorldDef = { +/** @internal */ const DEFAULTS: WorldDef = { gravity : Vec2.zero(), allowSleep : true, warmStarting : true, @@ -134,11 +139,13 @@ export class World { this.s_step = new TimeStep(); - if (def && Vec2.isValid(def)) { + if (!def) { + def = {}; + } else if (Vec2.isValid(def)) { def = { gravity: def as Vec2 }; } - def = options(def, WorldDefDefault) as WorldDef; + def = options(def, DEFAULTS) as WorldDef; this.m_solver = new Solver(this); @@ -272,8 +279,8 @@ export class World { /** * Change the global gravity vector. */ - setGravity(gravity: Vec2): void { - this.m_gravity = gravity; + setGravity(gravity: Vec2Value): void { + this.m_gravity.set(gravity); } /** @@ -458,7 +465,7 @@ export class World { * * @param newOrigin The new origin with respect to the old origin */ - shiftOrigin(newOrigin: Vec2): void { + shiftOrigin(newOrigin: Vec2Value): void { _ASSERT && console.assert(this.m_locked == false); if (this.m_locked) { return; @@ -501,7 +508,7 @@ export class World { * Warning: This function is locked during callbacks. */ createBody(def?: BodyDef): Body; - createBody(position: Vec2, angle?: number): Body; + createBody(position: Vec2Value, angle?: number): Body; // tslint:disable-next-line:typedef /** @internal */ createBody(arg1?, arg2?) { _ASSERT && console.assert(this.isLocked() == false); @@ -523,7 +530,7 @@ export class World { } createDynamicBody(def?: BodyDef): Body; - createDynamicBody(position: Vec2, angle?: number): Body; + createDynamicBody(position: Vec2Value, angle?: number): Body; // tslint:disable-next-line:typedef /** @internal */ createDynamicBody(arg1?, arg2?) { let def: BodyDef = {}; @@ -538,7 +545,7 @@ export class World { } createKinematicBody(def?: BodyDef): Body; - createKinematicBody(position: Vec2, angle?: number): Body; + createKinematicBody(position: Vec2Value, angle?: number): Body; // tslint:disable-next-line:typedef createKinematicBody(arg1?, arg2?) { let def: BodyDef = {};