Skip to content

Commit

Permalink
hunting for speed improvements, removing arrow functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gorkermann committed Jun 4, 2024
1 parent 4e4ccd8 commit 652b653
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 65 deletions.
8 changes: 6 additions & 2 deletions Contact.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Entity } from './Entity.js'
import { Shape } from './Shape.js'
import { Vec2 } from './Vec2.js'

export class Contact {
Expand All @@ -8,13 +9,15 @@ export class Contact {
normal: Vec2; // normal to surface of other entity
vel: Vec2 = new Vec2(); // at point on other entity
ovel: Vec2 = new Vec2();
slice: number = 0.0; // portion of primary entity which is outside of the contacted edge of the other entity
slice: number = 0.0; // portion of primary entity which is outside of the contacted edge of the other entity

otherShape: Shape;

/* debug only */

inters: Array<Vec2> = [];

constructor( sub: Entity, otherSub: Entity, point: Vec2, normal: Vec2 ) {
constructor( sub: Entity, otherSub: Entity, otherShape: Shape, point: Vec2, normal: Vec2 ) {
if ( !point ) {
console.error( 'Contact.constructor: point is ' + point );
}
Expand All @@ -30,6 +33,7 @@ export class Contact {

this.sub = sub;
this.otherSub = otherSub;
this.otherShape = otherShape;
this.point = point;
this.normal = normal;
}
Expand Down
17 changes: 8 additions & 9 deletions Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Vec2 } from './Vec2.js'
import { toToast } from './serialization.js'
import { Shape, LocalPoint, WorldPoint } from './Shape.js'
import { Dict } from './util.js'
import { SolverResult } from './collisionSolver.js'

export function cullList( list: Array<any>, func: ( arg0: any ) => boolean=null ): Array<any> {
let result: Array<any> = [];
Expand Down Expand Up @@ -213,6 +214,8 @@ export class Entity {
}
}

updateGrav( grav: Vec2 ) {}

update() {}

_updateRecur() {
Expand Down Expand Up @@ -516,14 +519,10 @@ export class Entity {
if ( shape.minmax.length == 0 ) shape.calcMinMax();
if ( otherShape.minmax.length == 0 ) otherShape.calcMinMax();

if ( shape.minmax[0].x < otherShape.minmax[0].x - 1 &&
shape.minmax[1].x < otherShape.minmax[0].x - 1) continue;
if ( shape.minmax[0].x > otherShape.minmax[1].x + 1 &&
shape.minmax[1].x > otherShape.minmax[1].x + 1 ) continue;
if ( shape.minmax[0].y < otherShape.minmax[0].y - 1 &&
shape.minmax[1].y < otherShape.minmax[0].y - 1 ) continue;
if ( shape.minmax[0].y > otherShape.minmax[1].y + 1 &&
shape.minmax[1].y > otherShape.minmax[1].y + 1 ) continue;
if ( shape.minmax[1].x < otherShape.minmax[0].x - 1 ) continue;
if ( shape.minmax[0].x > otherShape.minmax[1].x + 1 ) continue;
if ( shape.minmax[1].y < otherShape.minmax[0].y - 1 ) continue;
if ( shape.minmax[0].y > otherShape.minmax[1].y + 1 ) continue;

// contact
let contact = null;
Expand Down Expand Up @@ -626,7 +625,7 @@ export class Entity {
delete this.savedVals['pos'];
}

/* drawing */
/* Drawing */

shade() {
for ( let sub of this.getSubs() ) {
Expand Down
26 changes: 13 additions & 13 deletions EntityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,40 +194,40 @@ export class EntityManager {
this.entities.push( entity );
}

entity.doForAllChildren( ( e: Entity ) => {
let index = this.entitiesById.indexOf( e );
for ( let flat of entity.getFlatList() ) {
let index = this.entitiesById.indexOf( flat );

// already in array
if ( index >= 0 ) {
e.id = index;
flat.id = index;
return;
}

// id collision
if ( this.entitiesById[e.id] ) {
e.id = -1;
if ( this.entitiesById[flat.id] ) {
flat.id = -1;
}

// search for lowest unused id
if ( e.id < 0 ) {
if ( flat.id < 0 ) {
for ( let i = 0; i < this.entitiesById.length; i++ ) {
if ( !this.entitiesById[i] ) {
this.entitiesById[i] = e;
e.id = i;
this.entitiesById[i] = flat;
flat.id = i;

break;
}
}

if ( e.id < 0 ) {
e.id = this.entitiesById.length;
this.entitiesById.push( e );
if ( flat.id < 0 ) {
flat.id = this.entitiesById.length;
this.entitiesById.push( flat );
}

} else {
this.entitiesById[e.id] = e;
this.entitiesById[flat.id] = flat;
}
} );
}
}

insertList( entities: Array<Entity> ) {
Expand Down
16 changes: 16 additions & 0 deletions Material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,22 @@ export class Material {
return mat;
}

setFrom( mat: Material ) {
this.hue = mat.hue;
this.sat = mat.sat;
this.lum = mat.lum;

this.shaderIndex = mat.shaderIndex;

this.alpha = mat.alpha;

this.skewH = mat.skewH;
this.skewS = mat.skewS;
this.skewL = mat.skewL;

this.emit = mat.emit;
}

getHSLA(): HSLA {
return shaders[this.shaderIndex]( this );
}
Expand Down
78 changes: 54 additions & 24 deletions Shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ function getTurnAngle( l1: Line, l2: Line ) {
return Math.asin( v1.cross( v2 ) );
}

function hitSortFunc ( a: ShapeHit, b: ShapeHit ) {
return a.dist - b.dist;
}

let VEL_EPSILON = 0.0001

type A = {
Expand All @@ -56,6 +60,7 @@ export class ShapeHit extends RayHit {
dist: number;
incidentDot: number;
normalDist: number;
cornerScore: number = 0;

constructor( point: Vec2, normal: Vec2, material: Material ) {
super( point, normal, material );
Expand Down Expand Up @@ -355,7 +360,7 @@ export class Shape {
// Sort in order of closest to farthest
//rayHits.sort( closestTo( ray.p1 ) );

result.sort( ( a: ShapeHit, b: ShapeHit ) => a.dist - b.dist );
result.sort( hitSortFunc );

return result;
}
Expand All @@ -380,9 +385,9 @@ export class Shape {
hit.normal.set( this.normals[i] );

if ( edge.material ) {
hit.material = edge.material.copy();
hit.material.setFrom( edge.material ); // EDIT (is this better ta)
} else {
hit.material = this.material.copy();
hit.material.setFrom( this.material );
}

hit.dist = dist;
Expand Down Expand Up @@ -498,7 +503,7 @@ export class Shape {
normal = new Vec2( 1, 0 );
}

let contact = new Contact( null, null, point, normal );
let contact = new Contact( null, null, otherShape, point, normal );

contact.vel = vel;
contact.slice = 1.0; // insideThis: a shape is always to the right of its own normals
Expand Down Expand Up @@ -537,14 +542,16 @@ export class Shape {
/*
inters.length == 0 means one shape is either completely inside or outside of the other
implying that an earlier collision was missed
implying no collision, or that an earlier collision was missed
inters.length == 1 means either:
one corner of either shape is right on an edge of the other,
and one of the intersections has been rejected
or the shape has an unconnected edge (edge.p2 != nextEdge.p1)
(this is rare)
*/
if ( inters.length < 1 ) {
return this.getBodyContact( otherShape );
Expand Down Expand Up @@ -622,16 +629,16 @@ export class Shape {
let push = normal.times( dot );

nvel.add( push ); // no friction
ovel.add( selfPush.minus( push ) );
ovel.add( selfPush.minus( push ) ); // GDS 6/3/2024: I don't understand what this is for
} else {
//ovel.add( selfPush );
}
}

// create contact
contact = new Contact( null, null, point, normal );
contact = new Contact( null, null, otherShape, point, normal );
contact.vel = nvel;
contact.ovel = ovel
contact.ovel = ovel;
contact.slice = slice;

if ( Debug.CONTACT_INTERS ) {
Expand Down Expand Up @@ -669,13 +676,21 @@ export class Shape {
return sum / 2;
}

// TODO: name forEachIndex arrow funcs for speed improvement
// how much of the shape is to the left of the line (0.0=none, 0.5=half, 1.0=all)
slice( line: Line ): number {
//return 0.5; // EDIT: not using this anywhere?

// check whether any of the points are on different sides of the line
let sides = line.whichSide( this.points );

let leftCount = sides.filter( x => x < 0 ).length;
let rightCount = sides.filter( x => x > 0 ).length;
let leftCount = 0;
let rightCount = 0;

for ( let side of sides ) {
if ( side < 0 ) leftCount += 1;
if ( side > 0 ) rightCount += 1;
}

if ( leftCount == 0 ) return 0.0; // all points are on the right
if ( rightCount == 0 ) return 1.0; // all points are on the left
Expand All @@ -685,28 +700,38 @@ export class Shape {
inters.fill( null );

// find edges which intersect the line
this.forEachIndex( ( i, iNext ) => {
if ( sides[i] != 0 && sides[iNext] != 0 && sides[i] == sides[iNext] ) return;

let inter = this.edges[i].intersects( line, true )
for ( let i = 0, iNext = 0; i < this.points.length; i++ ) {
iNext = ( i + 1 ) % this.points.length;

if ( inter != null ) {
inters[i] = inter;
}
} );
//this.forEachIndex( ( i, iNext ) => {
if ( sides[i] == sides[iNext] && sides[i] != 0 && sides[iNext] != 0 ) continue;

let inter = this.edges[i].intersects( line, true )

if ( inter != null ) {
inters[i] = inter;
}
//} );
}

// remove intersections that are at point 1 of an edge
// (leaving the intersection at point 2 of the next edge)
this.forEachIndex( ( i, iNext ) => {
for ( let i = 0, iNext = 0; i < this.points.length; i++ ) {
iNext = ( i + 1 ) % this.points.length;
//this.forEachIndex( ( i, iNext ) => {
if ( inters[i] && inters[iNext] &&
this.edges[i].p2.equals( inters[i] ) ) {

inters[iNext] = null;
}
} );
//} );
}

// sort intersections in by farthest along in the direction of the line
let sortedInters = inters.filter( x => x !== null );
// sort intersections by farthest along in the direction of the line
let sortedInters = [];
for ( let inter of inters ) {
if ( inter ) sortedInters.push( inter );
}

if ( sortedInters.length == 0 ) {
throw new Error( 'Shape.slice: no intersections' );
Expand All @@ -722,7 +747,11 @@ export class Shape {
let strand: Array<Vec2> = []
let leftArea = 0;

this.forEachIndex( ( i, iNext ) => {
//this.forEachIndex( ( i, iNext ) => {
for ( let j = 0; j < this.points.length; j++ ) {
let i = ( j + startIndex ) % this.points.length;
let iNext = ( i + 1 ) % this.points.length;

if ( inters[i] ) {
strand.push( inters[i] );

Expand All @@ -740,7 +769,8 @@ export class Shape {
if ( s != 0 ) side = s;

strand.push( this.edges[i].p2 );
}, startIndex );
//}, startIndex );
}

return leftArea / this.getArea();
}
Expand Down
Loading

0 comments on commit 652b653

Please sign in to comment.