Skip to content

Commit

Permalink
use Vec2Value interface in the api
Browse files Browse the repository at this point in the history
  • Loading branch information
shakiba committed Dec 21, 2023
1 parent 3c63900 commit 4830ce5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
22 changes: 11 additions & 11 deletions src/dynamics/Body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ 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.
*/
angle?: number;
/**
* 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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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));
Expand All @@ -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));
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/dynamics/Joint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;

Expand Down
47 changes: 27 additions & 20 deletions src/dynamics/World.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 = {};
Expand All @@ -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 = {};
Expand Down

0 comments on commit 4830ce5

Please sign in to comment.