Skip to content

Commit

Permalink
made some changes to reduce memory reallocation
Browse files Browse the repository at this point in the history
  • Loading branch information
gorkermann committed May 30, 2024
1 parent bdefd65 commit 4e4ccd8
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 70 deletions.
17 changes: 13 additions & 4 deletions Angle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export class Angle {
}

static between( angle: number, min: Angle_HalfTurn, max: Angle_HalfTurn ) {
if ( Math.abs( max - min ) > Math.PI * 2 - 0.01 ) return true; // sweep covers entire circle
//if ( Math.abs( max - min ) > Math.PI * 2 - 0.01 ) return true; // sweep covers entire circle
if ( max - min > Math.PI * 2 - 0.01 ) return true; // sweep covers entire circle
if ( min - max > Math.PI * 2 - 0.01 ) return true; // sweep covers entire circle

let sweep: Angle_PosTurn = ( max - min ) % ( Math.PI * 2 ); // modulus not necessary if min/max are actually half turns or less
if ( sweep < 0 ) sweep += Math.PI * 2;
Expand All @@ -34,7 +36,7 @@ export class Angle {
return diff <= sweep;
}

static getSweep( points: Array<Vec2>, origin: Vec2 ): [Angle_HalfTurn, Angle_HalfTurn] {
static getSweep( points: Array<Vec2>, origin: Vec2, target?: [Angle_HalfTurn, Angle_HalfTurn] ): [Angle_HalfTurn, Angle_HalfTurn] {
let min, max: Angle_Unbound;
let startAngle, prevAngle: Angle_HalfTurn;
let balance: Angle_Unbound;
Expand Down Expand Up @@ -76,8 +78,15 @@ export class Angle {
} else {
min = Angle.normalize( startAngle + min );
max = Angle.normalize( startAngle + max );
}
}

if ( !target ) {
return [min, max];
} else {
target[0] = min;
target[1] = max;

return [min, max];
return target;
}
}
}
8 changes: 5 additions & 3 deletions Anim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ export class Anim {
for ( let targetKey in frame.targets ) {
for ( let otherFrame of otherThread ) {
if ( targetKey in otherFrame.targets ) {
throw new Error( this.name + ': target collison for ' + targetKey );
throw new Error( this.name + ': target coillison for ' + targetKey );
}
}
}
Expand All @@ -667,9 +667,11 @@ export class Anim {
let threadIndex = Math.floor( options.threadIndex );

try {
if ( this.initFrame( frame ) &&
!this.matchesOtherThread( frame, this.threads[threadIndex] ) ) {
// hard to check if target collisions will actually occur
//if ( this.initFrame( frame ) &&
// !this.matchesOtherThread( frame, this.threads[threadIndex] ) ) {

if ( this.initFrame( frame ) ) {
if ( options.tag ) frame.tag = options.tag;
frame.delay = options.delay;

Expand Down
77 changes: 56 additions & 21 deletions Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export function cullList( list: Array<any>, func: ( arg0: any ) => boolean=null
return result;
}

export type GetShapesOptions = {
useCached?: boolean,
local?: boolean
}

type ApplyTransformOptions = {
angleOnly?: boolean,
local?: boolean
Expand All @@ -61,6 +66,8 @@ export class Entity {
parent: Entity = null;
_subs: Array<Entity> = [];

messages: Array<string> = [];

pos: Vec2 = new Vec2(); // position relative to parent
vel: Vec2 = new Vec2(); // added to pos in advance()
angle: number = 0.0; // if transformOrder=TRANSLATE_THEN_ROTATE, angle relative to parent
Expand All @@ -74,6 +81,7 @@ export class Entity {
width: number; // default width (diameter for a circular entity)
height: number; // default height

alpha: number = 1.0;
material: Material = new Material( 0, 0, 0.5 ); // default color

presetShapes: Array<Shape> = []; // overrides return value of this._getDefaultShapes(). Only updated when basic shapes are changed
Expand Down Expand Up @@ -249,8 +257,10 @@ export class Entity {
}

// don't override! override getOwnShapes instead
getShapes( step: number=0.0, useCached: boolean=false ): Array<Shape> {
if ( !this.parent && useCached ) {
getShapes( step: number=0.0, options: GetShapesOptions={} ): Array<Shape> {
if ( options.local === undefined ) options.local = true;

if ( !this.parent && options.useCached ) {
if ( this.cachedShapes[0] ) {
if ( step == 0.0 || !this.inMotion ) return this.cachedShapes[0];
}
Expand All @@ -260,22 +270,24 @@ export class Entity {
let shapes: Array<Shape> = this.getOwnShapes();

for ( let sub of this.getSubs() ) {
shapes.push( ...sub.getShapes( step ) );
for ( let sshape of sub.getShapes( step ) ) { // don't pass options
shapes.push( sshape );
}
}

for ( let shape of shapes ) {
for ( let p of shape.points ) {
this.applyTransform( p, step, { local: true } );
this.applyTransform( p, step, { local: options.local } );
}

for ( let n of shape.normals ) {
this.applyTransform( n, step, { local: true, angleOnly: true } );
this.applyTransform( n, step, { local: options.local, angleOnly: true } );
}
}

for ( let shape of shapes ) {
shape.calcMinMax();
}
// for ( let shape of shapes ) {
// shape.calcMinMax();
// }

return shapes;
}
Expand Down Expand Up @@ -384,6 +396,12 @@ export class Entity {
return p;
}

getAlpha(): number {
if ( this.alpha == 0.0 ) return 0.0;
else if ( !this.parent ) return this.alpha;
else return this.alpha * this.parent.getAlpha();
}

/* children */

cull() {
Expand Down Expand Up @@ -420,6 +438,16 @@ export class Entity {
}
}

getFlatList(): Array<Entity> {
let result: Array<Entity> = [this];

for ( let sub of this.getSubs() ) {
result = result.concat( sub.getFlatList() );
}

return result;
}

spawnEntity( newEntity: Entity ): void {
newEntity.collisionGroup = this.collisionGroup;
newEntity.collisionMask = this.collisionMask;
Expand Down Expand Up @@ -456,8 +484,8 @@ export class Entity {
}

overlaps ( otherEntity: Entity, step: number, useCached: boolean=false ): Array<Contact> {
let shapes = this.getShapes( step, useCached );
let otherShapes = otherEntity.getShapes( step, useCached );
let shapes = this.getShapes( step, { useCached: useCached } );
let otherShapes = otherEntity.getShapes( step, { useCached: useCached } );

/*
IDEAS
Expand All @@ -469,10 +497,22 @@ export class Entity {
output multiple contacts for entities with multiple shapes
*/

let contacts: Array<Contact> = [];
let contacts: Array<Contact>;
let sub, otherSub: Entity;

for ( let shape of shapes ) {
for ( let otherShape of otherShapes ) {

// collision group
sub = shape.parent;
if ( !sub.collisionGroup ) sub = this;

otherSub = otherShape.parent;
if ( !otherSub.collisionGroup ) otherSub = otherEntity;

if ( !sub.canBeHitBy( otherSub ) ) continue;

// boundary box
if ( shape.minmax.length == 0 ) shape.calcMinMax();
if ( otherShape.minmax.length == 0 ) otherShape.calcMinMax();

Expand All @@ -485,28 +525,23 @@ export class Entity {
if ( shape.minmax[0].y > otherShape.minmax[1].y + 1 &&
shape.minmax[1].y > otherShape.minmax[1].y + 1 ) continue;

// contact
let contact = null;
let maxScore = 0;

let sub = shape.parent;
if ( !sub.collisionGroup ) sub = this;

let otherSub = otherShape.parent;
if ( !otherSub.collisionGroup ) otherSub = otherEntity;

if ( !sub.canBeHitBy( otherSub ) ) continue;

contact = shape.getEdgeContact( otherShape );

if ( contact ) {
if ( !contacts ) contacts = [];

contact.sub = sub;
contact.otherSub = otherSub;
contacts.push( contact );
}
}
}

return contacts;
return contacts ? contacts : [];
}

hitWithMultiple( otherEntity: Entity, contacts: Array<Contact> ): void {
Expand Down Expand Up @@ -536,7 +571,7 @@ export class Entity {
let output = [];

for ( let shape of this.getShapes() ) {
if ( shape.contains( p, 0.0, false ) ) {
if ( shape.contains( p, 0.0 ) ) {
output.push( shape.parent );
}
}
Expand Down
15 changes: 10 additions & 5 deletions EntityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,24 @@ export class EntityManager {

entity.inMotion = false;

entity.doForAllChildren( ( child ) => {
if ( entity.inMotion ) return;

if ( child.vel.lengthSq() > 0.0 || child.angleVel != 0.0 ) {
for ( let flat of entity.getFlatList() ) {
if ( flat.vel.lengthSq() > 0.0 || flat.angleVel != 0.0 ) {
entity.inMotion = true;
break;
}
} );
}

if ( entity.cachedShapes.length == 2 && !entity.inMotion ) {
continue;
}

entity.cachedShapes = [];
entity.cachedShapes[0] = entity.getShapes( 0.0 );

if ( entity.inMotion ) {
entity.cachedShapes[1] = entity.getShapes( 1.0 );
} else {
entity.cachedShapes[1] = entity.cachedShapes[0];
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions Line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,14 @@ export class Line {

// find intersection
let result = null;
let dx_this = Math.abs( this.p2.x - this.p1.x );
let dx_line = Math.abs( line.p2.x - line.p1.x );
//let dx_this = Math.abs( this.p2.x - this.p1.x ); // EDIT
//let dx_line = Math.abs( line.p2.x - line.p1.x ); // EDIT

let dx_this = this.p2.x - this.p1.x;
if ( dx_this < 0 ) dx_this *= -1;

let dx_line = line.p2.x - line.p1.x;
if ( dx_line < 0 ) dx_line *= -1;

if ( dx_this < 0.01 && dx_line < 0.01 ) {
// both vertical, do nothing, return null
Expand Down
Loading

0 comments on commit 4e4ccd8

Please sign in to comment.