Compare commits

...

12 Commits

Author SHA1 Message Date
Oliver Zell
abdab5557e use modern ES syntax 2021-11-27 16:56:35 +01:00
Oliver Zell
9e5045c020 remove overloads in Rot 2021-11-27 14:48:57 +01:00
Oliver Zell
1021a89ce3 remove overloads in Mat22 and Mat33 2021-11-27 14:29:52 +01:00
Oliver Zell
8969f04cbb remove overloads in Vec3 2021-11-27 14:21:03 +01:00
Oliver Zell
3a51871c1d remove overloads in Transform 2021-11-27 14:15:56 +01:00
Oliver Zell
7bfa12ffea remove overloads in Vec2 2021-11-27 14:02:27 +01:00
Oliver Zell
eb09c3c24d add decorator implementation 2021-11-27 10:16:43 +01:00
Oliver Zell
42c35c0ac4 rename transformer to factoryConstructorTransformer 2021-11-26 19:58:32 +01:00
Oliver Zell
c4bc16309e add includeFactoryConstructors option to build config 2021-11-26 19:55:38 +01:00
Oliver Zell
f5804ad2f4 add factoryConstructor decorator 2021-11-25 21:50:18 +01:00
Oliver Zell
3cbb8c5b0d apply factory pattern through ts transformer 2021-11-24 22:02:57 +01:00
Oliver Zell
6ef6d16606 restructure typescript transformers 2021-11-21 15:59:26 +01:00
44 changed files with 834 additions and 922 deletions

16
package-lock.json generated
View File

@ -10345,9 +10345,9 @@
}
},
"node_modules/rollup": {
"version": "2.47.0",
"resolved": "https://registry.npmjs.org/rollup/-/rollup-2.47.0.tgz",
"integrity": "sha512-rqBjgq9hQfW0vRmz+0S062ORRNJXvwRpzxhFXORvar/maZqY6za3rgQ/p1Glg+j1hnc1GtYyQCPiAei95uTElg==",
"version": "2.60.1",
"resolved": "https://registry.npmjs.org/rollup/-/rollup-2.60.1.tgz",
"integrity": "sha512-akwfnpjY0rXEDSn1UTVfKXJhPsEBu+imi1gqBA1ZkHGydUnkV/fWCC90P7rDaLEW8KTwBcS1G3N4893Ndz+jwg==",
"dev": true,
"bin": {
"rollup": "dist/bin/rollup"
@ -10356,7 +10356,7 @@
"node": ">=10.0.0"
},
"optionalDependencies": {
"fsevents": "~2.3.1"
"fsevents": "~2.3.2"
}
},
"node_modules/rollup-plugin-babel": {
@ -21025,12 +21025,12 @@
}
},
"rollup": {
"version": "2.47.0",
"resolved": "https://registry.npmjs.org/rollup/-/rollup-2.47.0.tgz",
"integrity": "sha512-rqBjgq9hQfW0vRmz+0S062ORRNJXvwRpzxhFXORvar/maZqY6za3rgQ/p1Glg+j1hnc1GtYyQCPiAei95uTElg==",
"version": "2.60.1",
"resolved": "https://registry.npmjs.org/rollup/-/rollup-2.60.1.tgz",
"integrity": "sha512-akwfnpjY0rXEDSn1UTVfKXJhPsEBu+imi1gqBA1ZkHGydUnkV/fWCC90P7rDaLEW8KTwBcS1G3N4893Ndz+jwg==",
"dev": true,
"requires": {
"fsevents": "~2.3.1"
"fsevents": "~2.3.2"
}
},
"rollup-plugin-babel": {

View File

@ -6,36 +6,56 @@ import filesize from 'rollup-plugin-filesize';
import typescript from 'rollup-plugin-ts';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import declarationTransformer from './declarationTransformer';
import factoryConstructorTransformer from './transformers/factoryConstructorTransformer.js';
import licenseBanner from './license';
function buildTransformerPipeline(...transformers) {
transformers = transformers.map(t => t({}))
return {
before: transformers
.map(t => t.before)
.filter(Boolean),
after: transformers
.map(t => t.after)
.filter(Boolean),
afterDeclarations: transformers
.map(t => t.afterDeclarations)
.filter(Boolean),
}
}
export default [
{
src: 'src/index.ts',
dest: 'dist/planck.js',
minimize: false,
declaration: true,
includeFactoryConstructors: false,
},
{
src: 'src/index.ts',
dest: 'dist/planck.min.js',
minimize: true,
declaration: false,
},
{
src: 'testbed/index.ts',
dest: 'dist/planck-with-testbed.js',
minimize: false,
declaration: true,
},
{
src: 'testbed/index.ts',
dest: 'dist/planck-with-testbed.min.js',
minimize: true,
declaration: false,
}
// {
// src: 'src/index.ts',
// dest: 'dist/planck.min.js',
// minimize: true,
// declaration: false,
// includeFactoryConstructors: false,
// },
// {
// src: 'testbed/index.ts',
// dest: 'dist/planck-with-testbed.js',
// minimize: false,
// declaration: true,
// includeFactoryConstructors: false,
// },
// {
// src: 'testbed/index.ts',
// dest: 'dist/planck-with-testbed.min.js',
// minimize: true,
// declaration: false,
// includeFactoryConstructors: false,
// }
].map(options => {
const config = {
input: options.src,
@ -44,6 +64,7 @@ export default [
file: options.dest,
format: 'umd',
sourcemap: true,
generatedCode: 'es2015'
},
plugins: [
replace({
@ -62,36 +83,9 @@ export default [
...resolvedConfig,
declaration: options.declaration
}),
transformers: {
afterDeclarations: [
declarationTransformer({
classes: [
'Vec2',
'Vec3',
'Rot',
'Transform',
'AABB',
'World',
'BoxShape',
'CircleShape',
'ChainShape',
'EdgeShape',
'PolygonShape',
'DistanceJoint',
'FrictionJoint',
'GearJoint',
'MotorJoint',
'MouseJoint',
'PrismaticJoint',
'PulleyJoint',
'RevoluteJoint',
'RopeJoint',
'WeldJoint',
'WheelJoint',
]
})
]
},
transformers: buildTransformerPipeline(
factoryConstructorTransformer({ enabled: options.includeFactoryConstructors })
),
}),
babel({
runtimeHelpers: true,

View File

@ -65,10 +65,10 @@ describe('Math', function(): void {
r = Vec2.dot(v, new Vec2(2, 3));
expect(r).equal(13);
r = Vec2.cross(v, new Vec2(2, 3));
r = Vec2.crossNumberVec2(v, new Vec2(2, 3));
expect(r).equal(0);
r = Vec2.cross(v, 5);
r = Vec2.crossNumberVec2(v, 5);
expect(r.x).equal(15);
expect(r.y).equal(-10);

View File

@ -25,6 +25,7 @@
import common from '../util/common';
import Math from '../common/Math';
import Vec2 from '../common/Vec2';
import factoryConstructor from '../decorators/factoryConstructor';
const _DEBUG = typeof DEBUG === 'undefined' ? false : DEBUG;
@ -51,25 +52,22 @@ export interface RayCastOutput {
fraction: number;
}
@factoryConstructor
export default class AABB {
lowerBound: Vec2;
upperBound: Vec2;
constructor(lower?: Vec2, upper?: Vec2) {
if (!(this instanceof AABB)) {
return new AABB(lower, upper);
}
this.lowerBound = Vec2.zero();
this.upperBound = Vec2.zero();
if (typeof lower === 'object') {
this.lowerBound.set(lower);
this.lowerBound.setVec2(lower);
}
if (typeof upper === 'object') {
this.upperBound.set(upper);
this.upperBound.setVec2(upper);
} else if (typeof lower === 'object') {
this.upperBound.set(lower);
this.upperBound.setVec2(lower);
}
}

View File

@ -218,8 +218,8 @@ export default function Distance(output: DistanceOutput, cache: SimplexCache, in
// Shapes are overlapped when radii are considered.
// Move the witness points to the middle.
const p = Vec2.mid(output.pointA, output.pointB);
output.pointA.set(p);
output.pointB.set(p);
output.pointA.setVec2(p);
output.pointB.setVec2(p);
output.distance = 0.0;
}
}
@ -417,13 +417,13 @@ class Simplex {
case 2: {
const e12 = Vec2.sub(this.m_v2.w, this.m_v1.w);
const sgn = Vec2.cross(e12, Vec2.neg(this.m_v1.w));
const sgn = Vec2.crossVec2Vec2(e12, Vec2.neg(this.m_v1.w));
if (sgn > 0.0) {
// Origin is left of e12.
return Vec2.cross(1.0, e12);
return Vec2.crossNumberVec2(1.0, e12);
} else {
// Origin is right of e12.
return Vec2.cross(e12, 1.0);
return Vec2.crossVec2Number(e12, 1.0);
}
}
@ -461,8 +461,8 @@ class Simplex {
break;
case 1:
pA.set(this.m_v1.wA);
pB.set(this.m_v1.wB);
pA.setVec2(this.m_v1.wA);
pB.setVec2(this.m_v1.wB);
break;
case 2:
@ -473,7 +473,7 @@ class Simplex {
case 3:
pA.setCombine(this.m_v1.a, this.m_v1.wA, this.m_v2.a, this.m_v2.wA);
pA.addMul(this.m_v3.a, this.m_v3.wA);
pB.set(pA);
pB.setVec2(pA);
break;
default:
@ -495,7 +495,7 @@ class Simplex {
return Vec2.distance(this.m_v1.w, this.m_v2.w);
case 3:
return Vec2.cross(Vec2.sub(this.m_v2.w, this.m_v1.w), Vec2.sub(this.m_v3.w,
return Vec2.crossVec2Vec2(Vec2.sub(this.m_v2.w, this.m_v1.w), Vec2.sub(this.m_v3.w,
this.m_v1.w));
default:
@ -617,11 +617,11 @@ class Simplex {
const d23_2 = -w2e23;
// Triangle123
const n123 = Vec2.cross(e12, e13);
const n123 = Vec2.crossVec2Vec2(e12, e13);
const d123_1 = n123 * Vec2.cross(w2, w3);
const d123_2 = n123 * Vec2.cross(w3, w1);
const d123_3 = n123 * Vec2.cross(w1, w2);
const d123_1 = n123 * Vec2.crossVec2Vec2(w2, w3);
const d123_2 = n123 * Vec2.crossVec2Vec2(w3, w1);
const d123_3 = n123 * Vec2.crossVec2Vec2(w1, w2);
// w1 region
if (d12_2 <= 0.0 && d13_2 <= 0.0) {

View File

@ -794,7 +794,7 @@ export default class DynamicTree<T> {
r.normalize();
// v is perpendicular to the segment.
const v = Vec2.cross(1.0, r);
const v = Vec2.crossNumberVec2(1.0, r);
const abs_v = Vec2.abs(v);
// Separating axis for segment (Gino, p80).

View File

@ -93,7 +93,7 @@ export default class Manifold {
const pointB = Transform.mulVec2(xfB, this.points[0].localPoint);
const dist = Vec2.sub(pointB, pointA);
if (Vec2.lengthSquared(dist) > Math.EPSILON * Math.EPSILON) {
normal.set(dist);
normal.setVec2(dist);
normal.normalize();
}
const cA = pointA.clone().addMul(radiusA, normal);
@ -312,7 +312,7 @@ export class ClipVertex {
id: ContactID = new ContactID();
set(o: ClipVertex): void {
this.v.set(o.v);
this.v.setVec2(o.v);
this.id.set(o.id);
}
}

View File

@ -351,7 +351,7 @@ class SeparationFunction {
const localPointB1 = proxyB.getVertex(cache.indexB[0]);
const localPointB2 = proxyB.getVertex(cache.indexB[1]);
this.m_axis = Vec2.cross(Vec2.sub(localPointB2, localPointB1), 1.0);
this.m_axis = Vec2.crossVec2Number(Vec2.sub(localPointB2, localPointB1), 1.0);
this.m_axis.normalize();
const normal = Rot.mulVec2(xfB.q, this.m_axis);
@ -374,7 +374,7 @@ class SeparationFunction {
const localPointA1 = this.m_proxyA.getVertex(cache.indexA[0]);
const localPointA2 = this.m_proxyA.getVertex(cache.indexA[1]);
this.m_axis = Vec2.cross(Vec2.sub(localPointA2, localPointA1), 1.0);
this.m_axis = Vec2.crossVec2Number(Vec2.sub(localPointA2, localPointA1), 1.0);
this.m_axis.normalize();
const normal = Rot.mulVec2(xfA.q, this.m_axis);

View File

@ -24,19 +24,16 @@
import type Vec2 from '../../common/Vec2';
import PolygonShape from './PolygonShape';
import factoryConstructor from '../../decorators/factoryConstructor';
/**
* A rectangle polygon which extend PolygonShape.
*/
@factoryConstructor
export default class BoxShape extends PolygonShape {
static TYPE = 'polygon' as const;
constructor(hx: number, hy: number, center?: Vec2, angle?: number) {
// @ts-ignore
if (!(this instanceof BoxShape)) {
return new BoxShape(hx, hy, center, angle);
}
super();
this._setAsBox(hx, hy, center, angle);

View File

@ -31,6 +31,7 @@ import Vec2 from '../../common/Vec2';
import Settings from '../../Settings';
import Shape from '../Shape';
import EdgeShape from './EdgeShape';
import factoryConstructor from '../../decorators/factoryConstructor';
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
@ -44,6 +45,7 @@ const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
*
* WARNING: The chain will not collide properly if there are self-intersections.
*/
@factoryConstructor
export default class ChainShape extends Shape {
static TYPE = 'chain' as const;
@ -57,11 +59,6 @@ export default class ChainShape extends Shape {
m_isLoop: boolean;
constructor(vertices?: Vec2[], loop?: boolean) {
// @ts-ignore
if (!(this instanceof ChainShape)) {
return new ChainShape(vertices, loop);
}
super();
this.m_type = ChainShape.TYPE;

View File

@ -31,11 +31,13 @@ import AABB, { RayCastInput, RayCastOutput } from '../AABB';
import Transform from '../../common/Transform';
import { MassData } from '../../dynamics/Body';
import { DistanceProxy } from '../Distance';
import factoryConstructor from '../../decorators/factoryConstructor';
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
@factoryConstructor
export default class CircleShape extends Shape {
static TYPE = 'circle' as const;
@ -45,11 +47,6 @@ export default class CircleShape extends Shape {
constructor(radius?: number);
// tslint:disable-next-line:typedef
constructor(a, b?) {
// @ts-ignore
if (!(this instanceof CircleShape)) {
return new CircleShape(a, b);
}
super();
this.m_type = CircleShape.TYPE;
@ -57,7 +54,7 @@ export default class CircleShape extends Shape {
this.m_radius = 1;
if (typeof a === 'object' && Vec2.isValid(a)) {
this.m_p.set(a);
this.m_p.setVec2(a);
if (typeof b === 'number') {
this.m_radius = b;

View File

@ -58,10 +58,10 @@ export function CollideCircles(manifold: Manifold, circleA: CircleShape, xfA: Tr
}
manifold.type = ManifoldType.e_circles;
manifold.localPoint.set(circleA.m_p);
manifold.localPoint.setVec2(circleA.m_p);
manifold.localNormal.setZero();
manifold.pointCount = 1;
manifold.points[0].localPoint.set(circleB.m_p);
manifold.points[0].localPoint.setVec2(circleB.m_p);
// manifold.points[0].id.key = 0;
manifold.points[0].id.cf.indexA = 0;

View File

@ -83,7 +83,7 @@ export function CollidePolygonCircle(manifold: Manifold, polygonA: PolygonShape,
if (separation < Math.EPSILON) {
manifold.pointCount = 1;
manifold.type = ManifoldType.e_faceA;
manifold.localNormal.set(normals[normalIndex]);
manifold.localNormal.setVec2(normals[normalIndex]);
manifold.localPoint.setCombine(0.5, v1, 0.5, v2);
manifold.points[0].localPoint = circleB.m_p;
@ -108,7 +108,7 @@ export function CollidePolygonCircle(manifold: Manifold, polygonA: PolygonShape,
manifold.localNormal.setCombine(1, cLocal, -1, v1);
manifold.localNormal.normalize();
manifold.localPoint = v1;
manifold.points[0].localPoint.set(circleB.m_p);
manifold.points[0].localPoint.setVec2(circleB.m_p);
// manifold.points[0].id.key = 0;
manifold.points[0].id.cf.indexA = 0;
@ -124,8 +124,8 @@ export function CollidePolygonCircle(manifold: Manifold, polygonA: PolygonShape,
manifold.type = ManifoldType.e_faceA;
manifold.localNormal.setCombine(1, cLocal, -1, v2);
manifold.localNormal.normalize();
manifold.localPoint.set(v2);
manifold.points[0].localPoint.set(circleB.m_p);
manifold.localPoint.setVec2(v2);
manifold.points[0].localPoint.setVec2(circleB.m_p);
// manifold.points[0].id.key = 0;
manifold.points[0].id.cf.indexA = 0;
@ -141,9 +141,9 @@ export function CollidePolygonCircle(manifold: Manifold, polygonA: PolygonShape,
manifold.pointCount = 1;
manifold.type = ManifoldType.e_faceA;
manifold.localNormal.set(normals[vertIndex1]);
manifold.localPoint.set(faceCenter);
manifold.points[0].localPoint.set(circleB.m_p);
manifold.localNormal.setVec2(normals[vertIndex1]);
manifold.localPoint.setVec2(faceCenter);
manifold.points[0].localPoint.setVec2(circleB.m_p);
// manifold.points[0].id.key = 0;
manifold.points[0].id.cf.indexA = 0;

View File

@ -105,9 +105,9 @@ export function CollideEdgeCircle(manifold: Manifold, edgeA: EdgeShape, xfA: Tra
manifold.type = ManifoldType.e_circles;
manifold.localNormal.setZero();
manifold.localPoint.set(P);
manifold.localPoint.setVec2(P);
manifold.pointCount = 1;
manifold.points[0].localPoint.set(circleB.m_p);
manifold.points[0].localPoint.setVec2(circleB.m_p);
// manifold.points[0].id.key = 0;
manifold.points[0].id.cf.indexA = 0;
@ -141,9 +141,9 @@ export function CollideEdgeCircle(manifold: Manifold, edgeA: EdgeShape, xfA: Tra
manifold.type = ManifoldType.e_circles;
manifold.localNormal.setZero();
manifold.localPoint.set(P);
manifold.localPoint.setVec2(P);
manifold.pointCount = 1;
manifold.points[0].localPoint.set(circleB.m_p);
manifold.points[0].localPoint.setVec2(circleB.m_p);
// manifold.points[0].id.key = 0;
manifold.points[0].id.cf.indexA = 1;
@ -171,9 +171,9 @@ export function CollideEdgeCircle(manifold: Manifold, edgeA: EdgeShape, xfA: Tra
manifold.type = ManifoldType.e_faceA;
manifold.localNormal = n;
manifold.localPoint.set(A);
manifold.localPoint.setVec2(A);
manifold.pointCount = 1;
manifold.points[0].localPoint.set(circleB.m_p);
manifold.points[0].localPoint.setVec2(circleB.m_p);
// manifold.points[0].id.key = 0;
manifold.points[0].id.cf.indexA = 0;

View File

@ -159,7 +159,7 @@ export function CollideEdgePolygon(manifold: Manifold, edgeA: EdgeShape, xfA: Tr
const edge0 = Vec2.sub(v1, v0);
edge0.normalize();
normal0 = Vec2.neo(edge0.y, -edge0.x);
convex1 = Vec2.cross(edge0, edge1) >= 0.0;
convex1 = Vec2.crossVec2Vec2(edge0, edge1) >= 0.0;
offset0 = Vec2.dot(normal0, centroidB) - Vec2.dot(normal0, v0);
}
@ -168,7 +168,7 @@ export function CollideEdgePolygon(manifold: Manifold, edgeA: EdgeShape, xfA: Tr
const edge2 = Vec2.sub(v3, v2);
edge2.normalize();
normal2 = Vec2.neo(edge2.y, -edge2.x);
convex2 = Vec2.cross(edge1, edge2) > 0.0;
convex2 = Vec2.crossVec2Vec2(edge1, edge2) > 0.0;
offset2 = Vec2.dot(normal2, centroidB) - Vec2.dot(normal2, v2);
}
@ -182,9 +182,9 @@ export function CollideEdgePolygon(manifold: Manifold, edgeA: EdgeShape, xfA: Tr
if (convex1 && convex2) {
front = offset0 >= 0.0 || offset1 >= 0.0 || offset2 >= 0.0;
if (front) {
normal.set(normal1);
lowerLimit.set(normal0);
upperLimit.set(normal2);
normal.setVec2(normal1);
lowerLimit.setVec2(normal0);
upperLimit.setVec2(normal2);
} else {
normal.setMul(-1, normal1);
lowerLimit.setMul(-1, normal1);
@ -193,9 +193,9 @@ export function CollideEdgePolygon(manifold: Manifold, edgeA: EdgeShape, xfA: Tr
} else if (convex1) {
front = offset0 >= 0.0 || (offset1 >= 0.0 && offset2 >= 0.0);
if (front) {
normal.set(normal1);
lowerLimit.set(normal0);
upperLimit.set(normal1);
normal.setVec2(normal1);
lowerLimit.setVec2(normal0);
upperLimit.setVec2(normal1);
} else {
normal.setMul(-1, normal1);
lowerLimit.setMul(-1, normal2);
@ -204,9 +204,9 @@ export function CollideEdgePolygon(manifold: Manifold, edgeA: EdgeShape, xfA: Tr
} else if (convex2) {
front = offset2 >= 0.0 || (offset0 >= 0.0 && offset1 >= 0.0);
if (front) {
normal.set(normal1);
lowerLimit.set(normal1);
upperLimit.set(normal2);
normal.setVec2(normal1);
lowerLimit.setVec2(normal1);
upperLimit.setVec2(normal2);
} else {
normal.setMul(-1, normal1);
lowerLimit.setMul(-1, normal1);
@ -215,9 +215,9 @@ export function CollideEdgePolygon(manifold: Manifold, edgeA: EdgeShape, xfA: Tr
} else {
front = offset0 >= 0.0 && offset1 >= 0.0 && offset2 >= 0.0;
if (front) {
normal.set(normal1);
lowerLimit.set(normal1);
upperLimit.set(normal1);
normal.setVec2(normal1);
lowerLimit.setVec2(normal1);
upperLimit.setVec2(normal1);
} else {
normal.setMul(-1, normal1);
lowerLimit.setMul(-1, normal2);
@ -228,23 +228,23 @@ export function CollideEdgePolygon(manifold: Manifold, edgeA: EdgeShape, xfA: Tr
if (convex1) {
front = offset0 >= 0.0 || offset1 >= 0.0;
if (front) {
normal.set(normal1);
lowerLimit.set(normal0);
normal.setVec2(normal1);
lowerLimit.setVec2(normal0);
upperLimit.setMul(-1, normal1);
} else {
normal.setMul(-1, normal1);
lowerLimit.set(normal1);
lowerLimit.setVec2(normal1);
upperLimit.setMul(-1, normal1);
}
} else {
front = offset0 >= 0.0 && offset1 >= 0.0;
if (front) {
normal.set(normal1);
lowerLimit.set(normal1);
normal.setVec2(normal1);
lowerLimit.setVec2(normal1);
upperLimit.setMul(-1, normal1);
} else {
normal.setMul(-1, normal1);
lowerLimit.set(normal1);
lowerLimit.setVec2(normal1);
upperLimit.setMul(-1, normal0);
}
}
@ -252,36 +252,36 @@ export function CollideEdgePolygon(manifold: Manifold, edgeA: EdgeShape, xfA: Tr
if (convex2) {
front = offset1 >= 0.0 || offset2 >= 0.0;
if (front) {
normal.set(normal1);
normal.setVec2(normal1);
lowerLimit.setMul(-1, normal1);
upperLimit.set(normal2);
upperLimit.setVec2(normal2);
} else {
normal.setMul(-1, normal1);
lowerLimit.setMul(-1, normal1);
upperLimit.set(normal1);
upperLimit.setVec2(normal1);
}
} else {
front = offset1 >= 0.0 && offset2 >= 0.0;
if (front) {
normal.set(normal1);
normal.setVec2(normal1);
lowerLimit.setMul(-1, normal1);
upperLimit.set(normal1);
upperLimit.setVec2(normal1);
} else {
normal.setMul(-1, normal1);
lowerLimit.setMul(-1, normal2);
upperLimit.set(normal1);
upperLimit.setVec2(normal1);
}
}
} else {
front = offset1 >= 0.0;
if (front) {
normal.set(normal1);
normal.setVec2(normal1);
lowerLimit.setMul(-1, normal1);
upperLimit.setMul(-1, normal1);
} else {
normal.setMul(-1, normal1);
lowerLimit.set(normal1);
upperLimit.set(normal1);
lowerLimit.setVec2(normal1);
upperLimit.setVec2(normal1);
}
}
@ -414,7 +414,7 @@ export function CollideEdgePolygon(manifold: Manifold, edgeA: EdgeShape, xfA: Tr
rf.i2 = 1;
rf.v1 = v1;
rf.v2 = v2;
rf.normal.set(normal1);
rf.normal.setVec2(normal1);
} else {
rf.i1 = 1;
rf.i2 = 0;
@ -441,7 +441,7 @@ export function CollideEdgePolygon(manifold: Manifold, edgeA: EdgeShape, xfA: Tr
rf.i2 = rf.i1 + 1 < polygonBA.count ? rf.i1 + 1 : 0;
rf.v1 = polygonBA.vertices[rf.i1];
rf.v2 = polygonBA.vertices[rf.i2];
rf.normal.set(polygonBA.normals[rf.i1]);
rf.normal.setVec2(polygonBA.normals[rf.i1]);
}
rf.sideNormal1.set(rf.normal.y, -rf.normal.x);
@ -486,7 +486,7 @@ export function CollideEdgePolygon(manifold: Manifold, edgeA: EdgeShape, xfA: Tr
const cp = manifold.points[pointCount]; // ManifoldPoint
if (primaryAxis.type == EPAxisType.e_edgeA) {
cp.localPoint = Transform.mulT(xf, clipPoints2[i].v);
cp.localPoint = Transform.mulTVec2(xf, clipPoints2[i].v);
cp.id = clipPoints2[i].id;
} else {
cp.localPoint = clipPoints2[i].v;

View File

@ -98,7 +98,7 @@ function findIncidentEdge(c: ClipVertex[], poly1: PolygonShape, xf1: Transform,
_ASSERT && common.assert(0 <= edge1 && edge1 < poly1.m_count);
// Get the normal of the reference edge in poly2's frame.
const normal1 = Rot.mulT(xf2.q, Rot.mulVec2(xf1.q, normals1[edge1]));
const normal1 = Rot.mulTVec2(xf2.q, Rot.mulVec2(xf1.q, normals1[edge1]));
// Find the incident edge on poly2.
let index = 0;
@ -200,11 +200,11 @@ export function CollidePolygons(manifold: Manifold, polyA: PolygonShape, xfA: Tr
const localTangent = Vec2.sub(v12, v11);
localTangent.normalize();
const localNormal = Vec2.cross(localTangent, 1.0);
const localNormal = Vec2.crossVec2Number(localTangent, 1.0);
const planePoint = Vec2.combine(0.5, v11, 0.5, v12);
const tangent = Rot.mulVec2(xf1.q, localTangent);
const normal = Vec2.cross(tangent, 1.0);
const normal = Vec2.crossVec2Number(tangent, 1.0);
v11 = Transform.mulVec2(xf1, v11);
v12 = Transform.mulVec2(xf1, v12);
@ -245,7 +245,7 @@ export function CollidePolygons(manifold: Manifold, polyA: PolygonShape, xfA: Tr
if (separation <= totalRadius) {
const cp = manifold.points[pointCount];
cp.localPoint.set(Transform.mulTVec2(xf2, clipPoints2[i].v));
cp.localPoint.setVec2(Transform.mulTVec2(xf2, clipPoints2[i].v));
cp.id = clipPoints2[i].id;
if (flip) {
// Swap features

View File

@ -30,12 +30,14 @@ import Vec2 from '../../common/Vec2';
import AABB, { RayCastInput, RayCastOutput } from '../AABB';
import { MassData } from '../../dynamics/Body';
import { DistanceProxy } from '../Distance';
import factoryConstructor from '../../decorators/factoryConstructor';
/**
* A line segment (edge) shape. These can be connected in chains or loops to
* other edge shapes. The connectivity information is used to ensure correct
* contact normals.
*/
@factoryConstructor
export default class EdgeShape extends Shape {
static TYPE = 'edge' as const;
@ -51,11 +53,6 @@ export default class EdgeShape extends Shape {
m_hasVertex3: boolean;
constructor(v1?: Vec2, v2?: Vec2) {
// @ts-ignore
if (!(this instanceof EdgeShape)) {
return new EdgeShape(v1, v2);
}
super();
this.m_type = EdgeShape.TYPE;
@ -108,7 +105,7 @@ export default class EdgeShape extends Shape {
*/
setNextVertex(v?: Vec2): EdgeShape {
if (v) {
this.m_vertex3.set(v);
this.m_vertex3.setVec2(v);
this.m_hasVertex3 = true;
} else {
this.m_vertex3.setZero();
@ -134,7 +131,7 @@ export default class EdgeShape extends Shape {
*/
setPrevVertex(v?: Vec2): EdgeShape {
if (v) {
this.m_vertex0.set(v);
this.m_vertex0.setVec2(v);
this.m_hasVertex0 = true;
} else {
this.m_vertex0.setZero();
@ -154,8 +151,8 @@ export default class EdgeShape extends Shape {
* Set this as an isolated edge.
*/
_set(v1: Vec2, v2: Vec2): EdgeShape {
this.m_vertex1.set(v1);
this.m_vertex2.set(v2);
this.m_vertex1.setVec2(v1);
this.m_vertex2.setVec2(v2);
this.m_hasVertex0 = false;
this.m_hasVertex3 = false;
return this;
@ -171,10 +168,10 @@ export default class EdgeShape extends Shape {
const clone = new EdgeShape();
clone.m_type = this.m_type;
clone.m_radius = this.m_radius;
clone.m_vertex1.set(this.m_vertex1);
clone.m_vertex2.set(this.m_vertex2);
clone.m_vertex0.set(this.m_vertex0);
clone.m_vertex3.set(this.m_vertex3);
clone.m_vertex1.setVec2(this.m_vertex1);
clone.m_vertex2.setVec2(this.m_vertex2);
clone.m_vertex0.setVec2(this.m_vertex0);
clone.m_vertex3.setVec2(this.m_vertex3);
clone.m_hasVertex0 = this.m_hasVertex0;
clone.m_hasVertex3 = this.m_hasVertex3;
return clone;

View File

@ -32,6 +32,7 @@ import Rot from '../../common/Rot';
import Vec2 from '../../common/Vec2';
import Settings from '../../Settings';
import Shape from '../Shape';
import factoryConstructor from '../../decorators/factoryConstructor';
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
@ -43,6 +44,7 @@ const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
* Settings.maxPolygonVertices. In most cases you should not need many vertices
* for a convex polygon. extends Shape
*/
@factoryConstructor
export default class PolygonShape extends Shape {
static TYPE = 'polygon' as const;
@ -53,11 +55,6 @@ export default class PolygonShape extends Shape {
// @ts-ignore
constructor(vertices?: Vec2[]) {
// @ts-ignore
if (!(this instanceof PolygonShape)) {
return new PolygonShape(vertices);
}
super();
this.m_type = PolygonShape.TYPE;
@ -110,7 +107,7 @@ export default class PolygonShape extends Shape {
clone.m_type = this.m_type;
clone.m_radius = this.m_radius;
clone.m_count = this.m_count;
clone.m_centroid.set(this.m_centroid);
clone.m_centroid.setVec2(this.m_centroid);
for (let i = 0; i < this.m_count; i++) {
clone.m_vertices.push(this.m_vertices[i].clone());
}
@ -207,7 +204,7 @@ export default class PolygonShape extends Shape {
const r = Vec2.sub(ps[ie], ps[hull[m]]);
const v = Vec2.sub(ps[j], ps[hull[m]]);
const c = Vec2.cross(r, v);
const c = Vec2.crossVec2Vec2(r, v);
// c < 0 means counter-clockwise wrapping, c > 0 means clockwise wrapping
if (c < 0.0) {
ie = j;
@ -248,7 +245,7 @@ export default class PolygonShape extends Shape {
const i2 = i + 1 < m ? i + 1 : 0;
const edge = Vec2.sub(this.m_vertices[i2], this.m_vertices[i1]);
_ASSERT && common.assert(edge.lengthSquared() > Math.EPSILON * Math.EPSILON);
this.m_normals[i] = Vec2.cross(edge, 1.0);
this.m_normals[i] = Vec2.crossVec2Number(edge, 1.0);
this.m_normals[i].normalize();
}
@ -274,11 +271,11 @@ export default class PolygonShape extends Shape {
if (Vec2.isValid(center)) {
angle = angle || 0;
this.m_centroid.set(center);
this.m_centroid.setVec2(center);
const xf = Transform.identity();
xf.p.set(center);
xf.q.set(angle);
xf.p.setVec2(center);
xf.q.setAngle(angle);
// Transform vertices and normals.
for (let i = 0; i < this.m_count; ++i) {
@ -457,7 +454,7 @@ export default class PolygonShape extends Shape {
const e1 = Vec2.sub(this.m_vertices[i], s);
const e2 = i + 1 < this.m_count ? Vec2.sub(this.m_vertices[i + 1], s) : Vec2 .sub(this.m_vertices[0], s);
const D = Vec2.cross(e1, e2);
const D = Vec2.crossVec2Vec2(e1, e2);
const triangleArea = 0.5 * D;
area += triangleArea;
@ -508,7 +505,7 @@ export default class PolygonShape extends Shape {
}
const v = Vec2.sub(this.m_vertices[j], p);
const c = Vec2.cross(e, v);
const c = Vec2.crossVec2Vec2(e, v);
if (c < 0.0) {
return false;
}
@ -553,7 +550,7 @@ function ComputeCentroid(vs: Vec2[], count: number): Vec2 {
const e1 = Vec2.sub(p2, p1);
const e2 = Vec2.sub(p3, p1);
const D = Vec2.cross(e1, e2);
const D = Vec2.crossVec2Vec2(e1, e2);
const triangleArea = 0.5 * D;
area += triangleArea;

View File

@ -37,17 +37,17 @@ export default class Mat22 {
ex: Vec2;
ey: Vec2;
constructor(a: number, b: number, c: number, d: number);
constructor(a: { x: number; y: number }, b: { x: number; y: number });
// constructor(a: number, b: number, c: number, d: number);
constructor(a: Vec2, b: Vec2);
constructor();
// tslint:disable-next-line:typedef
constructor(a?, b?, c?, d?) {
if (typeof a === 'object' && a !== null) {
if (a != null) {
this.ex = Vec2.clone(a);
this.ey = Vec2.clone(b);
} else if (typeof a === 'number') {
this.ex = Vec2.neo(a, c);
this.ey = Vec2.neo(b, d);
// } else if (typeof a === 'number') {
// this.ex = Vec2.neo(a, c);
// this.ey = Vec2.neo(b, d);
} else {
this.ex = Vec2.zero();
this.ey = Vec2.zero();
@ -75,27 +75,27 @@ export default class Mat22 {
}
set(a: Mat22): void;
set(a: Vec2, b: Vec2): void;
set(a: number, b: number, c: number, d: number): void;
// set(a: Vec2, b: Vec2): void;
// set(a: number, b: number, c: number, d: number): void;
// tslint:disable-next-line:typedef
set(a, b?, c?, d?): void {
if (typeof a === 'number' && typeof b === 'number' && typeof c === 'number'
&& typeof d === 'number') {
this.ex.set(a, c);
this.ey.set(b, d);
// if (typeof a === 'number' && typeof b === 'number' && typeof c === 'number'
// && typeof d === 'number') {
// this.ex.set(a, c);
// this.ey.set(b, d);
} else if (typeof a === 'object' && typeof b === 'object') {
this.ex.set(a);
this.ey.set(b);
// } else if (typeof a === 'object' && typeof b === 'object') {
// this.ex.setVec2(a);
// this.ey.setVec2(b);
} else if (typeof a === 'object') {
// } else if (typeof a === 'object') {
_ASSERT && Mat22.assert(a);
this.ex.set(a.ex);
this.ey.set(a.ey);
this.ex.setVec2(a.ex);
this.ey.setVec2(a.ey);
} else {
_ASSERT && common.assert(false);
}
// } else {
// _ASSERT && common.assert(false);
// }
}
setIdentity(): void {
@ -153,28 +153,28 @@ export default class Mat22 {
* Multiply a matrix times a vector. If a rotation matrix is provided, then this
* transforms the vector from one frame to another.
*/
static mul(mx: Mat22, my: Mat22): Mat22;
static mul(mx: Mat22, v: Vec2): Vec2;
// tslint:disable-next-line:typedef
static mul(mx, v) {
if (v && 'x' in v && 'y' in v) {
_ASSERT && Vec2.assert(v);
const x = mx.ex.x * v.x + mx.ey.x * v.y;
const y = mx.ex.y * v.x + mx.ey.y * v.y;
return Vec2.neo(x, y);
// static mul(mx: Mat22, my: Mat22): Mat22;
// static mul(mx: Mat22, v: Vec2): Vec2;
// // tslint:disable-next-line:typedef
// static mul(mx, v) {
// if (v && 'x' in v && 'y' in v) {
// _ASSERT && Vec2.assert(v);
// const x = mx.ex.x * v.x + mx.ey.x * v.y;
// const y = mx.ex.y * v.x + mx.ey.y * v.y;
// return Vec2.neo(x, y);
} else if (v && 'ex' in v && 'ey' in v) { // Mat22
_ASSERT && Mat22.assert(v);
// return new Mat22(Vec2.mul(mx, v.ex), Vec2.mul(mx, v.ey));
const a = mx.ex.x * v.ex.x + mx.ey.x * v.ex.y;
const b = mx.ex.x * v.ey.x + mx.ey.x * v.ey.y;
const c = mx.ex.y * v.ex.x + mx.ey.y * v.ex.y;
const d = mx.ex.y * v.ey.x + mx.ey.y * v.ey.y;
return new Mat22(a, b, c, d);
}
// } else if (v && 'ex' in v && 'ey' in v) { // Mat22
// _ASSERT && Mat22.assert(v);
// // return new Mat22(Vec2.mul(mx, v.ex), Vec2.mul(mx, v.ey));
// const a = mx.ex.x * v.ex.x + mx.ey.x * v.ex.y;
// const b = mx.ex.x * v.ey.x + mx.ey.x * v.ey.y;
// const c = mx.ex.y * v.ex.x + mx.ey.y * v.ex.y;
// const d = mx.ex.y * v.ey.x + mx.ey.y * v.ey.y;
// return new Mat22(a, b, c, d);
// }
_ASSERT && common.assert(false);
}
// _ASSERT && common.assert(false);
// }
static mulVec2(mx: Mat22, v: Vec2): Vec2 {
_ASSERT && Vec2.assert(v);
@ -190,7 +190,7 @@ export default class Mat22 {
const b = mx.ex.x * v.ey.x + mx.ey.x * v.ey.y;
const c = mx.ex.y * v.ex.x + mx.ey.y * v.ex.y;
const d = mx.ex.y * v.ey.x + mx.ey.y * v.ey.y;
return new Mat22(a, b, c, d);
return new Mat22(Vec2.neo(a, b), Vec2.neo(c, d));
}
/**
@ -198,23 +198,23 @@ export default class Mat22 {
* then this transforms the vector from one frame to another (inverse
* transform).
*/
static mulT(mx: Mat22, my: Mat22): Mat22;
static mulT(mx: Mat22, v: Vec2): Vec2;
// tslint:disable-next-line:typedef
static mulT(mx, v) {
if (v && 'x' in v && 'y' in v) { // Vec2
_ASSERT && Vec2.assert(v);
return Vec2.neo(Vec2.dot(v, mx.ex), Vec2.dot(v, mx.ey));
// static mulT(mx: Mat22, my: Mat22): Mat22;
// static mulT(mx: Mat22, v: Vec2): Vec2;
// // tslint:disable-next-line:typedef
// static mulT(mx, v) {
// if (v && 'x' in v && 'y' in v) { // Vec2
// _ASSERT && Vec2.assert(v);
// return Vec2.neo(Vec2.dot(v, mx.ex), Vec2.dot(v, mx.ey));
} else if (v && 'ex' in v && 'ey' in v) { // Mat22
_ASSERT && Mat22.assert(v);
const c1 = Vec2.neo(Vec2.dot(mx.ex, v.ex), Vec2.dot(mx.ey, v.ex));
const c2 = Vec2.neo(Vec2.dot(mx.ex, v.ey), Vec2.dot(mx.ey, v.ey));
return new Mat22(c1, c2);
}
// } else if (v && 'ex' in v && 'ey' in v) { // Mat22
// _ASSERT && Mat22.assert(v);
// const c1 = Vec2.neo(Vec2.dot(mx.ex, v.ex), Vec2.dot(mx.ey, v.ex));
// const c2 = Vec2.neo(Vec2.dot(mx.ex, v.ey), Vec2.dot(mx.ey, v.ey));
// return new Mat22(c1, c2);
// }
_ASSERT && common.assert(false);
}
// _ASSERT && common.assert(false);
// }
static mulTVec2(mx: Mat22, v: Vec2): Vec2 {
_ASSERT && Mat22.assert(mx);

View File

@ -42,7 +42,7 @@ export default class Mat33 {
constructor(a: Vec3, b: Vec3, c: Vec3);
constructor();
constructor(a?: Vec3, b?: Vec3, c?: Vec3) {
if (typeof a === 'object' && a !== null) {
if (a != null) {
this.ex = Vec3.clone(a);
this.ey = Vec3.clone(b);
this.ez = Vec3.clone(c);
@ -175,27 +175,27 @@ export default class Mat33 {
/**
* Multiply a matrix times a vector.
*/
static mul(a: Mat33, b: Vec2): Vec2;
static mul(a: Mat33, b: Vec3): Vec3;
// tslint:disable-next-line:typedef
static mul(a, b) {
_ASSERT && Mat33.assert(a);
if (b && 'z' in b && 'y' in b && 'x' in b) {
_ASSERT && Vec3.assert(b);
const x = a.ex.x * b.x + a.ey.x * b.y + a.ez.x * b.z;
const y = a.ex.y * b.x + a.ey.y * b.y + a.ez.y * b.z;
const z = a.ex.z * b.x + a.ey.z * b.y + a.ez.z * b.z;
return new Vec3(x, y, z);
// static mul(a: Mat33, b: Vec2): Vec2;
// static mul(a: Mat33, b: Vec3): Vec3;
// // tslint:disable-next-line:typedef
// static mul(a, b) {
// _ASSERT && Mat33.assert(a);
// if (b && 'z' in b && 'y' in b && 'x' in b) {
// _ASSERT && Vec3.assert(b);
// const x = a.ex.x * b.x + a.ey.x * b.y + a.ez.x * b.z;
// const y = a.ex.y * b.x + a.ey.y * b.y + a.ez.y * b.z;
// const z = a.ex.z * b.x + a.ey.z * b.y + a.ez.z * b.z;
// return new Vec3(x, y, z);
} else if (b && 'y' in b && 'x' in b) {
_ASSERT && Vec2.assert(b);
const x = a.ex.x * b.x + a.ey.x * b.y;
const y = a.ex.y * b.x + a.ey.y * b.y;
return Vec2.neo(x, y);
}
// } else if (b && 'y' in b && 'x' in b) {
// _ASSERT && Vec2.assert(b);
// const x = a.ex.x * b.x + a.ey.x * b.y;
// const y = a.ex.y * b.x + a.ey.y * b.y;
// return Vec2.neo(x, y);
// }
_ASSERT && common.assert(false);
}
// _ASSERT && common.assert(false);
// }
static mulVec3(a: Mat33, b: Vec3): Vec3 {
_ASSERT && Mat33.assert(a);

View File

@ -25,50 +25,38 @@
import common from '../util/common';
import Vec2 from './Vec2';
import Math from './Math';
import factoryConstructor from '../decorators/factoryConstructor';
const _DEBUG = typeof DEBUG === 'undefined' ? false : DEBUG;
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
@factoryConstructor
export default class Rot {
s: number;
c: number;
/** Initialize from an angle in radians. */
constructor(angle?: number | Rot) {
if (!(this instanceof Rot)) {
return new Rot(angle);
}
if (typeof angle === 'number') {
this.setAngle(angle);
} else if (typeof angle === 'object') {
this.set(angle);
} else {
this.setIdentity();
}
constructor(angle=0) {
this.setAngle(angle);
}
/** @internal */
static neo(angle: number): Rot {
const obj = Object.create(Rot.prototype);
obj.setAngle(angle);
return obj;
return new Rot(angle);
}
static clone(rot: Rot): Rot {
_ASSERT && Rot.assert(rot);
const obj = Object.create(Rot.prototype);
const obj = new Rot();
obj.s = rot.s;
obj.c = rot.c;
return obj;
}
static identity(): Rot {
const obj = Object.create(Rot.prototype);
obj.s = 0.0;
obj.c = 1.0;
return obj;
return new Rot();
}
static isValid(obj: any): boolean {
@ -92,18 +80,18 @@ export default class Rot {
this.c = 1.0;
}
set(angle: number | Rot): void {
if (typeof angle === 'object') {
set(angle: Rot): void {
// if (typeof angle === 'object') {
_ASSERT && Rot.assert(angle);
this.s = angle.s;
this.c = angle.c;
} else {
_ASSERT && Math.assert(angle);
// TODO_ERIN optimize
this.s = Math.sin(angle);
this.c = Math.cos(angle);
}
// } else {
// _ASSERT && Math.assert(angle);
// // TODO_ERIN optimize
// this.s = Math.sin(angle);
// this.c = Math.cos(angle);
// }
}
/** Set using an angle in radians. */
@ -129,29 +117,29 @@ export default class Rot {
return Vec2.neo(-this.s, this.c);
}
/** Multiply two rotations: q * r */
static mul(rot: Rot, m: Rot): Rot;
/** Rotate a vector */
static mul(rot: Rot, m: Vec2): Vec2;
// tslint:disable-next-line:typedef
static mul(rot, m) {
_ASSERT && Rot.assert(rot);
if ('c' in m && 's' in m) {
_ASSERT && Rot.assert(m);
// [qc -qs] * [rc -rs] = [qc*rc-qs*rs -qc*rs-qs*rc]
// [qs qc] [rs rc] [qs*rc+qc*rs -qs*rs+qc*rc]
// s = qs * rc + qc * rs
// c = qc * rc - qs * rs
const qr = Rot.identity();
qr.s = rot.s * m.c + rot.c * m.s;
qr.c = rot.c * m.c - rot.s * m.s;
return qr;
// /** Multiply two rotations: q * r */
// static mul(rot: Rot, m: Rot): Rot;
// /** Rotate a vector */
// static mul(rot: Rot, m: Vec2): Vec2;
// // tslint:disable-next-line:typedef
// static mul(rot, m) {
// _ASSERT && Rot.assert(rot);
// if ('c' in m && 's' in m) {
// _ASSERT && Rot.assert(m);
// // [qc -qs] * [rc -rs] = [qc*rc-qs*rs -qc*rs-qs*rc]
// // [qs qc] [rs rc] [qs*rc+qc*rs -qs*rs+qc*rc]
// // s = qs * rc + qc * rs
// // c = qc * rc - qs * rs
// const qr = Rot.identity();
// qr.s = rot.s * m.c + rot.c * m.s;
// qr.c = rot.c * m.c - rot.s * m.s;
// return qr;
} else if ('x' in m && 'y' in m) {
_ASSERT && Vec2.assert(m);
return Vec2.neo(rot.c * m.x - rot.s * m.y, rot.s * m.x + rot.c * m.y);
}
}
// } else if ('x' in m && 'y' in m) {
// _ASSERT && Vec2.assert(m);
// return Vec2.neo(rot.c * m.x - rot.s * m.y, rot.s * m.x + rot.c * m.y);
// }
// }
static mulRot(rot: Rot, m: Rot): Rot {
_ASSERT && Rot.assert(rot);
@ -178,28 +166,28 @@ export default class Rot {
return Vec2.neo(x, y);
}
/** Transpose multiply two rotations: qT * r */
static mulT(rot: Rot, m: Rot): Rot;
/** Inverse rotate a vector */
static mulT(rot: Rot, m: Vec2): Vec2;
// tslint:disable-next-line:typedef
static mulT(rot, m) {
if ('c' in m && 's' in m) {
_ASSERT && Rot.assert(m);
// [ qc qs] * [rc -rs] = [qc*rc+qs*rs -qc*rs+qs*rc]
// [-qs qc] [rs rc] [-qs*rc+qc*rs qs*rs+qc*rc]
// s = qc * rs - qs * rc
// c = qc * rc + qs * rs
const qr = Rot.identity();
qr.s = rot.c * m.s - rot.s * m.c;
qr.c = rot.c * m.c + rot.s * m.s;
return qr;
// /** Transpose multiply two rotations: qT * r */
// static mulT(rot: Rot, m: Rot): Rot;
// /** Inverse rotate a vector */
// static mulT(rot: Rot, m: Vec2): Vec2;
// // tslint:disable-next-line:typedef
// static mulT(rot, m) {
// if ('c' in m && 's' in m) {
// _ASSERT && Rot.assert(m);
// // [ qc qs] * [rc -rs] = [qc*rc+qs*rs -qc*rs+qs*rc]
// // [-qs qc] [rs rc] [-qs*rc+qc*rs qs*rs+qc*rc]
// // s = qc * rs - qs * rc
// // c = qc * rc + qs * rs
// const qr = Rot.identity();
// qr.s = rot.c * m.s - rot.s * m.c;
// qr.c = rot.c * m.c + rot.s * m.s;
// return qr;
} else if ('x' in m && 'y' in m) {
_ASSERT && Vec2.assert(m);
return Vec2.neo(rot.c * m.x + rot.s * m.y, -rot.s * m.x + rot.c * m.y);
}
}
// } else if ('x' in m && 'y' in m) {
// _ASSERT && Vec2.assert(m);
// return Vec2.neo(rot.c * m.x + rot.s * m.y, -rot.s * m.x + rot.c * m.y);
// }
// }
static mulTRot(rot: Rot, m: Rot): Rot {
_ASSERT && Rot.assert(m);

View File

@ -67,19 +67,19 @@ export default class Sweep {
setTransform(xf: Transform): void {
const c = Transform.mulVec2(xf, this.localCenter);
this.c.set(c);
this.c0.set(c);
this.c.setVec2(c);
this.c0.setVec2(c);
this.a = xf.q.getAngle();
this.a0 = xf.q.getAngle();
}
setLocalCenter(localCenter: Vec2, xf: Transform): void {
this.localCenter.set(localCenter);
this.localCenter.setVec2(localCenter);
const c = Transform.mulVec2(xf, this.localCenter);
this.c.set(c);
this.c0.set(c);
this.c.setVec2(c);
this.c0.setVec2(c);
}
/**
@ -88,8 +88,7 @@ export default class Sweep {
* @param xf
* @param beta A factor in [0,1], where 0 indicates alpha0
*/
getTransform(xf: Transform, beta: number): void {
beta = typeof beta === 'undefined' ? 0 : beta;
getTransform(xf: Transform, beta=0): void {
xf.q.setAngle((1.0 - beta) * this.a0 + beta * this.a);
xf.p.setCombine((1.0 - beta), this.c0, beta, this.c);
@ -112,7 +111,7 @@ export default class Sweep {
forward(): void {
this.a0 = this.a;
this.c0.set(this.c);
this.c0.setVec2(this.c);
}
/**
@ -126,21 +125,21 @@ export default class Sweep {
clone(): Sweep {
const clone = new Sweep();
clone.localCenter.set(this.localCenter);
clone.localCenter.setVec2(this.localCenter);
clone.alpha0 = this.alpha0;
clone.a0 = this.a0;
clone.a = this.a;
clone.c0.set(this.c0);
clone.c.set(this.c);
clone.c0.setVec2(this.c0);
clone.c.setVec2(this.c);
return clone;
}
set(that: Sweep): void {
this.localCenter.set(that.localCenter);
this.localCenter.setVec2(that.localCenter);
this.alpha0 = that.alpha0;
this.a0 = that.a0;
this.a = that.a;
this.c0.set(that.c0);
this.c.set(that.c);
this.c0.setVec2(that.c0);
this.c.setVec2(that.c);
}
}

View File

@ -25,6 +25,7 @@
import common from '../util/common';
import Vec2 from './Vec2';
import Rot from './Rot';
import factoryConstructor from '../decorators/factoryConstructor';
const _DEBUG = typeof DEBUG === 'undefined' ? false : DEBUG;
@ -36,47 +37,37 @@ const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
* position and orientation of rigid frames. Initialize using a position vector
* and a rotation.
*/
@factoryConstructor
export default class Transform {
/** position */
p: Vec2;
p: Vec2 = new Vec2();
/** rotation */
q: Rot;
constructor(position?: Vec2, rotation?: number) {
if (!(this instanceof Transform)) {
return new Transform(position, rotation);
}
this.p = Vec2.zero();
constructor(position=Vec2.zero(), rotation=0) {
this.p.setVec2(position);
this.q = Rot.identity();
if (typeof position !== 'undefined') {
this.p.set(position);
}
if (typeof rotation !== 'undefined') {
this.q.set(rotation);
if (rotation != 0) {
this.q.setAngle(rotation);
}
}
static clone(xf: Transform): Transform {
const obj = Object.create(Transform.prototype);
obj.p = Vec2.clone(xf.p);
obj.q = Rot.clone(xf.q);
const obj = new Transform(xf.p);
obj.q.set(xf.q);
return obj;
}
/** @internal */
static neo(position: Vec2, rotation: Rot): Transform {
const obj = Object.create(Transform.prototype);
obj.p = Vec2.clone(position);
obj.q = Rot.clone(rotation);
const obj = new Transform(position);
obj.q.set(Rot.clone(rotation));
return obj;
}
static identity(): Transform {
const obj = Object.create(Transform.prototype);
obj.p = Vec2.zero();
obj.q = Rot.identity();
return obj;
return new Transform();
}
/**
@ -88,19 +79,19 @@ export default class Transform {
}
set(position: Vec2, rotation: number): void;
set(xf: Transform): void;
// set(xf: Transform): void; // TODO
/**
* Set this based on the position and angle.
*/
// tslint:disable-next-line:typedef
set(a, b?) {
if (typeof b === 'undefined') {
this.p.set(a.p);
this.q.set(a.q);
} else {
this.p.set(a);
this.q.set(b);
}
set(aposition: Vec2, rotation: number) {
// if (typeof b === 'undefined') {
// this.p.setVec2(a.p);
// this.q.set(a.q);
// } else {
this.p.setVec2(aposition);
this.q.setAngle(rotation);
// }
}
static isValid(obj: any): boolean {
@ -118,48 +109,48 @@ export default class Transform {
}
}
static mul(a: Transform, b: Vec2): Vec2;
static mul(a: Transform, b: Transform): Transform;
// static mul(a: Transform, b: Vec2[]): Vec2[];
// static mul(a: Transform, b: Transform[]): Transform[];
// tslint:disable-next-line:typedef
static mul(a, b) {
if (Array.isArray(b)) {
_ASSERT && Transform.assert(a);
const arr = [];
for (let i = 0; i < b.length; i++) {
arr[i] = Transform.mul(a, b[i]);
}
return arr;
// static mul(a: Transform, b: Vec2): Vec2;
// static mul(a: Transform, b: Transform): Transform; // TODO
// // static mul(a: Transform, b: Vec2[]): Vec2[];
// // static mul(a: Transform, b: Transform[]): Transform[];
// // tslint:disable-next-line:typedef
// static mul(a, b) {
// if (Array.isArray(b)) {
// _ASSERT && Transform.assert(a);
// const arr = [];
// for (let i = 0; i < b.length; i++) {
// arr[i] = Transform.mul(a, b[i]);
// }
// return arr;
} else if ('x' in b && 'y' in b) {
return Transform.mulVec2(a, b);
// } else if ('x' in b && 'y' in b) {
// return Transform.mulVec2(a, b);
} else if ('p' in b && 'q' in b) {
return Transform.mulXf(a, b);
}
}
// } else if ('p' in b && 'q' in b) {
// return Transform.mulXf(a, b);
// }
// }
static mulAll(a: Transform, b: Vec2[]): Vec2[];
static mulAll(a: Transform, b: Transform[]): Transform[];
// tslint:disable-next-line:typedef
static mulAll(a: Transform, b) {
_ASSERT && Transform.assert(a);
const arr = [];
for (let i = 0; i < b.length; i++) {
arr[i] = Transform.mul(a, b[i]);
}
return arr;
}
// static mulAll(a: Transform, b: Vec2[]): Vec2[];
// static mulAll(a: Transform, b: Transform[]): Transform[]; // TODO
// // tslint:disable-next-line:typedef
// static mulAll(a: Transform, b) {
// _ASSERT && Transform.assert(a);
// const arr = [];
// for (let i = 0; i < b.length; i++) {
// arr[i] = Transform.mul(a, b[i]);
// }
// return arr;
// }
/** @internal @deprecated */
// tslint:disable-next-line:typedef
static mulFn(a: Transform) {
_ASSERT && Transform.assert(a);
return function(b: Vec2): Vec2 {
return Transform.mul(a, b);
};
}
// /** @internal @deprecated */
// // tslint:disable-next-line:typedef
// static mulFn(a: Transform) {
// _ASSERT && Transform.assert(a);
// return function(b: Vec2): Vec2 {
// return Transform.mul(a, b);
// };
// }
static mulVec2(a: Transform, b: Vec2): Vec2 {
_ASSERT && Transform.assert(a);
@ -180,17 +171,17 @@ export default class Transform {
return xf;
}
static mulT(a: Transform, b: Vec2): Vec2;
static mulT(a: Transform, b: Transform): Transform;
// tslint:disable-next-line:typedef
static mulT(a, b) {
if ('x' in b && 'y' in b) {
return Transform.mulTVec2(a, b);
// static mulT(a: Transform, b: Vec2): Vec2;
// static mulT(a: Transform, b: Transform): Transform;
// // tslint:disable-next-line:typedef
// static mulT(a, b) {
// if ('x' in b && 'y' in b) {
// return Transform.mulTVec2(a, b);
} else if ('p' in b && 'q' in b) {
return Transform.mulTXf(a, b);
}
}
// } else if ('p' in b && 'q' in b) {
// return Transform.mulTXf(a, b);
// }
// }
static mulTVec2(a: Transform, b: Vec2): Vec2 {
_ASSERT && Transform.assert(a);
@ -209,7 +200,7 @@ export default class Transform {
// = A.q' * B.q * v1 + A.q' * (B.p - A.p)
const xf = Transform.identity();
xf.q.set(Rot.mulTRot(a.q, b.q));
xf.p.set(Rot.mulTVec2(a.q, Vec2.sub(b.p, a.p)));
xf.p.setVec2(Rot.mulTVec2(a.q, Vec2.sub(b.p, a.p)));
return xf;
}
}

View File

@ -24,34 +24,24 @@
import common from '../util/common';
import Math from './Math';
import factoryConstructor from '../decorators/factoryConstructor';
const _DEBUG = typeof DEBUG === 'undefined' ? false : DEBUG;
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
@factoryConstructor
export default class Vec2 {
x: number;
y: number;
constructor(x: number, y: number);
constructor(obj: { x: number, y: number });
constructor();
// tslint:disable-next-line:typedef
constructor(x?, y?) {
if (!(this instanceof Vec2)) {
return new Vec2(x, y);
}
if (typeof x === 'undefined') {
this.x = 0;
this.y = 0;
} else if (typeof x === 'object') {
this.x = x.x;
this.y = x.y;
} else {
this.x = x;
this.y = y;
}
constructor(x=0, y=0) {
this.x = x;
this.y = y;
_ASSERT && Vec2.assert(this);
}
@ -65,25 +55,16 @@ export default class Vec2 {
/** @internal */
static _deserialize(data: any): Vec2 {
const obj = Object.create(Vec2.prototype);
obj.x = data.x;
obj.y = data.y;
return obj;
return new Vec2(data.x, data.y);
}
static zero(): Vec2 {
const obj = Object.create(Vec2.prototype);
obj.x = 0;
obj.y = 0;
return obj;
return new Vec2(0, 0);
}
/** @internal */
static neo(x: number, y: number): Vec2 {
const obj = Object.create(Vec2.prototype);
obj.x = x;
obj.y = y;
return obj;
return new Vec2(x, y);
}
static clone(v: Vec2): Vec2 {
@ -129,40 +110,25 @@ export default class Vec2 {
return this;
}
set(x: number, y: number): Vec2;
set(value: Vec2): Vec2;
set(x: number, y: number) {
// _ASSERT && Math.assert(x);
// _ASSERT && Math.assert(y);
this.x = x;
this.y = y;
return this;
}
/**
* Set this vector to some specified coordinates.
*
* @returns this
*/
// tslint:disable-next-line:typedef
set(x, y?) {
if (typeof x === 'object') {
_ASSERT && Vec2.assert(x);
this.x = x.x;
this.y = x.y;
} else {
_ASSERT && Math.assert(x);
_ASSERT && Math.assert(y);
this.x = x;
this.y = y;
}
setVec2(value: Vec2) {
this.x = value.x;
this.y = value.y;
return this;
}
/**
* @internal
* @deprecated Use setCombine or setMul
*/
wSet(a: number, v: Vec2, b?: number, w?: Vec2): Vec2 {
if (typeof b !== 'undefined' || typeof w !== 'undefined') {
return this.setCombine(a, v, b, w);
} else {
return this.setMul(a, v);
}
}
/**
* Set linear combination of v and w: `a * v + b * w`
*/
@ -203,18 +169,6 @@ export default class Vec2 {
return this;
}
/**
* @internal
* @deprecated Use addCombine or addMul
*/
wAdd(a: number, v: Vec2, b?: number, w?: Vec2): Vec2 {
if (typeof b !== 'undefined' || typeof w !== 'undefined') {
return this.addCombine(a, v, b, w);
} else {
return this.addMul(a, v);
}
}
/**
* Add linear combination of v and w: `a * v + b * w`
*/
@ -244,16 +198,6 @@ export default class Vec2 {
return this;
}
/**
* @deprecated Use subCombine or subMul
*/
wSub(a: number, v: Vec2, b?: number, w?: Vec2): Vec2 {
if (typeof b !== 'undefined' || typeof w !== 'undefined') {
return this.subCombine(a, v, b, w);
} else {
return this.subMul(a, v);
}}
/**
* Subtract linear combination of v and w: `a * v + b * w`
*/
@ -395,53 +339,43 @@ export default class Vec2 {
return v.x * w.x + v.y * w.y;
}
static cross(v: Vec2, w: Vec2): number;
static cross(v: Vec2, w: number): Vec2;
static cross(v: number, w: Vec2): Vec2;
/**
* Perform the cross product on two vectors. In 2D this produces a scalar.
*
*/
static crossVec2Vec2(v: Vec2, w: Vec2): number {
_ASSERT && Vec2.assert(v);
_ASSERT && Vec2.assert(w);
return v.x * w.y - v.y * w.x;
}
/**
* Perform the cross product on a vector and a scalar. In 2D this produces a
* vector.
*/
// tslint:disable-next-line:typedef
static cross(v, w) {
if (typeof w === 'number') {
_ASSERT && Vec2.assert(v);
_ASSERT && Math.assert(w);
return Vec2.neo(w * v.y, -w * v.x);
} else if (typeof v === 'number') {
_ASSERT && Math.assert(v);
_ASSERT && Vec2.assert(w);
return Vec2.neo(-v * w.y, v * w.x);
} else {
_ASSERT && Vec2.assert(v);
_ASSERT && Vec2.assert(w);
return v.x * w.y - v.y * w.x;
}
static crossNumberVec2(v: number, w: Vec2): Vec2 {
_ASSERT && Math.assert(v);
_ASSERT && Vec2.assert(w);
return Vec2.neo(-v * w.y, v * w.x);
}
/**
* Perform the cross product on a vector and a scalar. In 2D this produces a
* vector.
*/
static crossVec2Number(v: Vec2, w: number): Vec2 {
_ASSERT && Vec2.assert(v);
_ASSERT && Math.assert(w);
return Vec2.neo(w * v.y, -w * v.x);
}
static addCross(a: Vec2, v: Vec2, w: number): Vec2;
static addCross(a: Vec2, v: number, w: Vec2): Vec2;
/**
* Returns `a + (v x w)`
*/
// tslint:disable-next-line:typedef
static addCross(a, v, w) {
if (typeof w === 'number') {
_ASSERT && Vec2.assert(v);
_ASSERT && Math.assert(w);
return Vec2.neo(w * v.y + a.x, -w * v.x + a.y);
} else if (typeof v === 'number') {
_ASSERT && Math.assert(v);
_ASSERT && Vec2.assert(w);
return Vec2.neo(-v * w.y + a.x, v * w.x + a.y);
}
_ASSERT && common.assert(false);
_ASSERT && Math.assert(v);
_ASSERT && Vec2.assert(w);
return Vec2.neo(-v * w.y + a.x, v * w.x + a.y);
}
static add(v: Vec2, w: Vec2): Vec2 {
@ -450,15 +384,6 @@ export default class Vec2 {
return Vec2.neo(v.x + w.x, v.y + w.y);
}
/** @internal @deprecated */
static wAdd(a: number, v: Vec2, b: number, w: Vec2): Vec2 {
if (typeof b !== 'undefined' || typeof w !== 'undefined') {
return Vec2.combine(a, v, b, w);
} else {
return Vec2.mul(a, v);
}
}
static combine(a: number, v: Vec2, b: number, w: Vec2): Vec2 {
return Vec2.zero().setCombine(a, v, b, w);
}
@ -469,20 +394,10 @@ export default class Vec2 {
return Vec2.neo(v.x - w.x, v.y - w.y);
}
static mul(a: Vec2, b: number): Vec2;
static mul(a: number, b: Vec2): Vec2;
// tslint:disable-next-line:typedef
static mul(a, b) {
if (typeof a === 'object') {
_ASSERT && Vec2.assert(a);
_ASSERT && Math.assert(b);
return Vec2.neo(a.x * b, a.y * b);
} else if (typeof b === 'object') {
_ASSERT && Math.assert(a);
_ASSERT && Vec2.assert(b);
return Vec2.neo(a * b.x, a * b.y);
}
static mul(a: number, b: Vec2): Vec2 {
_ASSERT && Math.assert(a);
_ASSERT && Vec2.assert(b);
return Vec2.neo(a * b.x, a * b.y);
}
neg(): Vec2 {
@ -535,19 +450,4 @@ export default class Vec2 {
return v;
}
/** @internal @deprecated */
// tslint:disable-next-line:typedef
static scaleFn(x: number, y: number) {
return function(v: Vec2): Vec2 {
return Vec2.neo(v.x * x, v.y * y);
};
}
/** @internal @deprecated */
// tslint:disable-next-line:typedef
static translateFn(x: number, y: number) {
return function(v: Vec2): Vec2 {
return Vec2.neo(v.x + x, v.y + y);
};
}
}

View File

@ -24,38 +24,26 @@
import common from '../util/common';
import Math from './Math';
import factoryConstructor from '../decorators/factoryConstructor';
const _DEBUG = typeof DEBUG === 'undefined' ? false : DEBUG;
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
@factoryConstructor
export default class Vec3 {
x: number;
y: number;
z: number;
constructor(x: number, y: number, z: number);
constructor(obj: { x: number, y: number, z: number });
constructor();
// tslint:disable-next-line:typedef
constructor(x?, y?, z?) {
if (!(this instanceof Vec3)) {
return new Vec3(x, y, z);
}
if (typeof x === 'undefined') {
this.x = 0;
this.y = 0;
this.z = 0;
} else if (typeof x === 'object') {
this.x = x.x;
this.y = x.y;
this.z = x.z;
} else {
this.x = x;
this.y = y;
this.z = z;
}
constructor(x=0, y=0, z=0) {
this.x = x;
this.y = y;
this.z = z;
_ASSERT && Vec3.assert(this);
}
@ -70,28 +58,16 @@ export default class Vec3 {
/** @internal */
static _deserialize(data: any): Vec3 {
const obj = Object.create(Vec3.prototype);
obj.x = data.x;
obj.y = data.y;
obj.z = data.z;
return obj;
return new Vec3(data.x, data.y, data.z);
}
/** @internal */
static neo(x: number, y: number, z: number): Vec3 {
const obj = Object.create(Vec3.prototype);
obj.x = x;
obj.y = y;
obj.z = z;
return obj;
return new Vec3(x, y, z);
}
static zero(): Vec3 {
const obj = Object.create(Vec3.prototype);
obj.x = 0;
obj.y = 0;
obj.z = 0;
return obj;
return new Vec3(0, 0, 0);
}
static clone(v: Vec3): Vec3 {
@ -161,7 +137,7 @@ export default class Vec3 {
_ASSERT && Vec3.assert(v);
_ASSERT && Vec3.assert(w);
return v === w ||
typeof v === 'object' && v !== null &&
typeof v === 'object' && v !== null && // TODO remove with strictNullChecks
typeof w === 'object' && w !== null &&
v.x === w.x && v.y === w.y && v.z === w.z;
}

View File

@ -0,0 +1,23 @@
/**
* Adds a factory constructor by transforming a class
* ```
* class A {
* constructor(param: number) {
* // ...
* }
* }
* ```
* into
* ```
* class A {
* constructor(param: number) {
* if(!(this instaceof A)) {
* return new A(param);
* }
* // ...
* }
* }
* ```
* **Note**: Only supported when `target` is ES5 or lower.
*/
export default function factoryConstructor(target: Function) { }

View File

@ -606,7 +606,7 @@ export default class Body {
advance(alpha: number): void {
// Advance to the new safe time. This doesn't sync the broad-phase.
this.m_sweep.advance(alpha);
this.m_sweep.c.set(this.m_sweep.c0);
this.m_sweep.c.setVec2(this.m_sweep.c0);
this.m_sweep.a = this.m_sweep.a0;
this.m_sweep.getTransform(this.m_xf, 1);
}
@ -663,7 +663,7 @@ export default class Body {
*/
getLinearVelocityFromWorldPoint(worldPoint: Vec2): Vec2 {
const localCenter = Vec2.sub(worldPoint, this.m_sweep.c);
return Vec2.add(this.m_linearVelocity, Vec2.cross(this.m_angularVelocity,
return Vec2.add(this.m_linearVelocity, Vec2.crossNumberVec2(this.m_angularVelocity,
localCenter));
}
@ -688,7 +688,7 @@ export default class Body {
if (Vec2.dot(v, v) > 0.0) {
this.setAwake(true);
}
this.m_linearVelocity.set(v);
this.m_linearVelocity.setVec2(v);
}
/**
@ -767,7 +767,7 @@ export default class Body {
getMassData(data: MassData): void {
data.mass = this.m_mass;
data.I = this.getInertia();
data.center.set(this.m_sweep.localCenter);
data.center.setVec2(this.m_sweep.localCenter);
}
/**
@ -785,8 +785,8 @@ export default class Body {
// Static and kinematic bodies have zero mass.
if (this.isStatic() || this.isKinematic()) {
this.m_sweep.c0.set(this.m_xf.p);
this.m_sweep.c.set(this.m_xf.p);
this.m_sweep.c0.setVec2(this.m_xf.p);
this.m_sweep.c.setVec2(this.m_xf.p);
this.m_sweep.a0 = this.m_sweep.a;
return;
}
@ -834,7 +834,7 @@ export default class Body {
this.m_sweep.setLocalCenter(localCenter, this.m_xf);
// Update center of mass velocity.
this.m_linearVelocity.add(Vec2.cross(this.m_angularVelocity, Vec2.sub(
this.m_linearVelocity.add(Vec2.crossNumberVec2(this.m_angularVelocity, Vec2.sub(
this.m_sweep.c, oldCenter)));
}
@ -879,7 +879,7 @@ export default class Body {
this.m_sweep.setLocalCenter(massData.center, this.m_xf);
// Update center of mass velocity.
this.m_linearVelocity.add(Vec2.cross(this.m_angularVelocity, Vec2.sub(
this.m_linearVelocity.add(Vec2.crossNumberVec2(this.m_angularVelocity, Vec2.sub(
this.m_sweep.c, oldCenter)));
}
@ -902,7 +902,7 @@ export default class Body {
// Don't accumulate a force if the body is sleeping.
if (this.m_awakeFlag) {
this.m_force.add(force);
this.m_torque += Vec2.cross(Vec2.sub(point, this.m_sweep.c), force);
this.m_torque += Vec2.crossVec2Vec2(Vec2.sub(point, this.m_sweep.c), force);
}
}
@ -965,7 +965,7 @@ export default class Body {
// Don't accumulate velocity if the body is sleeping
if (this.m_awakeFlag) {
this.m_linearVelocity.addMul(this.m_invMass, impulse);
this.m_angularVelocity += this.m_invI * Vec2.cross(Vec2.sub(point, this.m_sweep.c), impulse);
this.m_angularVelocity += this.m_invI * Vec2.crossVec2Vec2(Vec2.sub(point, this.m_sweep.c), impulse);
}
}

View File

@ -573,15 +573,15 @@ export default class Contact {
for (let j = 0; j < this.p_pointCount; ++j) {
const xfA = Transform.identity();
const xfB = Transform.identity();
xfA.q.set(aA);
xfB.q.set(aB);
xfA.q.setAngle(aA);
xfB.q.setAngle(aB);
xfA.p = Vec2.sub(cA, Rot.mulVec2(xfA.q, localCenterA));
xfB.p = Vec2.sub(cB, Rot.mulVec2(xfB.q, localCenterB));
// PositionSolverManifold
let normal;
let point;
let separation;
let normal: Vec2;
let point: Vec2;
let separation: number;
switch (this.p_type) {
case ManifoldType.e_circles: {
const pointA = Transform.mulVec2(xfA, this.p_localPoint);
@ -629,8 +629,8 @@ export default class Contact {
const C = Math.clamp(baumgarte * (separation + linearSlop), -maxLinearCorrection, 0.0);
// Compute the effective mass.
const rnA = Vec2.cross(rA, normal);
const rnB = Vec2.cross(rB, normal);
const rnA = Vec2.crossVec2Vec2(rA, normal);
const rnB = Vec2.crossVec2Vec2(rB, normal);
const K = mA + mB + iA * rnA * rnA + iB * rnB * rnB;
// Compute normal impulse
@ -639,16 +639,16 @@ export default class Contact {
const P = Vec2.mul(impulse, normal);
cA.subMul(mA, P);
aA -= iA * Vec2.cross(rA, P);
aA -= iA * Vec2.crossVec2Vec2(rA, P);
cB.addMul(mB, P);
aB += iB * Vec2.cross(rB, P);
aB += iB * Vec2.crossVec2Vec2(rB, P);
}
positionA.c.set(cA);
positionA.c.setVec2(cA);
positionA.a = aA;
positionB.c.set(cB);
positionB.c.setVec2(cB);
positionB.a = aB;
return minSeparation;
@ -692,32 +692,32 @@ export default class Contact {
const xfA = Transform.identity();
const xfB = Transform.identity();
xfA.q.set(aA);
xfB.q.set(aB);
xfA.q.setAngle(aA);
xfB.q.setAngle(aB);
xfA.p.setCombine(1, cA, -1, Rot.mulVec2(xfA.q, localCenterA));
xfB.p.setCombine(1, cB, -1, Rot.mulVec2(xfB.q, localCenterB));
const worldManifold = manifold.getWorldManifold(null, xfA, radiusA, xfB, radiusB);
this.v_normal.set(worldManifold.normal);
this.v_normal.setVec2(worldManifold.normal);
for (let j = 0; j < this.v_pointCount; ++j) {
const vcp = this.v_points[j]; // VelocityConstraintPoint
vcp.rA.set(Vec2.sub(worldManifold.points[j], cA));
vcp.rB.set(Vec2.sub(worldManifold.points[j], cB));
vcp.rA.setVec2(Vec2.sub(worldManifold.points[j], cA));
vcp.rB.setVec2(Vec2.sub(worldManifold.points[j], cB));
const rnA = Vec2.cross(vcp.rA, this.v_normal);
const rnB = Vec2.cross(vcp.rB, this.v_normal);
const rnA = Vec2.crossVec2Vec2(vcp.rA, this.v_normal);
const rnB = Vec2.crossVec2Vec2(vcp.rB, this.v_normal);
const kNormal = mA + mB + iA * rnA * rnA + iB * rnB * rnB;
vcp.normalMass = kNormal > 0.0 ? 1.0 / kNormal : 0.0;
const tangent = Vec2.cross(this.v_normal, 1.0);
const tangent = Vec2.crossVec2Number(this.v_normal, 1.0);
const rtA = Vec2.cross(vcp.rA, tangent);
const rtB = Vec2.cross(vcp.rB, tangent);
const rtA = Vec2.crossVec2Vec2(vcp.rA, tangent);
const rtB = Vec2.crossVec2Vec2(vcp.rB, tangent);
const kTangent = mA + mB + iA * rtA * rtA + iB * rtB * rtB;
@ -726,9 +726,9 @@ export default class Contact {
// Setup a velocity bias for restitution.
vcp.velocityBias = 0.0;
const vRel = Vec2.dot(this.v_normal, vB)
+ Vec2.dot(this.v_normal, Vec2.cross(wB, vcp.rB))
+ Vec2.dot(this.v_normal, Vec2.crossNumberVec2(wB, vcp.rB))
- Vec2.dot(this.v_normal, vA)
- Vec2.dot(this.v_normal, Vec2.cross(wA, vcp.rA));
- Vec2.dot(this.v_normal, Vec2.crossNumberVec2(wA, vcp.rA));
if (vRel < -Settings.velocityThreshold) {
vcp.velocityBias = -this.v_restitution * vRel;
}
@ -739,10 +739,10 @@ export default class Contact {
const vcp1 = this.v_points[0]; // VelocityConstraintPoint
const vcp2 = this.v_points[1]; // VelocityConstraintPoint
const rn1A = Vec2.cross(vcp1.rA, this.v_normal);
const rn1B = Vec2.cross(vcp1.rB, this.v_normal);
const rn2A = Vec2.cross(vcp2.rA, this.v_normal);
const rn2B = Vec2.cross(vcp2.rB, this.v_normal);
const rn1A = Vec2.crossVec2Vec2(vcp1.rA, this.v_normal);
const rn1B = Vec2.crossVec2Vec2(vcp1.rB, this.v_normal);
const rn2A = Vec2.crossVec2Vec2(vcp2.rA, this.v_normal);
const rn2B = Vec2.crossVec2Vec2(vcp2.rB, this.v_normal);
const k11 = mA + mB + iA * rn1A * rn1A + iB * rn1B * rn1B;
const k22 = mA + mB + iA * rn2A * rn2A + iB * rn2B * rn2B;
@ -762,14 +762,14 @@ export default class Contact {
}
}
positionA.c.set(cA);
positionA.c.setVec2(cA);
positionA.a = aA;
velocityA.v.set(vA);
velocityA.v.setVec2(vA);
velocityA.w = wA;
positionB.c.set(cB);
positionB.c.setVec2(cB);
positionB.a = aB;
velocityB.v.set(vB);
velocityB.v.setVec2(vB);
velocityB.w = wB;
}
@ -796,21 +796,21 @@ export default class Contact {
let wB = velocityB.w;
const normal = this.v_normal;
const tangent = Vec2.cross(normal, 1.0);
const tangent = Vec2.crossVec2Number(normal, 1.0);
for (let j = 0; j < this.v_pointCount; ++j) {
const vcp = this.v_points[j]; // VelocityConstraintPoint
const P = Vec2.combine(vcp.normalImpulse, normal, vcp.tangentImpulse, tangent);
wA -= iA * Vec2.cross(vcp.rA, P);
wA -= iA * Vec2.crossVec2Vec2(vcp.rA, P);
vA.subMul(mA, P);
wB += iB * Vec2.cross(vcp.rB, P);
wB += iB * Vec2.crossVec2Vec2(vcp.rB, P);
vB.addMul(mB, P);
}
velocityA.v.set(vA);
velocityA.v.setVec2(vA);
velocityA.w = wA;
velocityB.v.set(vB);
velocityB.v.setVec2(vB);
velocityB.w = wB;
}
@ -843,7 +843,7 @@ export default class Contact {
let wB = velocityB.w;
const normal = this.v_normal;
const tangent = Vec2.cross(normal, 1.0);
const tangent = Vec2.crossVec2Number(normal, 1.0);
const friction = this.v_friction;
_ASSERT && common.assert(this.v_pointCount == 1 || this.v_pointCount == 2);
@ -855,8 +855,8 @@ export default class Contact {
// Relative velocity at contact
const dv = Vec2.zero();
dv.addCombine(1, vB, 1, Vec2.cross(wB, vcp.rB));
dv.subCombine(1, vA, 1, Vec2.cross(wA, vcp.rA));
dv.addCombine(1, vB, 1, Vec2.crossNumberVec2(wB, vcp.rB));
dv.subCombine(1, vA, 1, Vec2.crossNumberVec2(wA, vcp.rA));
// Compute tangent force
const vt = Vec2.dot(dv, tangent) - this.v_tangentSpeed;
@ -872,10 +872,10 @@ export default class Contact {
const P = Vec2.mul(lambda, tangent);
vA.subMul(mA, P);
wA -= iA * Vec2.cross(vcp.rA, P);
wA -= iA * Vec2.crossVec2Vec2(vcp.rA, P);
vB.addMul(mB, P);
wB += iB * Vec2.cross(vcp.rB, P);
wB += iB * Vec2.crossVec2Vec2(vcp.rB, P);
}
// Solve normal constraints
@ -885,8 +885,8 @@ export default class Contact {
// Relative velocity at contact
const dv = Vec2.zero();
dv.addCombine(1, vB, 1, Vec2.cross(wB, vcp.rB));
dv.subCombine(1, vA, 1, Vec2.cross(wA, vcp.rA));
dv.addCombine(1, vB, 1, Vec2.crossNumberVec2(wB, vcp.rB));
dv.subCombine(1, vA, 1, Vec2.crossNumberVec2(wA, vcp.rA));
// Compute normal impulse
const vn = Vec2.dot(dv, normal);
@ -901,10 +901,10 @@ export default class Contact {
const P = Vec2.mul(lambda, normal);
vA.subMul(mA, P);
wA -= iA * Vec2.cross(vcp.rA, P);
wA -= iA * Vec2.crossVec2Vec2(vcp.rA, P);
vB.addMul(mB, P);
wB += iB * Vec2.cross(vcp.rB, P);
wB += iB * Vec2.crossVec2Vec2(vcp.rB, P);
}
} else {
// Block solver developed in collaboration with Dirk Gregorius (back in
@ -955,8 +955,8 @@ export default class Contact {
_ASSERT && common.assert(a.x >= 0.0 && a.y >= 0.0);
// Relative velocity at contact
let dv1 = Vec2.zero().add(vB).add(Vec2.cross(wB, vcp1.rB)).sub(vA).sub(Vec2.cross(wA, vcp1.rA));
let dv2 = Vec2.zero().add(vB).add(Vec2.cross(wB, vcp2.rB)).sub(vA).sub(Vec2.cross(wA, vcp2.rA));
let dv1 = Vec2.zero().add(vB).add(Vec2.crossNumberVec2(wB, vcp1.rB)).sub(vA).sub(Vec2.crossNumberVec2(wA, vcp1.rA));
let dv2 = Vec2.zero().add(vB).add(Vec2.crossNumberVec2(wB, vcp2.rB)).sub(vA).sub(Vec2.crossNumberVec2(wA, vcp2.rA));
// Compute normal velocity
let vn1 = Vec2.dot(dv1, normal);
@ -991,10 +991,10 @@ export default class Contact {
const P2 = Vec2.mul(d.y, normal);
vA.subCombine(mA, P1, mA, P2);
wA -= iA * (Vec2.cross(vcp1.rA, P1) + Vec2.cross(vcp2.rA, P2));
wA -= iA * (Vec2.crossVec2Vec2(vcp1.rA, P1) + Vec2.crossVec2Vec2(vcp2.rA, P2));
vB.addCombine(mB, P1, mB, P2);
wB += iB * (Vec2.cross(vcp1.rB, P1) + Vec2.cross(vcp2.rB, P2));
wB += iB * (Vec2.crossVec2Vec2(vcp1.rB, P1) + Vec2.crossVec2Vec2(vcp2.rB, P2));
// Accumulate
vcp1.normalImpulse = x.x;
@ -1002,8 +1002,8 @@ export default class Contact {
if (DEBUG_SOLVER) {
// Postconditions
dv1 = Vec2.sub(Vec2.add(vB, Vec2.cross(wB, vcp1.rB)), Vec2.add(vA, Vec2.cross(wA, vcp1.rA)));
dv2 = Vec2.sub(Vec2.add(vB, Vec2.cross(wB, vcp2.rB)), Vec2.add(vA, Vec2.cross(wA, vcp2.rA)));
dv1 = Vec2.sub(Vec2.add(vB, Vec2.crossNumberVec2(wB, vcp1.rB)), Vec2.add(vA, Vec2.crossNumberVec2(wA, vcp1.rA)));
dv2 = Vec2.sub(Vec2.add(vB, Vec2.crossNumberVec2(wB, vcp2.rB)), Vec2.add(vA, Vec2.crossNumberVec2(wA, vcp2.rA)));
// Compute normal velocity
vn1 = Vec2.dot(dv1, normal);
@ -1034,10 +1034,10 @@ export default class Contact {
const P1 = Vec2.mul(d.x, normal);
const P2 = Vec2.mul(d.y, normal);
vA.subCombine(mA, P1, mA, P2);
wA -= iA * (Vec2.cross(vcp1.rA, P1) + Vec2.cross(vcp2.rA, P2));
wA -= iA * (Vec2.crossVec2Vec2(vcp1.rA, P1) + Vec2.crossVec2Vec2(vcp2.rA, P2));
vB.addCombine(mB, P1, mB, P2);
wB += iB * (Vec2.cross(vcp1.rB, P1) + Vec2.cross(vcp2.rB, P2));
wB += iB * (Vec2.crossVec2Vec2(vcp1.rB, P1) + Vec2.crossVec2Vec2(vcp2.rB, P2));
// Accumulate
vcp1.normalImpulse = x.x;
@ -1045,8 +1045,8 @@ export default class Contact {
if (DEBUG_SOLVER) {
// Postconditions
const dv1B = Vec2.add(vB, Vec2.cross(wB, vcp1.rB));
const dv1A = Vec2.add(vA, Vec2.cross(wA, vcp1.rA));
const dv1B = Vec2.add(vB, Vec2.crossNumberVec2(wB, vcp1.rB));
const dv1A = Vec2.add(vA, Vec2.crossNumberVec2(wA, vcp1.rA));
const dv1 = Vec2.sub(dv1B, dv1A);
// Compute normal velocity
@ -1076,10 +1076,10 @@ export default class Contact {
const P1 = Vec2.mul(d.x, normal);
const P2 = Vec2.mul(d.y, normal);
vA.subCombine(mA, P1, mA, P2);
wA -= iA * (Vec2.cross(vcp1.rA, P1) + Vec2.cross(vcp2.rA, P2));
wA -= iA * (Vec2.crossVec2Vec2(vcp1.rA, P1) + Vec2.crossVec2Vec2(vcp2.rA, P2));
vB.addCombine(mB, P1, mB, P2);
wB += iB * (Vec2.cross(vcp1.rB, P1) + Vec2.cross(vcp2.rB, P2));
wB += iB * (Vec2.crossVec2Vec2(vcp1.rB, P1) + Vec2.crossVec2Vec2(vcp2.rB, P2));
// Accumulate
vcp1.normalImpulse = x.x;
@ -1087,8 +1087,8 @@ export default class Contact {
if (DEBUG_SOLVER) {
// Postconditions
const dv2B = Vec2.add(vB, Vec2.cross(wB, vcp2.rB));
const dv2A = Vec2.add(vA, Vec2.cross(wA, vcp2.rA));
const dv2B = Vec2.add(vB, Vec2.crossNumberVec2(wB, vcp2.rB));
const dv2A = Vec2.add(vA, Vec2.crossNumberVec2(wA, vcp2.rA));
const dv1 = Vec2.sub(dv2B, dv2A);
// Compute normal velocity
@ -1118,10 +1118,10 @@ export default class Contact {
const P1 = Vec2.mul(d.x, normal);
const P2 = Vec2.mul(d.y, normal);
vA.subCombine(mA, P1, mA, P2);
wA -= iA * (Vec2.cross(vcp1.rA, P1) + Vec2.cross(vcp2.rA, P2));
wA -= iA * (Vec2.crossVec2Vec2(vcp1.rA, P1) + Vec2.crossVec2Vec2(vcp2.rA, P2));
vB.addCombine(mB, P1, mB, P2);
wB += iB * (Vec2.cross(vcp1.rB, P1) + Vec2.cross(vcp2.rB, P2));
wB += iB * (Vec2.crossVec2Vec2(vcp1.rB, P1) + Vec2.crossVec2Vec2(vcp2.rB, P2));
// Accumulate
vcp1.normalImpulse = x.x;
@ -1136,10 +1136,10 @@ export default class Contact {
}
}
velocityA.v.set(vA);
velocityA.v.setVec2(vA);
velocityA.w = wA;
velocityB.v.set(vB);
velocityB.v.setVec2(vB);
velocityB.w = wB;
}

View File

@ -40,8 +40,8 @@ export default class Position {
}
getTransform(xf: Transform, p: Vec2): Transform {
xf.q.set(this.a);
xf.p.set(Vec2.sub(this.c, Rot.mulVec2(xf.q, p)));
xf.q.setAngle(this.a);
xf.p.setVec2(Vec2.sub(this.c, Rot.mulVec2(xf.q, p)));
return xf;
}
}

View File

@ -299,7 +299,7 @@ export default class Solver {
let w = body.m_angularVelocity;
// Store positions for continuous collision.
body.m_sweep.c0.set(body.m_sweep.c);
body.m_sweep.c0.setVec2(body.m_sweep.c);
body.m_sweep.a0 = body.m_sweep.a;
if (body.isDynamic()) {
@ -408,9 +408,9 @@ export default class Solver {
c.addMul(h, v);
a += h * w;
body.c_position.c.set(c);
body.c_position.c.setVec2(c);
body.c_position.a = a;
body.c_velocity.v.set(v);
body.c_velocity.v.setVec2(v);
body.c_velocity.w = w;
}
@ -449,9 +449,9 @@ export default class Solver {
for (let i = 0; i < this.m_bodies.length; ++i) {
const body = this.m_bodies[i];
body.m_sweep.c.set(body.c_position.c);
body.m_sweep.c.setVec2(body.c_position.c);
body.m_sweep.a = body.c_position.a;
body.m_linearVelocity.set(body.c_velocity.v);
body.m_linearVelocity.setVec2(body.c_velocity.v);
body.m_angularVelocity = body.c_velocity.w;
body.synchronizeTransform();
}
@ -785,9 +785,9 @@ export default class Solver {
// Initialize the body state.
for (let i = 0; i < this.m_bodies.length; ++i) {
const body = this.m_bodies[i];
body.c_position.c.set(body.m_sweep.c);
body.c_position.c.setVec2(body.m_sweep.c);
body.c_position.a = body.m_sweep.a;
body.c_velocity.v.set(body.m_linearVelocity);
body.c_velocity.v.setVec2(body.m_linearVelocity);
body.c_velocity.w = body.m_angularVelocity;
}
@ -843,9 +843,9 @@ export default class Solver {
}
// Leap of faith to new safe state.
toiA.m_sweep.c0.set(toiA.c_position.c);
toiA.m_sweep.c0.setVec2(toiA.c_position.c);
toiA.m_sweep.a0 = toiA.c_position.a;
toiB.m_sweep.c0.set(toiB.c_position.c);
toiB.m_sweep.c0.setVec2(toiB.c_position.c);
toiB.m_sweep.a0 = toiB.c_position.a;
// No warm starting is needed for TOI events because warm

View File

@ -33,6 +33,7 @@ import Contact from './Contact';
import AABB, { RayCastInput, RayCastOutput } from "../collision/AABB";
import Fixture, { FixtureProxy } from "./Fixture";
import Manifold from "../collision/Manifold";
import factoryConstructor from '../decorators/factoryConstructor';
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
@ -92,6 +93,7 @@ export type WorldRayCastCallback = (fixture: Fixture, point: Vec2, normal: Vec2,
*/
export type WorldAABBQueryCallback = (fixture: Fixture) => boolean;
@factoryConstructor
export default class World {
/** @internal */ m_solver: Solver;
/** @internal */ m_broadPhase: BroadPhase;
@ -124,10 +126,6 @@ export default class World {
* @param def World definition or gravity vector.
*/
constructor(def?: WorldDef | Vec2 | null) {
if (!(this instanceof World)) {
return new World(def);
}
if (def && Vec2.isValid(def)) {
def = { gravity: def as Vec2 };
}

View File

@ -30,6 +30,7 @@ import Rot from '../../common/Rot';
import Joint, { JointOpt, JointDef } from '../Joint';
import Body from '../Body';
import { TimeStep } from "../Solver";
import factoryConstructor from '../../decorators/factoryConstructor';
/**
* Distance joint definition. This requires defining an anchor point on both
@ -82,6 +83,7 @@ const DEFAULTS = {
* @param anchorA Anchor A in global coordination.
* @param anchorB Anchor B in global coordination.
*/
@factoryConstructor
export default class DistanceJoint extends Joint {
static TYPE = 'distance-joint' as const;
@ -110,17 +112,12 @@ export default class DistanceJoint extends Joint {
constructor(def: DistanceJointDef);
constructor(def: DistanceJointOpt, bodyA: Body, bodyB: Body, anchorA: Vec2, anchorB: Vec2);
constructor(def: DistanceJointDef, bodyA?: Body, bodyB?: Body, anchorA?: Vec2, anchorB?: Vec2) {
// @ts-ignore
if (!(this instanceof DistanceJoint)) {
return new DistanceJoint(def, bodyA, bodyB, anchorA, anchorB);
}
// order of constructor arguments is changed in v0.2
if (bodyB && anchorA && ('m_type' in anchorA) && ('x' in bodyB) && ('y' in bodyB)) {
const temp = bodyB;
bodyB = anchorA;
anchorA = temp;
}
// if (bodyB && anchorA && ('m_type' in anchorA) && ('x' in bodyB) && ('y' in bodyB)) {
// const temp = bodyB;
// bodyB = anchorA;
// anchorA = temp;
// }
def = options(def, DEFAULTS);
super(def, bodyA, bodyB);
@ -195,15 +192,15 @@ export default class DistanceJoint extends Joint {
length?: number,
}): void {
if (def.anchorA) {
this.m_localAnchorA.set(this.m_bodyA.getLocalPoint(def.anchorA));
this.m_localAnchorA.setVec2(this.m_bodyA.getLocalPoint(def.anchorA));
} else if (def.localAnchorA) {
this.m_localAnchorA.set(def.localAnchorA);
this.m_localAnchorA.setVec2(def.localAnchorA);
}
if (def.anchorB) {
this.m_localAnchorB.set(this.m_bodyB.getLocalPoint(def.anchorB));
this.m_localAnchorB.setVec2(this.m_bodyB.getLocalPoint(def.anchorB));
} else if (def.localAnchorB) {
this.m_localAnchorB.set(def.localAnchorB);
this.m_localAnchorB.setVec2(def.localAnchorB);
}
if (def.length > 0) {
@ -323,8 +320,8 @@ export default class DistanceJoint extends Joint {
this.m_u.set(0.0, 0.0);
}
const crAu = Vec2.cross(this.m_rA, this.m_u);
const crBu = Vec2.cross(this.m_rB, this.m_u);
const crAu = Vec2.crossVec2Vec2(this.m_rA, this.m_u);
const crBu = Vec2.crossVec2Vec2(this.m_rB, this.m_u);
let invMass = this.m_invMassA + this.m_invIA * crAu * crAu + this.m_invMassB
+ this.m_invIB * crBu * crBu;
@ -363,18 +360,18 @@ export default class DistanceJoint extends Joint {
const P = Vec2.mul(this.m_impulse, this.m_u);
vA.subMul(this.m_invMassA, P);
wA -= this.m_invIA * Vec2.cross(this.m_rA, P);
wA -= this.m_invIA * Vec2.crossVec2Vec2(this.m_rA, P);
vB.addMul(this.m_invMassB, P);
wB += this.m_invIB * Vec2.cross(this.m_rB, P);
wB += this.m_invIB * Vec2.crossVec2Vec2(this.m_rB, P);
} else {
this.m_impulse = 0.0;
}
this.m_bodyA.c_velocity.v.set(vA);
this.m_bodyA.c_velocity.v.setVec2(vA);
this.m_bodyA.c_velocity.w = wA;
this.m_bodyB.c_velocity.v.set(vB);
this.m_bodyB.c_velocity.v.setVec2(vB);
this.m_bodyB.c_velocity.w = wB;
}
@ -385,8 +382,8 @@ export default class DistanceJoint extends Joint {
let wB = this.m_bodyB.c_velocity.w;
// Cdot = dot(u, v + cross(w, r))
const vpA = Vec2.add(vA, Vec2.cross(wA, this.m_rA));
const vpB = Vec2.add(vB, Vec2.cross(wB, this.m_rB));
const vpA = Vec2.add(vA, Vec2.crossNumberVec2(wA, this.m_rA));
const vpB = Vec2.add(vB, Vec2.crossNumberVec2(wB, this.m_rB));
const Cdot = Vec2.dot(this.m_u, vpB) - Vec2.dot(this.m_u, vpA);
const impulse = -this.m_mass
@ -395,13 +392,13 @@ export default class DistanceJoint extends Joint {
const P = Vec2.mul(impulse, this.m_u);
vA.subMul(this.m_invMassA, P);
wA -= this.m_invIA * Vec2.cross(this.m_rA, P);
wA -= this.m_invIA * Vec2.crossVec2Vec2(this.m_rA, P);
vB.addMul(this.m_invMassB, P);
wB += this.m_invIB * Vec2.cross(this.m_rB, P);
wB += this.m_invIB * Vec2.crossVec2Vec2(this.m_rB, P);
this.m_bodyA.c_velocity.v.set(vA);
this.m_bodyA.c_velocity.v.setVec2(vA);
this.m_bodyA.c_velocity.w = wA;
this.m_bodyB.c_velocity.v.set(vB);
this.m_bodyB.c_velocity.v.setVec2(vB);
this.m_bodyB.c_velocity.w = wB;
}
@ -435,13 +432,13 @@ export default class DistanceJoint extends Joint {
const P = Vec2.mul(impulse, u);
cA.subMul(this.m_invMassA, P);
aA -= this.m_invIA * Vec2.cross(rA, P);
aA -= this.m_invIA * Vec2.crossVec2Vec2(rA, P);
cB.addMul(this.m_invMassB, P);
aB += this.m_invIB * Vec2.cross(rB, P);
aB += this.m_invIB * Vec2.crossVec2Vec2(rB, P);
this.m_bodyA.c_position.c.set(cA);
this.m_bodyA.c_position.c.setVec2(cA);
this.m_bodyA.c_position.a = aA;
this.m_bodyB.c_position.c.set(cB);
this.m_bodyB.c_position.c.setVec2(cB);
this.m_bodyB.c_position.a = aB;
return Math.abs(C) < Settings.linearSlop;

View File

@ -31,6 +31,7 @@ import Rot from '../../common/Rot';
import Joint, { JointOpt, JointDef } from '../Joint';
import Body from '../Body';
import { TimeStep } from "../Solver";
import factoryConstructor from '../../decorators/factoryConstructor';
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
@ -74,6 +75,7 @@ const DEFAULTS = {
*
* @param anchor Anchor in global coordination.
*/
@factoryConstructor
export default class FrictionJoint extends Joint {
static TYPE = 'friction-joint' as const;
@ -103,11 +105,6 @@ export default class FrictionJoint extends Joint {
constructor(def: FrictionJointDef);
constructor(def: FrictionJointOpt, bodyA: Body, bodyB: Body, anchor: Vec2);
constructor(def: FrictionJointDef, bodyA?: Body, bodyB?: Body, anchor?: Vec2) {
// @ts-ignore
if (!(this instanceof FrictionJoint)) {
return new FrictionJoint(def, bodyA, bodyB, anchor);
}
def = options(def, DEFAULTS);
super(def, bodyA, bodyB);
bodyA = this.m_bodyA;
@ -170,15 +167,15 @@ export default class FrictionJoint extends Joint {
localAnchorB?: Vec2,
}): void {
if (def.anchorA) {
this.m_localAnchorA.set(this.m_bodyA.getLocalPoint(def.anchorA));
this.m_localAnchorA.setVec2(this.m_bodyA.getLocalPoint(def.anchorA));
} else if (def.localAnchorA) {
this.m_localAnchorA.set(def.localAnchorA);
this.m_localAnchorA.setVec2(def.localAnchorA);
}
if (def.anchorB) {
this.m_localAnchorB.set(this.m_bodyB.getLocalPoint(def.anchorB));
this.m_localAnchorB.setVec2(this.m_bodyB.getLocalPoint(def.anchorB));
} else if (def.localAnchorB) {
this.m_localAnchorB.set(def.localAnchorB);
this.m_localAnchorB.setVec2(def.localAnchorB);
}
}
@ -315,10 +312,10 @@ export default class FrictionJoint extends Joint {
const P = Vec2.neo(this.m_linearImpulse.x, this.m_linearImpulse.y);
vA.subMul(mA, P);
wA -= iA * (Vec2.cross(this.m_rA, P) + this.m_angularImpulse);
wA -= iA * (Vec2.crossVec2Vec2(this.m_rA, P) + this.m_angularImpulse);
vB.addMul(mB, P);
wB += iB * (Vec2.cross(this.m_rB, P) + this.m_angularImpulse);
wB += iB * (Vec2.crossVec2Vec2(this.m_rB, P) + this.m_angularImpulse);
} else {
this.m_linearImpulse.setZero();
@ -361,8 +358,8 @@ export default class FrictionJoint extends Joint {
// Solve linear friction
{
const Cdot = Vec2.sub(Vec2.add(vB, Vec2.cross(wB, this.m_rB)), Vec2.add(vA,
Vec2.cross(wA, this.m_rA))); // Vec2
const Cdot = Vec2.sub(Vec2.add(vB, Vec2.crossNumberVec2(wB, this.m_rB)), Vec2.add(vA,
Vec2.crossNumberVec2(wA, this.m_rA))); // Vec2
let impulse = Vec2.neg(Mat22.mulVec2(this.m_linearMass, Cdot)); // Vec2
const oldImpulse = this.m_linearImpulse; // Vec2
@ -378,10 +375,10 @@ export default class FrictionJoint extends Joint {
impulse = Vec2.sub(this.m_linearImpulse, oldImpulse);
vA.subMul(mA, impulse);
wA -= iA * Vec2.cross(this.m_rA, impulse);
wA -= iA * Vec2.crossVec2Vec2(this.m_rA, impulse);
vB.addMul(mB, impulse);
wB += iB * Vec2.cross(this.m_rB, impulse);
wB += iB * Vec2.crossVec2Vec2(this.m_rB, impulse);
}
this.m_bodyA.c_velocity.v = vA;

View File

@ -33,6 +33,7 @@ import Body from '../Body';
import RevoluteJoint from './RevoluteJoint';
import PrismaticJoint from './PrismaticJoint';
import { TimeStep } from "../Solver";
import factoryConstructor from '../../decorators/factoryConstructor';
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
@ -78,6 +79,7 @@ const DEFAULTS = {
* This definition requires two existing revolute or prismatic joints (any
* combination will work).
*/
@factoryConstructor
export default class GearJoint extends Joint {
static TYPE = 'gear-joint' as const;
@ -124,11 +126,6 @@ export default class GearJoint extends Joint {
constructor(def: GearJointDef);
constructor(def: GearJointOpt, bodyA: Body, bodyB: Body, joint1: RevoluteJoint | PrismaticJoint, joint2: RevoluteJoint | PrismaticJoint, ratio?: number);
constructor(def: GearJointDef, bodyA?: Body, bodyB?: Body, joint1?: RevoluteJoint | PrismaticJoint, joint2?: RevoluteJoint | PrismaticJoint, ratio?: number) {
// @ts-ignore
if (!(this instanceof GearJoint)) {
return new GearJoint(def, bodyA, bodyB, joint1, joint2, ratio);
}
def = options(def, DEFAULTS);
super(def, bodyA, bodyB);
bodyA = this.m_bodyA;
@ -181,7 +178,7 @@ export default class GearJoint extends Joint {
this.m_localAxisC = prismatic.m_localXAxisA;
const pC = this.m_localAnchorC;
const pA = Rot.mulTVec2(xfC.q, Vec2.add(Rot.mul(xfA.q, this.m_localAnchorA), Vec2.sub(xfA.p, xfC.p)));
const pA = Rot.mulTVec2(xfC.q, Vec2.add(Rot.mulVec2(xfA.q, this.m_localAnchorA), Vec2.sub(xfA.p, xfC.p)));
coordinateA = Vec2.dot(pA, this.m_localAxisC) - Vec2.dot(pC, this.m_localAxisC);
}
@ -210,7 +207,7 @@ export default class GearJoint extends Joint {
this.m_localAxisD = prismatic.m_localXAxisA;
const pD = this.m_localAnchorD;
const pB = Rot.mulTVec2(xfD.q, Vec2.add(Rot.mul(xfB.q, this.m_localAnchorB), Vec2.sub(xfB.p, xfD.p)));
const pB = Rot.mulTVec2(xfD.q, Vec2.add(Rot.mulVec2(xfB.q, this.m_localAnchorB), Vec2.sub(xfB.p, xfD.p)));
coordinateB = Vec2.dot(pB, this.m_localAxisD) - Vec2.dot(pD, this.m_localAxisD);
}
@ -371,8 +368,8 @@ export default class GearJoint extends Joint {
const rC = Rot.mulSub(qC, this.m_localAnchorC, this.m_lcC); // Vec2
const rA = Rot.mulSub(qA, this.m_localAnchorA, this.m_lcA); // Vec2
this.m_JvAC = u;
this.m_JwC = Vec2.cross(rC, u);
this.m_JwA = Vec2.cross(rA, u);
this.m_JwC = Vec2.crossVec2Vec2(rC, u);
this.m_JwA = Vec2.crossVec2Vec2(rA, u);
this.m_mass += this.m_mC + this.m_mA + this.m_iC * this.m_JwC * this.m_JwC + this.m_iA * this.m_JwA * this.m_JwA;
}
@ -386,8 +383,8 @@ export default class GearJoint extends Joint {
const rD = Rot.mulSub(qD, this.m_localAnchorD, this.m_lcD); // Vec2
const rB = Rot.mulSub(qB, this.m_localAnchorB, this.m_lcB); // Vec2
this.m_JvBD = Vec2.mul(this.m_ratio, u);
this.m_JwD = this.m_ratio * Vec2.cross(rD, u);
this.m_JwB = this.m_ratio * Vec2.cross(rB, u);
this.m_JwD = this.m_ratio * Vec2.crossVec2Vec2(rD, u);
this.m_JwB = this.m_ratio * Vec2.crossVec2Vec2(rB, u);
this.m_mass += this.m_ratio * this.m_ratio * (this.m_mD + this.m_mB) + this.m_iD * this.m_JwD * this.m_JwD + this.m_iB * this.m_JwB * this.m_JwB;
}
@ -411,13 +408,13 @@ export default class GearJoint extends Joint {
this.m_impulse = 0.0;
}
this.m_bodyA.c_velocity.v.set(vA);
this.m_bodyA.c_velocity.v.setVec2(vA);
this.m_bodyA.c_velocity.w = wA;
this.m_bodyB.c_velocity.v.set(vB);
this.m_bodyB.c_velocity.v.setVec2(vB);
this.m_bodyB.c_velocity.w = wB;
this.m_bodyC.c_velocity.v.set(vC);
this.m_bodyC.c_velocity.v.setVec2(vC);
this.m_bodyC.c_velocity.w = wC;
this.m_bodyD.c_velocity.v.set(vD);
this.m_bodyD.c_velocity.v.setVec2(vD);
this.m_bodyD.c_velocity.w = wD;
}
@ -448,13 +445,13 @@ export default class GearJoint extends Joint {
vD.subMul(this.m_mD * impulse, this.m_JvBD);
wD -= this.m_iD * impulse * this.m_JwD;
this.m_bodyA.c_velocity.v.set(vA);
this.m_bodyA.c_velocity.v.setVec2(vA);
this.m_bodyA.c_velocity.w = wA;
this.m_bodyB.c_velocity.v.set(vB);
this.m_bodyB.c_velocity.v.setVec2(vB);
this.m_bodyB.c_velocity.w = wB;
this.m_bodyC.c_velocity.v.set(vC);
this.m_bodyC.c_velocity.v.setVec2(vC);
this.m_bodyC.c_velocity.w = wC;
this.m_bodyD.c_velocity.v.set(vD);
this.m_bodyD.c_velocity.v.setVec2(vD);
this.m_bodyD.c_velocity.w = wD;
}
@ -501,8 +498,8 @@ export default class GearJoint extends Joint {
const rC = Rot.mulSub(qC, this.m_localAnchorC, this.m_lcC); // Vec2
const rA = Rot.mulSub(qA, this.m_localAnchorA, this.m_lcA); // Vec2
JvAC = u;
JwC = Vec2.cross(rC, u);
JwA = Vec2.cross(rA, u);
JwC = Vec2.crossVec2Vec2(rC, u);
JwA = Vec2.crossVec2Vec2(rA, u);
mass += this.m_mC + this.m_mA + this.m_iC * JwC * JwC + this.m_iA * JwA * JwA;
const pC = Vec2.sub(this.m_localAnchorC, this.m_lcC); // Vec2
@ -522,8 +519,8 @@ export default class GearJoint extends Joint {
const rD = Rot.mulSub(qD, this.m_localAnchorD, this.m_lcD);
const rB = Rot.mulSub(qB, this.m_localAnchorB, this.m_lcB);
JvBD = Vec2.mul(this.m_ratio, u);
JwD = this.m_ratio * Vec2.cross(rD, u);
JwB = this.m_ratio * Vec2.cross(rB, u);
JwD = this.m_ratio * Vec2.crossVec2Vec2(rD, u);
JwB = this.m_ratio * Vec2.crossVec2Vec2(rB, u);
mass += this.m_ratio * this.m_ratio * (this.m_mD + this.m_mB) + this.m_iD
* JwD * JwD + this.m_iB * JwB * JwB;
@ -549,13 +546,13 @@ export default class GearJoint extends Joint {
cD.subMul(this.m_mD * impulse, JvBD);
aD -= this.m_iD * impulse * JwD;
this.m_bodyA.c_position.c.set(cA);
this.m_bodyA.c_position.c.setVec2(cA);
this.m_bodyA.c_position.a = aA;
this.m_bodyB.c_position.c.set(cB);
this.m_bodyB.c_position.c.setVec2(cB);
this.m_bodyB.c_position.a = aB;
this.m_bodyC.c_position.c.set(cC);
this.m_bodyC.c_position.c.setVec2(cC);
this.m_bodyC.c_position.a = aC;
this.m_bodyD.c_position.c.set(cD);
this.m_bodyD.c_position.c.setVec2(cD);
this.m_bodyD.c_position.a = aD;
// TODO_ERIN not implemented

View File

@ -31,6 +31,7 @@ import Rot from '../../common/Rot';
import Joint, { JointOpt, JointDef } from '../Joint';
import Body from '../Body';
import { TimeStep } from "../Solver";
import factoryConstructor from '../../decorators/factoryConstructor';
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
@ -78,6 +79,7 @@ const DEFAULTS = {
* typical usage is to control the movement of a dynamic body with respect to
* the ground.
*/
@factoryConstructor
export default class MotorJoint extends Joint {
static TYPE = 'motor-joint' as const;
@ -107,11 +109,6 @@ export default class MotorJoint extends Joint {
constructor(def: MotorJointDef);
constructor(def: MotorJointOpt, bodyA: Body, bodyB: Body);
constructor(def: MotorJointDef | MotorJointOpt, bodyA?: Body, bodyB?: Body) {
// @ts-ignore
if (!(this instanceof MotorJoint)) {
return new MotorJoint(def, bodyA, bodyB);
}
def = options(def, DEFAULTS);
super(def, bodyA, bodyB);
bodyA = this.m_bodyA;
@ -343,10 +340,10 @@ export default class MotorJoint extends Joint {
const P = Vec2.neo(this.m_linearImpulse.x, this.m_linearImpulse.y);
vA.subMul(mA, P);
wA -= iA * (Vec2.cross(this.m_rA, P) + this.m_angularImpulse);
wA -= iA * (Vec2.crossVec2Vec2(this.m_rA, P) + this.m_angularImpulse);
vB.addMul(mB, P);
wB += iB * (Vec2.cross(this.m_rB, P) + this.m_angularImpulse);
wB += iB * (Vec2.crossVec2Vec2(this.m_rB, P) + this.m_angularImpulse);
} else {
this.m_linearImpulse.setZero();
@ -391,8 +388,8 @@ export default class MotorJoint extends Joint {
// Solve linear friction
{
const Cdot = Vec2.zero();
Cdot.addCombine(1, vB, 1, Vec2.cross(wB, this.m_rB));
Cdot.subCombine(1, vA, 1, Vec2.cross(wA, this.m_rA));
Cdot.addCombine(1, vB, 1, Vec2.crossNumberVec2(wB, this.m_rB));
Cdot.subCombine(1, vA, 1, Vec2.crossNumberVec2(wA, this.m_rA));
Cdot.addMul(inv_h * this.m_correctionFactor, this.m_linearError);
let impulse = Vec2.neg(Mat22.mulVec2(this.m_linearMass, Cdot));
@ -406,10 +403,10 @@ export default class MotorJoint extends Joint {
impulse = Vec2.sub(this.m_linearImpulse, oldImpulse);
vA.subMul(mA, impulse);
wA -= iA * Vec2.cross(this.m_rA, impulse);
wA -= iA * Vec2.crossVec2Vec2(this.m_rA, impulse);
vB.addMul(mB, impulse);
wB += iB * Vec2.cross(this.m_rB, impulse);
wB += iB * Vec2.crossVec2Vec2(this.m_rB, impulse);
}
this.m_bodyA.c_velocity.v = vA;

View File

@ -32,6 +32,7 @@ import Transform from '../../common/Transform';
import Joint, { JointOpt, JointDef } from '../Joint';
import Body from '../Body';
import { TimeStep } from "../Solver";
import factoryConstructor from '../../decorators/factoryConstructor';
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
@ -85,6 +86,7 @@ const DEFAULTS = {
* be used in the testbed. If you want to learn how to use the mouse joint, look
* at the testbed.
*/
@factoryConstructor
export default class MouseJoint extends Joint {
static TYPE = 'mouse-joint' as const;
@ -108,11 +110,6 @@ export default class MouseJoint extends Joint {
constructor(def: MouseJointDef);
constructor(def: MouseJointOpt, bodyA: Body, bodyB: Body, target: Vec2);
constructor(def: MouseJointDef, bodyA?: Body, bodyB?: Body, target?: Vec2) {
// @ts-ignore
if (!(this instanceof MouseJoint)) {
return new MouseJoint(def, bodyA, bodyB, target);
}
def = options(def, DEFAULTS);
super(def, bodyA, bodyB);
bodyA = this.m_bodyA;
@ -175,7 +172,7 @@ export default class MouseJoint extends Joint {
data = {...data};
data.bodyA = restore(Body, data.bodyA, world);
data.bodyB = restore(Body, data.bodyB, world);
data.target = new Vec2(data.target);
data.target = Vec2.clone(data.target);
const joint = new MouseJoint(data);
if (data._localAnchorB) {
joint.m_localAnchorB = data._localAnchorB;
@ -329,7 +326,7 @@ export default class MouseJoint extends Joint {
this.m_mass = K.getInverse();
this.m_C.set(cB);
this.m_C.setVec2(cB);
this.m_C.addCombine(1, this.m_rB, -1, this.m_targetA);
this.m_C.mul(this.m_beta);
@ -339,13 +336,13 @@ export default class MouseJoint extends Joint {
if (step.warmStarting) {
this.m_impulse.mul(step.dtRatio);
vB.addMul(this.m_invMassB, this.m_impulse);
wB += this.m_invIB * Vec2.cross(this.m_rB, this.m_impulse);
wB += this.m_invIB * Vec2.crossVec2Vec2(this.m_rB, this.m_impulse);
} else {
this.m_impulse.setZero();
}
velocity.v.set(vB);
velocity.v.setVec2(vB);
velocity.w = wB;
}
@ -356,7 +353,7 @@ export default class MouseJoint extends Joint {
// Cdot = v + cross(w, r)
const Cdot = Vec2.cross(wB, this.m_rB);
const Cdot = Vec2.crossNumberVec2(wB, this.m_rB);
Cdot.add(vB);
Cdot.addCombine(1, this.m_C, this.m_gamma, this.m_impulse);
@ -371,9 +368,9 @@ export default class MouseJoint extends Joint {
impulse = Vec2.sub(this.m_impulse, oldImpulse);
vB.addMul(this.m_invMassB, impulse);
wB += this.m_invIB * Vec2.cross(this.m_rB, impulse);
wB += this.m_invIB * Vec2.crossVec2Vec2(this.m_rB, impulse);
velocity.v.set(vB);
velocity.v.setVec2(vB);
velocity.w = wB;
}

View File

@ -34,6 +34,7 @@ import Rot from '../../common/Rot';
import Joint, { JointOpt, JointDef } from '../Joint';
import Body from '../Body';
import { TimeStep } from "../Solver";
import factoryConstructor from '../../decorators/factoryConstructor';
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
@ -121,6 +122,7 @@ const DEFAULTS = {
* joint limit to restrict the range of motion and a joint motor to drive the
* motion or to model joint friction.
*/
@factoryConstructor
export default class PrismaticJoint extends Joint {
static TYPE = 'prismatic-joint' as const;
@ -158,11 +160,6 @@ export default class PrismaticJoint extends Joint {
constructor(def: PrismaticJointDef);
constructor(def: PrismaticJointOpt, bodyA: Body, bodyB: Body, anchor: Vec2, axis: Vec2);
constructor(def: PrismaticJointDef, bodyA?: Body, bodyB?: Body, anchor?: Vec2, axis?: Vec2) {
// @ts-ignore
if (!(this instanceof PrismaticJoint)) {
return new PrismaticJoint(def, bodyA, bodyB, anchor, axis);
}
def = options(def, DEFAULTS);
super(def, bodyA, bodyB);
bodyA = this.m_bodyA;
@ -174,7 +171,7 @@ export default class PrismaticJoint extends Joint {
this.m_localAnchorB = Vec2.clone(anchor ? bodyB.getLocalPoint(anchor) : def.localAnchorB || Vec2.zero());
this.m_localXAxisA = Vec2.clone(axis ? bodyA.getLocalVector(axis) : def.localAxisA || Vec2.neo(1.0, 0.0));
this.m_localXAxisA.normalize();
this.m_localYAxisA = Vec2.cross(1.0, this.m_localXAxisA);
this.m_localYAxisA = Vec2.crossNumberVec2(1.0, this.m_localXAxisA);
this.m_referenceAngle = Math.isFinite(def.referenceAngle) ? def.referenceAngle : bodyB.getAngle() - bodyA.getAngle();
this.m_impulse = new Vec3();
@ -293,7 +290,7 @@ export default class PrismaticJoint extends Joint {
data = {...data};
data.bodyA = restore(Body, data.bodyA, world);
data.bodyB = restore(Body, data.bodyB, world);
data.localAxisA = new Vec2(data.localAxisA);
data.localAxisA = Vec2.clone(data.localAxisA);
const joint = new PrismaticJoint(data);
return joint;
}
@ -307,20 +304,20 @@ export default class PrismaticJoint extends Joint {
localAxisA?: Vec2,
}): void {
if (def.anchorA) {
this.m_localAnchorA.set(this.m_bodyA.getLocalPoint(def.anchorA));
this.m_localAnchorA.setVec2(this.m_bodyA.getLocalPoint(def.anchorA));
} else if (def.localAnchorA) {
this.m_localAnchorA.set(def.localAnchorA);
this.m_localAnchorA.setVec2(def.localAnchorA);
}
if (def.anchorB) {
this.m_localAnchorB.set(this.m_bodyB.getLocalPoint(def.anchorB));
this.m_localAnchorB.setVec2(this.m_bodyB.getLocalPoint(def.anchorB));
} else if (def.localAnchorB) {
this.m_localAnchorB.set(def.localAnchorB);
this.m_localAnchorB.setVec2(def.localAnchorB);
}
if (def.localAxisA) {
this.m_localXAxisA.set(def.localAxisA);
this.m_localYAxisA.set(Vec2.cross(1.0, def.localAxisA));
this.m_localXAxisA.setVec2(def.localAxisA);
this.m_localYAxisA.setVec2(Vec2.crossNumberVec2(1.0, def.localAxisA));
}
}
@ -384,7 +381,7 @@ export default class PrismaticJoint extends Joint {
const wA = bA.m_angularVelocity; // float
const wB = bB.m_angularVelocity; // float
const speed = Vec2.dot(d, Vec2.cross(wA, axis))
const speed = Vec2.dot(d, Vec2.crossNumberVec2(wA, axis))
+ Vec2.dot(axis, Vec2.sub(Vec2.addCross(vB, wB, rB), Vec2.addCross(vA, wA, rA))); // float
return speed;
}
@ -552,8 +549,8 @@ export default class PrismaticJoint extends Joint {
// Compute motor Jacobian and effective mass.
{
this.m_axis = Rot.mulVec2(qA, this.m_localXAxisA);
this.m_a1 = Vec2.cross(Vec2.add(d, rA), this.m_axis);
this.m_a2 = Vec2.cross(rB, this.m_axis);
this.m_a1 = Vec2.crossVec2Vec2(Vec2.add(d, rA), this.m_axis);
this.m_a2 = Vec2.crossVec2Vec2(rB, this.m_axis);
this.m_motorMass = mA + mB + iA * this.m_a1 * this.m_a1 + iB * this.m_a2
* this.m_a2;
@ -566,10 +563,10 @@ export default class PrismaticJoint extends Joint {
{
this.m_perp = Rot.mulVec2(qA, this.m_localYAxisA);
this.m_s1 = Vec2.cross(Vec2.add(d, rA), this.m_perp);
this.m_s2 = Vec2.cross(rB, this.m_perp);
this.m_s1 = Vec2.crossVec2Vec2(Vec2.add(d, rA), this.m_perp);
this.m_s2 = Vec2.crossVec2Vec2(rB, this.m_perp);
const s1test = Vec2.cross(rA, this.m_perp);
const s1test = Vec2.crossVec2Vec2(rA, this.m_perp);
const k11 = mA + mB + iA * this.m_s1 * this.m_s1 + iB * this.m_s2 * this.m_s2;
const k12 = iA * this.m_s1 + iB * this.m_s2;
@ -642,9 +639,9 @@ export default class PrismaticJoint extends Joint {
this.m_motorImpulse = 0.0;
}
this.m_bodyA.c_velocity.v.set(vA);
this.m_bodyA.c_velocity.v.setVec2(vA);
this.m_bodyA.c_velocity.w = wA;
this.m_bodyB.c_velocity.v.set(vB);
this.m_bodyB.c_velocity.v.setVec2(vB);
this.m_bodyB.c_velocity.w = wB;
}
@ -694,7 +691,7 @@ export default class PrismaticJoint extends Joint {
const Cdot = new Vec3(Cdot1.x, Cdot1.y, Cdot2);
const f1 = new Vec3(this.m_impulse);
const f1 = Vec3.clone(this.m_impulse);
let df = this.m_K.solve33(Vec3.neg(Cdot)); // Vec3
this.m_impulse.add(df);
@ -768,12 +765,12 @@ export default class PrismaticJoint extends Joint {
const d = Vec2.sub(Vec2.add(cB, rB), Vec2.add(cA, rA)); // Vec2
const axis = Rot.mulVec2(qA, this.m_localXAxisA); // Vec2
const a1 = Vec2.cross(Vec2.add(d, rA), axis); // float
const a2 = Vec2.cross(rB, axis); // float
const a1 = Vec2.crossVec2Vec2(Vec2.add(d, rA), axis); // float
const a2 = Vec2.crossVec2Vec2(rB, axis); // float
const perp = Rot.mulVec2(qA, this.m_localYAxisA); // Vec2
const s1 = Vec2.cross(Vec2.add(d, rA), perp); // float
const s2 = Vec2.cross(rB, perp); // float
const s1 = Vec2.crossVec2Vec2(Vec2.add(d, rA), perp); // float
const s2 = Vec2.crossVec2Vec2(rB, perp); // float
let impulse = new Vec3();
const C1 = Vec2.zero(); // Vec2

View File

@ -31,6 +31,7 @@ import Rot from '../../common/Rot';
import Joint, { JointOpt, JointDef } from '../Joint';
import Body from '../Body';
import { TimeStep } from "../Solver";
import factoryConstructor from '../../decorators/factoryConstructor';
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
@ -93,6 +94,7 @@ const DEFAULTS = {
* anchor points with static shapes to prevent one side from going to zero
* length.
*/
@factoryConstructor
export default class PulleyJoint extends Joint {
static TYPE = 'pulley-joint' as const;
// static MIN_PULLEY_LENGTH: number = 2.0; // TODO where this is used?
@ -124,11 +126,6 @@ export default class PulleyJoint extends Joint {
constructor(def: PulleyJointDef);
constructor(def: PulleyJointOpt, bodyA: Body, bodyB: Body, groundA: Vec2, groundB: Vec2, anchorA: Vec2, anchorB: Vec2, ratio: number);
constructor(def: PulleyJointDef, bodyA?: Body, bodyB?: Body, groundA?: Vec2, groundB?: Vec2, anchorA?: Vec2, anchorB?: Vec2, ratio?: number) {
// @ts-ignore
if (!(this instanceof PulleyJoint)) {
return new PulleyJoint(def, bodyA, bodyB, groundA, groundB, anchorA, anchorB, ratio);
}
def = options(def, DEFAULTS);
super(def, bodyA, bodyB);
bodyA = this.m_bodyA;
@ -324,8 +321,8 @@ export default class PulleyJoint extends Joint {
}
// Compute effective mass.
const ruA = Vec2.cross(this.m_rA, this.m_uA); // float
const ruB = Vec2.cross(this.m_rB, this.m_uB); // float
const ruA = Vec2.crossVec2Vec2(this.m_rA, this.m_uA); // float
const ruB = Vec2.crossVec2Vec2(this.m_rB, this.m_uB); // float
const mA = this.m_invMassA + this.m_invIA * ruA * ruA; // float
const mB = this.m_invMassB + this.m_invIB * ruB * ruB; // float
@ -345,10 +342,10 @@ export default class PulleyJoint extends Joint {
const PB = Vec2.mul(-this.m_ratio * this.m_impulse, this.m_uB);
vA.addMul(this.m_invMassA, PA);
wA += this.m_invIA * Vec2.cross(this.m_rA, PA);
wA += this.m_invIA * Vec2.crossVec2Vec2(this.m_rA, PA);
vB.addMul(this.m_invMassB, PB);
wB += this.m_invIB * Vec2.cross(this.m_rB, PB);
wB += this.m_invIB * Vec2.crossVec2Vec2(this.m_rB, PB);
} else {
this.m_impulse = 0.0;
@ -366,8 +363,8 @@ export default class PulleyJoint extends Joint {
const vB = this.m_bodyB.c_velocity.v;
let wB = this.m_bodyB.c_velocity.w;
const vpA = Vec2.add(vA, Vec2.cross(wA, this.m_rA));
const vpB = Vec2.add(vB, Vec2.cross(wB, this.m_rB));
const vpA = Vec2.add(vA, Vec2.crossNumberVec2(wA, this.m_rA));
const vpB = Vec2.add(vB, Vec2.crossNumberVec2(wB, this.m_rB));
const Cdot = -Vec2.dot(this.m_uA, vpA) - this.m_ratio
* Vec2.dot(this.m_uB, vpB); // float
@ -377,9 +374,9 @@ export default class PulleyJoint extends Joint {
const PA = Vec2.mul(-impulse, this.m_uA); // Vec2
const PB = Vec2.mul(-this.m_ratio * impulse, this.m_uB); // Vec2
vA.addMul(this.m_invMassA, PA);
wA += this.m_invIA * Vec2.cross(this.m_rA, PA);
wA += this.m_invIA * Vec2.crossVec2Vec2(this.m_rA, PA);
vB.addMul(this.m_invMassB, PB);
wB += this.m_invIB * Vec2.cross(this.m_rB, PB);
wB += this.m_invIB * Vec2.crossVec2Vec2(this.m_rB, PB);
this.m_bodyA.c_velocity.v = vA;
this.m_bodyA.c_velocity.w = wA;
@ -422,8 +419,8 @@ export default class PulleyJoint extends Joint {
}
// Compute effective mass.
const ruA = Vec2.cross(rA, uA);
const ruB = Vec2.cross(rB, uB);
const ruA = Vec2.crossVec2Vec2(rA, uA);
const ruB = Vec2.crossVec2Vec2(rB, uB);
const mA = this.m_invMassA + this.m_invIA * ruA * ruA; // float
const mB = this.m_invMassB + this.m_invIB * ruB * ruB; // float
@ -443,9 +440,9 @@ export default class PulleyJoint extends Joint {
const PB = Vec2.mul(-this.m_ratio * impulse, uB); // Vec2
cA.addMul(this.m_invMassA, PA);
aA += this.m_invIA * Vec2.cross(rA, PA);
aA += this.m_invIA * Vec2.crossVec2Vec2(rA, PA);
cB.addMul(this.m_invMassB, PB);
aB += this.m_invIB * Vec2.cross(rB, PB);
aB += this.m_invIB * Vec2.crossVec2Vec2(rB, PB);
this.m_bodyA.c_position.c = cA;
this.m_bodyA.c_position.a = aA;

View File

@ -34,6 +34,7 @@ import Rot from '../../common/Rot';
import Joint, { JointOpt, JointDef } from '../Joint';
import Body from '../Body';
import { TimeStep } from "../Solver";
import factoryConstructor from '../../decorators/factoryConstructor';
const _ASSERT = typeof ASSERT === 'undefined' ? false : ASSERT;
@ -127,6 +128,7 @@ const DEFAULTS = {
* relative rotation about the shared point. A maximum motor torque is provided
* so that infinite forces are not generated.
*/
@factoryConstructor
export default class RevoluteJoint extends Joint {
static TYPE = 'revolute-joint' as const;
@ -162,11 +164,6 @@ export default class RevoluteJoint extends Joint {
constructor(def: RevoluteJointOpt, bodyA: Body, bodyB: Body, anchor: Vec2);
// @ts-ignore
constructor(def: RevoluteJointDef, bodyA?: Body, bodyB?: Body, anchor?: Vec2) {
// @ts-ignore
if (!(this instanceof RevoluteJoint)) {
return new RevoluteJoint(def, bodyA, bodyB, anchor);
}
def = options(def, DEFAULTS);
super(def, bodyA, bodyB);
bodyA = this.m_bodyA;
@ -240,15 +237,15 @@ export default class RevoluteJoint extends Joint {
localAnchorB?: Vec2,
}): void {
if (def.anchorA) {
this.m_localAnchorA.set(this.m_bodyA.getLocalPoint(def.anchorA));
this.m_localAnchorA.setVec2(this.m_bodyA.getLocalPoint(def.anchorA));
} else if (def.localAnchorA) {
this.m_localAnchorA.set(def.localAnchorA);
this.m_localAnchorA.setVec2(def.localAnchorA);
}
if (def.anchorB) {
this.m_localAnchorB.set(this.m_bodyB.getLocalPoint(def.anchorB));
this.m_localAnchorB.setVec2(this.m_bodyB.getLocalPoint(def.anchorB));
} else if (def.localAnchorB) {
this.m_localAnchorB.set(def.localAnchorB);
this.m_localAnchorB.setVec2(def.localAnchorB);
}
}
@ -515,10 +512,10 @@ export default class RevoluteJoint extends Joint {
const P = Vec2.neo(this.m_impulse.x, this.m_impulse.y);
vA.subMul(mA, P);
wA -= iA * (Vec2.cross(this.m_rA, P) + this.m_motorImpulse + this.m_impulse.z);
wA -= iA * (Vec2.crossVec2Vec2(this.m_rA, P) + this.m_motorImpulse + this.m_impulse.z);
vB.addMul(mB, P);
wB += iB * (Vec2.cross(this.m_rB, P) + this.m_motorImpulse + this.m_impulse.z);
wB += iB * (Vec2.crossVec2Vec2(this.m_rB, P) + this.m_motorImpulse + this.m_impulse.z);
} else {
this.m_impulse.setZero();
@ -563,8 +560,8 @@ export default class RevoluteJoint extends Joint {
if (this.m_enableLimit && this.m_limitState != inactiveLimit
&& fixedRotation == false) {
const Cdot1 = Vec2.zero();
Cdot1.addCombine(1, vB, 1, Vec2.cross(wB, this.m_rB));
Cdot1.subCombine(1, vA, 1, Vec2.cross(wA, this.m_rA));
Cdot1.addCombine(1, vB, 1, Vec2.crossNumberVec2(wB, this.m_rB));
Cdot1.subCombine(1, vA, 1, Vec2.crossNumberVec2(wA, this.m_rA));
const Cdot2 = wB - wA; // float
const Cdot = new Vec3(Cdot1.x, Cdot1.y, Cdot2);
@ -611,26 +608,26 @@ export default class RevoluteJoint extends Joint {
const P = Vec2.neo(impulse.x, impulse.y);
vA.subMul(mA, P);
wA -= iA * (Vec2.cross(this.m_rA, P) + impulse.z);
wA -= iA * (Vec2.crossVec2Vec2(this.m_rA, P) + impulse.z);
vB.addMul(mB, P);
wB += iB * (Vec2.cross(this.m_rB, P) + impulse.z);
wB += iB * (Vec2.crossVec2Vec2(this.m_rB, P) + impulse.z);
} else {
// Solve point-to-point constraint
const Cdot = Vec2.zero();
Cdot.addCombine(1, vB, 1, Vec2.cross(wB, this.m_rB));
Cdot.subCombine(1, vA, 1, Vec2.cross(wA, this.m_rA));
Cdot.addCombine(1, vB, 1, Vec2.crossNumberVec2(wB, this.m_rB));
Cdot.subCombine(1, vA, 1, Vec2.crossNumberVec2(wA, this.m_rA));
const impulse = this.m_mass.solve22(Vec2.neg(Cdot)); // Vec2
this.m_impulse.x += impulse.x;
this.m_impulse.y += impulse.y;
vA.subMul(mA, impulse);
wA -= iA * Vec2.cross(this.m_rA, impulse);
wA -= iA * Vec2.crossVec2Vec2(this.m_rA, impulse);
vB.addMul(mB, impulse);
wB += iB * Vec2.cross(this.m_rB, impulse);
wB += iB * Vec2.crossVec2Vec2(this.m_rB, impulse);
}
this.m_bodyA.c_velocity.v = vA;
@ -694,8 +691,8 @@ export default class RevoluteJoint extends Joint {
// Solve point-to-point constraint.
{
qA.set(aA);
qB.set(aB);
qA.setAngle(aA);
qB.setAngle(aB);
const rA = Rot.mulVec2(qA, Vec2.sub(this.m_localAnchorA, this.m_localCenterA)); // Vec2
const rB = Rot.mulVec2(qB, Vec2.sub(this.m_localAnchorB, this.m_localCenterB)); // Vec2
@ -718,15 +715,15 @@ export default class RevoluteJoint extends Joint {
const impulse = Vec2.neg(K.solve(C)); // Vec2
cA.subMul(mA, impulse);
aA -= iA * Vec2.cross(rA, impulse);
aA -= iA * Vec2.crossVec2Vec2(rA, impulse);
cB.addMul(mB, impulse);
aB += iB * Vec2.cross(rB, impulse);
aB += iB * Vec2.crossVec2Vec2(rB, impulse);
}
this.m_bodyA.c_position.c.set(cA);
this.m_bodyA.c_position.c.setVec2(cA);
this.m_bodyA.c_position.a = aA;
this.m_bodyB.c_position.c.set(cB);
this.m_bodyB.c_position.c.setVec2(cB);
this.m_bodyB.c_position.a = aB;
return positionError <= Settings.linearSlop

View File

@ -30,6 +30,7 @@ import Rot from '../../common/Rot';
import Joint, { JointOpt, JointDef } from '../Joint';
import Body from '../Body';
import { TimeStep } from "../Solver";
import factoryConstructor from '../../decorators/factoryConstructor';
const inactiveLimit = 0;
const atLowerLimit = 1;
@ -79,6 +80,7 @@ const DEFAULTS = {
* sponginess, so I chose not to implement it that way. See {@link DistanceJoint} if you
* want to dynamically control length.
*/
@factoryConstructor
export default class RopeJoint extends Joint {
static TYPE = 'rope-joint' as const;
@ -107,11 +109,6 @@ export default class RopeJoint extends Joint {
constructor(def: RopeJointDef);
constructor(def: RopeJointOpt, bodyA: Body, bodyB: Body, anchor: Vec2);
constructor(def: RopeJointDef, bodyA?: Body, bodyB?: Body, anchor?: Vec2) {
// @ts-ignore
if (!(this instanceof RopeJoint)) {
return new RopeJoint(def, bodyA, bodyB, anchor);
}
def = options(def, DEFAULTS);
super(def, bodyA, bodyB);
bodyA = this.m_bodyA;
@ -267,8 +264,8 @@ export default class RopeJoint extends Joint {
}
// Compute effective mass.
const crA = Vec2.cross(this.m_rA, this.m_u); // float
const crB = Vec2.cross(this.m_rB, this.m_u); // float
const crA = Vec2.crossVec2Vec2(this.m_rA, this.m_u); // float
const crB = Vec2.crossVec2Vec2(this.m_rB, this.m_u); // float
const invMass = this.m_invMassA + this.m_invIA * crA * crA + this.m_invMassB
+ this.m_invIB * crB * crB; // float
@ -281,18 +278,18 @@ export default class RopeJoint extends Joint {
const P = Vec2.mul(this.m_impulse, this.m_u);
vA.subMul(this.m_invMassA, P);
wA -= this.m_invIA * Vec2.cross(this.m_rA, P);
wA -= this.m_invIA * Vec2.crossVec2Vec2(this.m_rA, P);
vB.addMul(this.m_invMassB, P);
wB += this.m_invIB * Vec2.cross(this.m_rB, P);
wB += this.m_invIB * Vec2.crossVec2Vec2(this.m_rB, P);
} else {
this.m_impulse = 0.0;
}
this.m_bodyA.c_velocity.v.set(vA);
this.m_bodyA.c_velocity.v.setVec2(vA);
this.m_bodyA.c_velocity.w = wA;
this.m_bodyB.c_velocity.v.set(vB);
this.m_bodyB.c_velocity.v.setVec2(vB);
this.m_bodyB.c_velocity.w = wB;
}
@ -320,9 +317,9 @@ export default class RopeJoint extends Joint {
const P = Vec2.mul(impulse, this.m_u); // Vec2
vA.subMul(this.m_invMassA, P);
wA -= this.m_invIA * Vec2.cross(this.m_rA, P);
wA -= this.m_invIA * Vec2.crossVec2Vec2(this.m_rA, P);
vB.addMul(this.m_invMassB, P);
wB += this.m_invIB * Vec2.cross(this.m_rB, P);
wB += this.m_invIB * Vec2.crossVec2Vec2(this.m_rB, P);
this.m_bodyA.c_velocity.v = vA;
this.m_bodyA.c_velocity.w = wA;
@ -357,13 +354,13 @@ export default class RopeJoint extends Joint {
const P = Vec2.mul(impulse, u); // Vec2
cA.subMul(this.m_invMassA, P);
aA -= this.m_invIA * Vec2.cross(rA, P);
aA -= this.m_invIA * Vec2.crossVec2Vec2(rA, P);
cB.addMul(this.m_invMassB, P);
aB += this.m_invIB * Vec2.cross(rB, P);
aB += this.m_invIB * Vec2.crossVec2Vec2(rB, P);
this.m_bodyA.c_position.c.set(cA);
this.m_bodyA.c_position.c.setVec2(cA);
this.m_bodyA.c_position.a = aA;
this.m_bodyB.c_position.c.set(cB);
this.m_bodyB.c_position.c.setVec2(cB);
this.m_bodyB.c_position.a = aB;
return length - this.m_maxLength < Settings.linearSlop;

View File

@ -32,6 +32,7 @@ import Rot from '../../common/Rot';
import Joint, { JointOpt, JointDef } from '../Joint';
import Body from '../Body';
import { TimeStep } from "../Solver";
import factoryConstructor from '../../decorators/factoryConstructor';
/**
@ -86,6 +87,7 @@ const DEFAULTS = {
* A weld joint essentially glues two bodies together. A weld joint may distort
* somewhat because the island constraint solver is approximate.
*/
@factoryConstructor
export default class WeldJoint extends Joint {
static TYPE = 'weld-joint' as const
@ -116,11 +118,6 @@ export default class WeldJoint extends Joint {
constructor(def: WeldJointDef);
constructor(def: WeldJointOpt, bodyA: Body, bodyB: Body, anchor: Vec2);
constructor(def: WeldJointDef, bodyA?: Body, bodyB?: Body, anchor?: Vec2) {
// @ts-ignore
if (!(this instanceof WeldJoint)) {
return new WeldJoint(def, bodyA, bodyB, anchor);
}
def = options(def, DEFAULTS);
super(def, bodyA, bodyB);
bodyA = this.m_bodyA;
@ -200,15 +197,15 @@ export default class WeldJoint extends Joint {
localAnchorB?: Vec2,
}): void {
if (def.anchorA) {
this.m_localAnchorA.set(this.m_bodyA.getLocalPoint(def.anchorA));
this.m_localAnchorA.setVec2(this.m_bodyA.getLocalPoint(def.anchorA));
} else if (def.localAnchorA) {
this.m_localAnchorA.set(def.localAnchorA);
this.m_localAnchorA.setVec2(def.localAnchorA);
}
if (def.anchorB) {
this.m_localAnchorB.set(this.m_bodyB.getLocalPoint(def.anchorB));
this.m_localAnchorB.setVec2(this.m_bodyB.getLocalPoint(def.anchorB));
} else if (def.localAnchorB) {
this.m_localAnchorB.set(def.localAnchorB);
this.m_localAnchorB.setVec2(def.localAnchorB);
}
}
@ -380,10 +377,10 @@ export default class WeldJoint extends Joint {
const P = Vec2.neo(this.m_impulse.x, this.m_impulse.y);
vA.subMul(mA, P);
wA -= iA * (Vec2.cross(this.m_rA, P) + this.m_impulse.z);
wA -= iA * (Vec2.crossVec2Vec2(this.m_rA, P) + this.m_impulse.z);
vB.addMul(mB, P);
wB += iB * (Vec2.cross(this.m_rB, P) + this.m_impulse.z);
wB += iB * (Vec2.crossVec2Vec2(this.m_rB, P) + this.m_impulse.z);
} else {
this.m_impulse.setZero();
@ -417,8 +414,8 @@ export default class WeldJoint extends Joint {
wB += iB * impulse2;
const Cdot1 = Vec2.zero();
Cdot1.addCombine(1, vB, 1, Vec2.cross(wB, this.m_rB));
Cdot1.subCombine(1, vA, 1, Vec2.cross(wA, this.m_rA)); // Vec2
Cdot1.addCombine(1, vB, 1, Vec2.crossNumberVec2(wB, this.m_rB));
Cdot1.subCombine(1, vA, 1, Vec2.crossNumberVec2(wA, this.m_rA)); // Vec2
const impulse1 = Vec2.neg(Mat33.mulVec2(this.m_mass, Cdot1)); // Vec2
this.m_impulse.x += impulse1.x;
@ -427,14 +424,14 @@ export default class WeldJoint extends Joint {
const P = Vec2.clone(impulse1); // Vec2
vA.subMul(mA, P);
wA -= iA * Vec2.cross(this.m_rA, P);
wA -= iA * Vec2.crossVec2Vec2(this.m_rA, P);
vB.addMul(mB, P);
wB += iB * Vec2.cross(this.m_rB, P);
wB += iB * Vec2.crossVec2Vec2(this.m_rB, P);
} else {
const Cdot1 = Vec2.zero();
Cdot1.addCombine(1, vB, 1, Vec2.cross(wB, this.m_rB));
Cdot1.subCombine(1, vA, 1, Vec2.cross(wA, this.m_rA)); // Vec2
Cdot1.addCombine(1, vB, 1, Vec2.crossNumberVec2(wB, this.m_rB));
Cdot1.subCombine(1, vA, 1, Vec2.crossNumberVec2(wA, this.m_rA)); // Vec2
const Cdot2 = wB - wA; // float
const Cdot = new Vec3(Cdot1.x, Cdot1.y, Cdot2); // Vec3
@ -444,10 +441,10 @@ export default class WeldJoint extends Joint {
const P = Vec2.neo(impulse.x, impulse.y);
vA.subMul(mA, P);
wA -= iA * (Vec2.cross(this.m_rA, P) + impulse.z);
wA -= iA * (Vec2.crossVec2Vec2(this.m_rA, P) + impulse.z);
vB.addMul(mB, P);
wB += iB * (Vec2.cross(this.m_rB, P) + impulse.z);
wB += iB * (Vec2.crossVec2Vec2(this.m_rB, P) + impulse.z);
}
this.m_bodyA.c_velocity.v = vA;
@ -501,10 +498,10 @@ export default class WeldJoint extends Joint {
const P = Vec2.neg(K.solve22(C1)); // Vec2
cA.subMul(mA, P);
aA -= iA * Vec2.cross(rA, P);
aA -= iA * Vec2.crossVec2Vec2(rA, P);
cB.addMul(mB, P);
aB += iB * Vec2.cross(rB, P);
aB += iB * Vec2.crossVec2Vec2(rB, P);
} else {
const C1 = Vec2.zero();
C1.addCombine(1, cB, 1, rB);
@ -528,10 +525,10 @@ export default class WeldJoint extends Joint {
const P = Vec2.neo(impulse.x, impulse.y);
cA.subMul(mA, P);
aA -= iA * (Vec2.cross(rA, P) + impulse.z);
aA -= iA * (Vec2.crossVec2Vec2(rA, P) + impulse.z);
cB.addMul(mB, P);
aB += iB * (Vec2.cross(rB, P) + impulse.z);
aB += iB * (Vec2.crossVec2Vec2(rB, P) + impulse.z);
}
this.m_bodyA.c_position.c = cA;

View File

@ -30,6 +30,7 @@ import Rot from '../../common/Rot';
import Joint, { JointOpt, JointDef } from '../Joint';
import Body from '../Body';
import { TimeStep } from "../Solver";
import factoryConstructor from '../../decorators/factoryConstructor';
/**
@ -99,6 +100,7 @@ const DEFAULTS = {
* point to line constraint with a rotational motor and a linear spring/damper.
* This joint is designed for vehicle suspensions.
*/
@factoryConstructor
export default class WheelJoint extends Joint {
static TYPE = 'wheel-joint' as const;
@ -144,11 +146,6 @@ export default class WheelJoint extends Joint {
constructor(def: WheelJointOpt, bodyA: Body, bodyB: Body, anchor: Vec2, axis: Vec2);
// @ts-ignore
constructor(def: WheelJointDef, bodyA?: Body, bodyB?: Body, anchor?: Vec2, axis?: Vec2) {
// @ts-ignore
if (!(this instanceof WheelJoint)) {
return new WheelJoint(def, bodyA, bodyB, anchor, axis);
}
def = options(def, DEFAULTS);
super(def, bodyA, bodyB);
bodyA = this.m_bodyA;
@ -160,7 +157,7 @@ export default class WheelJoint extends Joint {
this.m_localAnchorB = Vec2.clone(anchor ? bodyB.getLocalPoint(anchor) : def.localAnchorB || Vec2.zero());
// @ts-ignore localAxis
this.m_localXAxisA = Vec2.clone(axis ? bodyA.getLocalVector(axis) : def.localAxisA || def.localAxis || Vec2.neo(1.0, 0.0));
this.m_localYAxisA = Vec2.cross(1.0, this.m_localXAxisA);
this.m_localYAxisA = Vec2.crossNumberVec2(1.0, this.m_localXAxisA);
this.m_mass = 0.0;
this.m_impulse = 0.0;
@ -237,20 +234,20 @@ export default class WheelJoint extends Joint {
localAxisA?: Vec2,
}): void {
if (def.anchorA) {
this.m_localAnchorA.set(this.m_bodyA.getLocalPoint(def.anchorA));
this.m_localAnchorA.setVec2(this.m_bodyA.getLocalPoint(def.anchorA));
} else if (def.localAnchorA) {
this.m_localAnchorA.set(def.localAnchorA);
this.m_localAnchorA.setVec2(def.localAnchorA);
}
if (def.anchorB) {
this.m_localAnchorB.set(this.m_bodyB.getLocalPoint(def.anchorB));
this.m_localAnchorB.setVec2(this.m_bodyB.getLocalPoint(def.anchorB));
} else if (def.localAnchorB) {
this.m_localAnchorB.set(def.localAnchorB);
this.m_localAnchorB.setVec2(def.localAnchorB);
}
if (def.localAxisA) {
this.m_localXAxisA.set(def.localAxisA);
this.m_localYAxisA.set(Vec2.cross(1.0, def.localAxisA));
this.m_localXAxisA.setVec2(def.localAxisA);
this.m_localYAxisA.setVec2(Vec2.crossNumberVec2(1.0, def.localAxisA));
}
}
@ -439,8 +436,8 @@ export default class WheelJoint extends Joint {
// Point to line constraint
{
this.m_ay = Rot.mulVec2(qA, this.m_localYAxisA);
this.m_sAy = Vec2.cross(Vec2.add(d, rA), this.m_ay);
this.m_sBy = Vec2.cross(rB, this.m_ay);
this.m_sAy = Vec2.crossVec2Vec2(Vec2.add(d, rA), this.m_ay);
this.m_sBy = Vec2.crossVec2Vec2(rB, this.m_ay);
this.m_mass = mA + mB + iA * this.m_sAy * this.m_sAy + iB * this.m_sBy
* this.m_sBy;
@ -456,8 +453,8 @@ export default class WheelJoint extends Joint {
this.m_gamma = 0.0;
if (this.m_frequencyHz > 0.0) {
this.m_ax = Rot.mulVec2(qA, this.m_localXAxisA);
this.m_sAx = Vec2.cross(Vec2.add(d, rA), this.m_ax);
this.m_sBx = Vec2.cross(rB, this.m_ax);
this.m_sAx = Vec2.crossVec2Vec2(Vec2.add(d, rA), this.m_ax);
this.m_sBx = Vec2.crossVec2Vec2(rB, this.m_ax);
const invMass = mA + mB + iA * this.m_sAx * this.m_sAx + iB * this.m_sBx
* this.m_sBx; // float
@ -527,9 +524,9 @@ export default class WheelJoint extends Joint {
this.m_motorImpulse = 0.0;
}
this.m_bodyA.c_velocity.v.set(vA);
this.m_bodyA.c_velocity.v.setVec2(vA);
this.m_bodyA.c_velocity.w = wA;
this.m_bodyB.c_velocity.v.set(vB);
this.m_bodyB.c_velocity.v.setVec2(vB);
this.m_bodyB.c_velocity.w = wB;
}
@ -596,9 +593,9 @@ export default class WheelJoint extends Joint {
wB += iB * LB;
}
this.m_bodyA.c_velocity.v.set(vA);
this.m_bodyA.c_velocity.v.setVec2(vA);
this.m_bodyA.c_velocity.w = wA;
this.m_bodyB.c_velocity.v.set(vB);
this.m_bodyB.c_velocity.v.setVec2(vB);
this.m_bodyB.c_velocity.w = wB;
}
@ -622,8 +619,8 @@ export default class WheelJoint extends Joint {
const ay = Rot.mulVec2(qA, this.m_localYAxisA);
const sAy = Vec2.cross(Vec2.add(d, rA), ay); // float
const sBy = Vec2.cross(rB, ay); // float
const sAy = Vec2.crossVec2Vec2(Vec2.add(d, rA), ay); // float
const sBy = Vec2.crossVec2Vec2(rB, ay); // float
const C = Vec2.dot(d, ay); // float
@ -646,9 +643,9 @@ export default class WheelJoint extends Joint {
cB.addMul(this.m_invMassB, P);
aB += this.m_invIB * LB;
this.m_bodyA.c_position.c.set(cA);
this.m_bodyA.c_position.c.setVec2(cA);
this.m_bodyA.c_position.a = aA;
this.m_bodyB.c_position.c.set(cB);
this.m_bodyB.c_position.c.setVec2(cB);
this.m_bodyB.c_position.a = aB;
return Math.abs(C) <= Settings.linearSlop;

View File

@ -1,12 +1,104 @@
import ts from 'typescript';
export default options => context => node => {
return ts.visitEachChild(node, (node) => visitor(context.factory, node, options), context);
export default ({ enabled }) => shared => {
shared.classes = new Set();
return {
before: context => node => {
node = ts.visitEachChild(node, (node) => removeFactoryConstructorDecoratorVisitor(context.factory, node, shared), context);
if (!enabled) {
return node;
}
return ts.visitEachChild(node, (node) => applyFactoryConstructorDecoratorVisitor(context.factory, node, shared), context);
},
afterDeclarations: context => node => {
if (!enabled) {
return node;
}
return ts.visitEachChild(node, (node) => visitor(context.factory, node, shared), context);
}
}
}
function visitor(factory, node, options) {
if (!ts.isClassDeclaration(node) ||
!isClassIncluded(node.name.escapedText, options)) {
function removeFactoryConstructorDecoratorVisitor(factory, node, shared) {
if (ts.isClassDeclaration(node) && hasFactoryConstructorDecorator(node.decorators)) {
shared.classes.add(node.name.escapedText);
return factory.updateClassDeclaration(
node,
removeFactoryConstructorDecorator(node.decorators),
node.modifiers,
node.name,
node.typeParameters,
node.heritageClauses,
node.members
);
}
return node;
}
function applyFactoryConstructorDecoratorVisitor(factory, node, shared) {
if (isRelevantClassDeclaration(node, shared)) {
const members = node.members.map(member => {
if (ts.isConstructorDeclaration(member) && member.body) {
const block = factory.updateBlock(member.body, [
createRecallWithNewKeyword(factory, node.name, member.parameters),
...member.body.statements
]);
return factory.updateConstructorDeclaration(member, member.decorators, member.modifiers, member.parameters, block);
}
return member;
});
return factory.updateClassDeclaration(
node,
removeFactoryConstructorDecorator(node.decorators),
node.modifiers,
node.name,
node.typeParameters,
node.heritageClauses,
members
);
}
return node;
}
function hasFactoryConstructorDecorator(decorators) {
return decorators?.some(isFactoryConstructorDecorator);
}
function removeFactoryConstructorDecorator(decorators) {
const filtered = decorators?.filter(decorator => !isFactoryConstructorDecorator(decorator));
if (filtered?.length > 0) {
return filtered;
}
}
function isFactoryConstructorDecorator(decorator) {
return ts.isIdentifier(decorator.expression) && decorator.expression.escapedText === 'factoryConstructor';
}
function createRecallWithNewKeyword(factory, className, parameters) {
return factory.createIfStatement(
factory.createPrefixUnaryExpression(
ts.SyntaxKind.ExclamationToken,
factory.createParenthesizedExpression(factory.createBinaryExpression(
factory.createThis(),
factory.createToken(ts.SyntaxKind.InstanceOfKeyword),
className
))
),
factory.createBlock(
[factory.createReturnStatement(factory.createNewExpression(
className,
undefined,
parameters.map(parameter => parameter.name)
))],
true
),
undefined
)
}
function visitor(factory, node, shared) {
if (!isRelevantClassDeclaration(node, shared)) {
return node;
}
if (isClassDerived(node)) {
@ -16,8 +108,8 @@ function visitor(factory, node, options) {
return createNodeWithFactories(factory, node);
}
function isClassIncluded(className, options) {
return options.classes.includes(className);
function isRelevantClassDeclaration(node, shared) {
return ts.isClassDeclaration(node) && shared.classes.has(node.name.escapedText);
}
function isClassDerived(node) {

View File

@ -10,6 +10,7 @@
// "noImplicitAny": true,
"lib": ["ES2015", "DOM"],
"stripInternal": true,
"target": "ES5"
"target": "ESNEXT",
"experimentalDecorators": true
}
}