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) {
///////////////////////////////// work starts here
this.
}
}
function collide(objectA,objectB,callBackFunction) {
var ASpeed = Math.abs(objectA.speed);
var BSpeed = Math.abs(objectB.speed);
var simAX = ASpeed <= BSpeed ? objectA.x : objectB.x;
var simAY = ASpeed <= BSpeed ? objectA.y : objectB.y;
var simBX = ASpeed <= BSpeed ? objectB.x : objectA.x;
var simBY = ASpeed <= BSpeed ? objectB.y : objectA.y;
var simADir = ASpeed <= BSpeed ? objectA.direction : objectB.direction;
var simBDir = ASpeed <= BSpeed ? objectB.direction : objectA.direction;
var simASpeed = ASpeed <= BSpeed ? objectA.speed : objectB.speed;
var simBSpeed = ASpeed <= BSpeed ? objectB.speed : objectA.speed;
var simAheight = ASpeed <= BSpeed ? objectA.sizeX : objectB.sizeX;
var simBheight = ASpeed <= BSpeed ? objectB.sizeX : objectA.sizeX;
var simAwidth = ASpeed <= BSpeed ? objectA.sizeY : objectB.sizeY;
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 (
pointInside(Aleft,Atop,Btop,Bright,Bbottom,Bleft) |
pointInside(Aright,Atop,Btop,Bright,Bbottom,Bleft) |
pointInside(Aleft,Abottom,Btop,Bright,Bbottom,Bleft) |
pointInside(Aright,Abottom,Btop,Bright,Bbottom,Bleft) |
pointInside(Bleft,Btop,Atop,Aright,Abottom,Aleft) |
pointInside(Bright,Btop,Atop,Aright,Abottom,Aleft) |
pointInside(Bleft,Bbottom,Atop,Aright,Abottom,Aleft) |
pointInside(Bright,Bbottom,Atop,Aright,Abottom,Aleft)
){
return callBackFunction(objectA,objectB,t);
var slowest = this.movementVector.getLength() <= otherContainer.movementVector.getLength() ? this : otherContainer;
var fastest = this.movementVector.getLength() <= otherContainer.movementVector.getLength() ? otherContainer : this;
var quickest = fastest.movementVector.getLength();
for (var t = 0; t <= quickest; t++) {
var tFraction = t/(quickest+1);
var Atop = slowest.coordinates.d[1]+(tFraction * slowest.movementVector.d[1]);
var Abottom = slowest.coordinates.d[1]+(tFraction * slowest.movementVector.d[1])+slowest.dimentions.d[0];
var Aleft = slowest.coordinates.d[0]+(tFraction * slowest.movementVector.d[0]);
var Aright = slowest.coordinates.d[0]+(tFraction * slowest.movementVector.d[0])+slowest.dimentions.d[1];
var Btop = fastest.coordinates.d[1]+(tFraction * fastest.movementVector.d[1]);
var Bbottom = fastest.coordinates.d[1]+(tFraction * fastest.movementVector.d[1])+fastest.dimentions.d[0];
var Bleft = fastest.coordinates.d[0]+(tFraction * fastest.movementVector.d[0]);
var Bright = fastest.coordinates.d[0]+(tFraction * fastest.movementVector.d[0])+fastest.dimentions.d[1];
var Abound = new bound([Aleft,Atop],[Aright,Abottom]);
var Bbound = new bound([Bleft,Btop],[Bright,Bbottom]);
if (
Bbound.inbounds([Aleft,Atop]) |
Bbound.inbounds([Aright,Atop]) |
Bbound.inbounds([Aleft,Abottom]) |
Bbound.inbounds([Aright,Abottom]) |
Abound.inbounds([Bleft,Btop]) |
Abound.inbounds([Bright,Btop]) |
Abound.inbounds([Bleft,Bbottom]) |
Abound.inbounds([Bright,Bbottom])
){
return callBackFunction(this,otherContainer,t);
}
}
return false;
}
return false;
}
function chase (source,target,max,minDis,acc) {