Absorbed the colide function into container as a method called overlap

This commit is contained in:
bluesaxman 2022-02-09 12:34:58 -07:00
parent 2ed2791528
commit 3f16141ae8

View File

@ -139,57 +139,38 @@ class container {
} }
overlap(otherContainer, callbackFunction) { overlap(otherContainer, callbackFunction) {
///////////////////////////////// work starts here var slowest = this.movementVector.getLength() <= otherContainer.movementVector.getLength() ? this : otherContainer;
this. var fastest = this.movementVector.getLength() <= otherContainer.movementVector.getLength() ? otherContainer : this;
}
}
function collide(objectA,objectB,callBackFunction) { var quickest = fastest.movementVector.getLength();
var ASpeed = Math.abs(objectA.speed); for (var t = 0; t <= quickest; t++) {
var BSpeed = Math.abs(objectB.speed); var tFraction = t/(quickest+1);
var simAX = ASpeed <= BSpeed ? objectA.x : objectB.x; var Atop = slowest.coordinates.d[1]+(tFraction * slowest.movementVector.d[1]);
var simAY = ASpeed <= BSpeed ? objectA.y : objectB.y; var Abottom = slowest.coordinates.d[1]+(tFraction * slowest.movementVector.d[1])+slowest.dimentions.d[0];
var simBX = ASpeed <= BSpeed ? objectB.x : objectA.x; var Aleft = slowest.coordinates.d[0]+(tFraction * slowest.movementVector.d[0]);
var simBY = ASpeed <= BSpeed ? objectB.y : objectA.y; var Aright = slowest.coordinates.d[0]+(tFraction * slowest.movementVector.d[0])+slowest.dimentions.d[1];
var simADir = ASpeed <= BSpeed ? objectA.direction : objectB.direction; var Btop = fastest.coordinates.d[1]+(tFraction * fastest.movementVector.d[1]);
var simBDir = ASpeed <= BSpeed ? objectB.direction : objectA.direction; var Bbottom = fastest.coordinates.d[1]+(tFraction * fastest.movementVector.d[1])+fastest.dimentions.d[0];
var simASpeed = ASpeed <= BSpeed ? objectA.speed : objectB.speed; var Bleft = fastest.coordinates.d[0]+(tFraction * fastest.movementVector.d[0]);
var simBSpeed = ASpeed <= BSpeed ? objectB.speed : objectA.speed; var Bright = fastest.coordinates.d[0]+(tFraction * fastest.movementVector.d[0])+fastest.dimentions.d[1];
var simAheight = ASpeed <= BSpeed ? objectA.sizeX : objectB.sizeX;
var simBheight = ASpeed <= BSpeed ? objectB.sizeX : objectA.sizeX; var Abound = new bound([Aleft,Atop],[Aright,Abottom]);
var simAwidth = ASpeed <= BSpeed ? objectA.sizeY : objectB.sizeY; var Bbound = new bound([Bleft,Btop],[Bright,Bbottom]);
var simBwidth = ASpeed <= BSpeed ? objectB.sizeY : objectA.sizeY;
var thetaA = deg2rad(simADir);
var deltaAX = Math.cos(thetaA)*simASpeed;
var deltaAY = Math.sin(thetaA)*simASpeed;
var thetaB = deg2rad(simBDir);
var deltaBX = Math.cos(thetaB)*simBSpeed;
var deltaBY = Math.sin(thetaB)*simBSpeed;
var fastest = Math.max(Math.abs(objectA.speed),Math.abs(objectB.speed));
for (var t = 0; t <= fastest; t++) {
var tFraction = t/(fastest+1);
var Atop = simAY+(tFraction * deltaAY);
var Abottom = simAY+(tFraction * deltaAY)+simAheight;
var Aleft = simAX+(tFraction * deltaAX);
var Aright = simAX+(tFraction * deltaAX)+simAwidth;
var Btop = simBY+(tFraction * deltaBY);
var Bbottom = simBY+(tFraction * deltaBY)+simBheight;
var Bleft = simBX+(tFraction * deltaBX);
var Bright = simBX+(tFraction * deltaBX)+simBwidth;
if ( if (
pointInside(Aleft,Atop,Btop,Bright,Bbottom,Bleft) | Bbound.inbounds([Aleft,Atop]) |
pointInside(Aright,Atop,Btop,Bright,Bbottom,Bleft) | Bbound.inbounds([Aright,Atop]) |
pointInside(Aleft,Abottom,Btop,Bright,Bbottom,Bleft) | Bbound.inbounds([Aleft,Abottom]) |
pointInside(Aright,Abottom,Btop,Bright,Bbottom,Bleft) | Bbound.inbounds([Aright,Abottom]) |
pointInside(Bleft,Btop,Atop,Aright,Abottom,Aleft) | Abound.inbounds([Bleft,Btop]) |
pointInside(Bright,Btop,Atop,Aright,Abottom,Aleft) | Abound.inbounds([Bright,Btop]) |
pointInside(Bleft,Bbottom,Atop,Aright,Abottom,Aleft) | Abound.inbounds([Bleft,Bbottom]) |
pointInside(Bright,Bbottom,Atop,Aright,Abottom,Aleft) Abound.inbounds([Bright,Bbottom])
){ ){
return callBackFunction(objectA,objectB,t); return callBackFunction(this,otherContainer,t);
} }
} }
return false; return false;
}
} }
function chase (source,target,max,minDis,acc) { function chase (source,target,max,minDis,acc) {