Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

performance and other stuff from https://github.com/schteppe/p2.js/pull/366 recreated #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion packages/p2-es-sandbox/src/pixi/draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ export const drawConvex = ({
if (!debugPolygons) {
graphics.lineStyle(lineWidth, lineColor, 1)
graphics.beginFill(fillColor, isSleeping ? sleepOpacity : 1.0)
for (let i = 0; i !== verts.length; i++) {
const l = verts.length
for (let i = 0; i !== l; i++) {
const v = verts[i]
const x = v[0]
const y = v[1]
Expand Down
4 changes: 2 additions & 2 deletions packages/p2-es/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "p2-es",
"version": "1.2.2",
"version": "1.3.0",
"license": "MIT",
"description": "A JavaScript 2D physics engine.",
"homepage": "https://github.com/pmndrs/p2-es",
Expand Down Expand Up @@ -77,4 +77,4 @@
"lint-staged": {
"*.{js,json,jsx,ts,tsx,md,yaml,yml}": "prettier --write"
}
}
}
8 changes: 5 additions & 3 deletions packages/p2-es/src/collision/AABB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ export class AABB {
vec2.copy(u, l)

// Compute cosines and sines just once
const cosAngle = Math.cos(angle)
const sinAngle = Math.sin(angle)
for (let i = 1; i < points.length; i++) {
const cosAngle = Math.cos(angle);
const sinAngle = Math.sin(angle);
const len = points.length;

for (let i = 1; i < len; i++) {
let p = points[i]

if (angle !== 0) {
Expand Down
7 changes: 5 additions & 2 deletions packages/p2-es/src/collision/NaiveBroadphase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ export class NaiveBroadphase extends Broadphase {
* @param result An array to store resulting bodies in.
*/
aabbQuery(world: World, aabb: AABB, result: Body[] = []) {
const bodies = world.bodies
for (let i = 0; i < bodies.length; i++) {
const bodies = world.bodies;

const l = bodies.length;

for (let i = 0; i < l; i++) {
const b = bodies[i]

if (b.aabbNeedsUpdate) {
Expand Down
46 changes: 26 additions & 20 deletions packages/p2-es/src/collision/Narrowphase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,14 @@ export class Narrowphase {
this.collidingBodiesLastStep.set(id1, id2, true)
}

const ce = this.contactEquations
const fe = this.frictionEquations
for (let i = 0; i < ce.length; i++) {
const ce = this.contactEquations;
const fe = this.frictionEquations;
const cel = ce.length;
const fel = fe.length;
for (let i = 0; i < cel; i++) {
this.contactEquationPool.release(ce[i])
}
for (let i = 0; i < fe.length; i++) {
for (let i = 0; i < fel; i++) {
this.frictionEquationPool.release(fe[i])
}

Expand Down Expand Up @@ -666,9 +668,11 @@ export class Narrowphase {
vec2.rotate(worldNormal, yAxis, planeAngle)

// Check line ends
verts[0] = worldVertex0
verts[1] = worldVertex1
for (let i = 0; i < verts.length; i++) {
verts[0] = worldVertex0;
verts[1] = worldVertex1;

const l = verts.length;
for (let i = 0; i < l; i++) {
const v = verts[i]

vec2.subtract(dist, v, planeOffset)
Expand Down Expand Up @@ -880,10 +884,12 @@ export class Narrowphase {
}

// Add corner
verts[0] = worldVertex0
verts[1] = worldVertex1
verts[0] = worldVertex0;
verts[1] = worldVertex1;

const l = verts.length;

for (let i = 0; i < verts.length; i++) {
for (let i = 0; i < l; i++) {
const v = verts[i]

vec2.subtract(dist, v, circleOffset)
Expand Down Expand Up @@ -1555,16 +1561,16 @@ export class Narrowphase {

// Do Narrowphase as two circles
const numContacts1 = this.circlePlane(
capsuleBody,
circle,
end1,
0,
planeBody,
planeShape,
planeOffset,
planeAngle,
justTest
),
capsuleBody,
circle,
end1,
0,
planeBody,
planeShape,
planeOffset,
planeAngle,
justTest
),
numContacts2 = this.circlePlane(
capsuleBody,
circle,
Expand Down
15 changes: 10 additions & 5 deletions packages/p2-es/src/collision/SAPBroadphase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ export class SAPBroadphase extends Broadphase {

this.removeBodyHandler = (e) => {
// Remove from list
const idx = this.axisList.indexOf(e.body)
if (idx !== -1) {
this.axisList.splice(idx, 1)
const l = this.axisList.length;
const list = this.axisList;
for (let i = 0; i < l; i++) {
if (list[i] === e.body) {
list.splice(i, 1);
break;
}
}
}
}
Expand Down Expand Up @@ -118,8 +122,9 @@ export class SAPBroadphase extends Broadphase {
aabbQuery(world: World, aabb: AABB, result: Body[] = []): Body[] {
this.sortList()

const axisList = this.axisList
for (let i = 0; i < axisList.length; i++) {
const axisList = this.axisList;
const l = axisList.length;
for (let i = 0; i < l; i++) {
const b = axisList[i]

if (b.aabbNeedsUpdate) {
Expand Down
15 changes: 9 additions & 6 deletions packages/p2-es/src/constraints/Constraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ export class Constraint {
* @param stiffness
*/
setStiffness(stiffness: number): void {
const eqs = this.equations
for (let i = 0; i !== eqs.length; i++) {
const eqs = this.equations;
const l = eqs.length;
for (let i = 0; i !== l; i++) {
const eq = eqs[i]
eq.stiffness = stiffness
eq.needsUpdate = true
Expand All @@ -110,8 +111,9 @@ export class Constraint {
* @param relaxation
*/
setRelaxation(relaxation: number): void {
const eqs = this.equations
for (let i = 0; i !== eqs.length; i++) {
const eqs = this.equations;
const l = eqs.length;
for (let i = 0; i !== l; i++) {
const eq = eqs[i]
eq.relaxation = relaxation
eq.needsUpdate = true
Expand All @@ -123,8 +125,9 @@ export class Constraint {
* @param maxBias
*/
setMaxBias(maxBias: number): void {
const eqs = this.equations
for (let i = 0; i !== eqs.length; i++) {
const eqs = this.equations;
const l = eqs.length;
for (let i = 0; i !== l; i++) {
const eq = eqs[i]
eq.maxBias = maxBias
}
Expand Down
5 changes: 3 additions & 2 deletions packages/p2-es/src/constraints/LockConstraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ export class LockConstraint extends Constraint {
* @param force
*/
setMaxForce(force: number): void {
const eqs = this.equations
for (let i = 0; i < this.equations.length; i++) {
const eqs = this.equations;
const l = eqs.length;
for (let i = 0; i < l; i++) {
eqs[i].maxForce = force
eqs[i].minForce = -force
}
Expand Down
29 changes: 20 additions & 9 deletions packages/p2-es/src/constraints/PrismaticConstraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,14 @@ export class PrismaticConstraint extends Constraint {
if (!this.motorEnabled) {
return
}
const i = this.equations.indexOf(this.motorEquation)
this.equations.splice(i, 1)
this.motorEnabled = false
const l = this.equations.length;
const eqs = this.equations;
for (let i = 0; i < l; i++) {
if (eqs[i] === this.motorEquation) {
eqs.splice(i, 1);
break;
}
}
}

/**
Expand Down Expand Up @@ -348,9 +353,12 @@ export class PrismaticConstraint extends Constraint {
eqs.push(upperLimitEquation)
}
} else {
const idx = eqs.indexOf(upperLimitEquation)
if (idx !== -1) {
eqs.splice(idx, 1)
const l = eqs.length;
for (let i = 0; i < l; i++) {
if (eqs[i] === upperLimitEquation) {
eqs.splice(i, 1);
break;
}
}
}

Expand All @@ -365,9 +373,12 @@ export class PrismaticConstraint extends Constraint {
eqs.push(lowerLimitEquation)
}
} else {
const idx = eqs.indexOf(lowerLimitEquation)
if (idx !== -1) {
eqs.splice(idx, 1)
const l = eqs.length;
for (let i = 0; i < l; i++) {
if (eqs[i] === lowerLimitEquation) {
eqs.splice(i, 1);
break;
}
}
}
}
Expand Down
42 changes: 24 additions & 18 deletions packages/p2-es/src/objects/Body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -748,16 +748,17 @@ export class Body extends EventEmitter<BodyEventMap> {
throw new Error('A shape cannot be removed during step.')
}

const idx = this.shapes.indexOf(shape)

if (idx !== -1) {
this.shapes.splice(idx, 1)
this.aabbNeedsUpdate = true
shape.body = null
return true
} else {
return false
const l = this.shapes.length;
const shapes = this.shapes;
for (let i = 0; i < l; i++) {
if (shapes[i] === shape) {
shapes.splice(i, 1);
this.aabbNeedsUpdate = true;
shape.body = null;
return true;
}
}
return false;
}

/**
Expand Down Expand Up @@ -971,8 +972,9 @@ export class Body extends EventEmitter<BodyEventMap> {
}

// Copy the path
const p = []
for (let i = 0; i < path.length; i++) {
const p = [];
const l = path.length;
for (let i = 0; i < l; i++) {
p[i] = vec2.clone(path[i])
}

Expand All @@ -998,8 +1000,9 @@ export class Body extends EventEmitter<BodyEventMap> {
}

// Save this path for later
const concavePath: Vec2[] = (this.concavePath = [])
for (let i = 0; i < p.length; i++) {
const concavePath: Vec2[] = (this.concavePath = []);
const pl = p.length;
for (let i = 0; i < pl; i++) {
concavePath[i] = vec2.clone(p[i])
}

Expand All @@ -1014,15 +1017,17 @@ export class Body extends EventEmitter<BodyEventMap> {
convexes = decomp.quickDecomp(p as decomp.Polygon)
}

const cm = vec2create()
const cm = vec2create();

// Add convexes
for (let i = 0; i !== convexes.length; i++) {
const convexL = convexes.length;
for (let i = 0; i !== convexL; i++) {
// Create convex
let c = new Convex({ vertices: convexes[i] })

// Move all vertices so its center of mass is in the local center of the convex
for (let j = 0; j !== c.vertices.length; j++) {
const vertsL = c.vertices.length;
for (let j = 0; j !== vertsL; j++) {
const v = c.vertices[j]
subtract(v, v, c.centerOfMass)
}
Expand Down Expand Up @@ -1258,8 +1263,9 @@ export class Body extends EventEmitter<BodyEventMap> {
// Ignore all the ignored body pairs
// This should probably be done somewhere else for optimization
const ignoreBodies = []
const disabledPairs = this.world.disabledBodyCollisionPairs
for (let i = 0; i < disabledPairs.length; i += 2) {
const disabledPairs = this.world.disabledBodyCollisionPairs;
const l = disabledPairs.length;
for (let i = 0; i < l; i += 2) {
const bodyA = disabledPairs[i]
const bodyB = disabledPairs[i + 1]
if (bodyA === this) {
Expand Down
16 changes: 10 additions & 6 deletions packages/p2-es/src/shapes/Convex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ export class Convex extends Shape {
const triangles = polyk.triangulate(polykVerts)

// Loop over all triangles, add their inertia contributions to I
for (let i = 0; i < triangles.length; i += 3) {
const l = triangles.length;
for (let i = 0; i < l; i += 3) {
const id1 = triangles[i],
id2 = triangles[i + 1],
id3 = triangles[i + 2]
Expand Down Expand Up @@ -221,9 +222,10 @@ export class Convex extends Shape {
vec2.set(cm, 0, 0)
let totalArea = 0

for (let i = 0; i !== triangles.length; i++) {
const l = triangles.length;
for (let i = 0; i !== l; i++) {
const t = triangles[i]
;(a = verts[t[0]]), (b = verts[t[1]]), (c = verts[t[2]])
; (a = verts[t[0]]), (b = verts[t[1]]), (c = verts[t[2]])

vec2.centroid(centroid, a, b, c)

Expand Down Expand Up @@ -266,7 +268,8 @@ export class Convex extends Shape {
const verts = this.vertices
let r2 = 0

for (let i = 0; i !== verts.length; i++) {
const l = verts.length;
for (let i = 0; i !== l; i++) {
const l2 = vec2.squaredLength(verts[i])
if (l2 > r2) {
r2 = l2
Expand All @@ -284,8 +287,9 @@ export class Convex extends Shape {
this.area = 0

const triangles = this.triangles,
verts = this.vertices
for (let i = 0; i !== triangles.length; i++) {
verts = this.vertices;
const l = triangles.length;
for (let i = 0; i !== l; i++) {
const t = triangles[i],
a = verts[t[0]],
b = verts[t[1]],
Expand Down
Loading