mirror of
https://github.com/RandomityGuy/MBHaxe.git
synced 2026-05-10 19:41:39 +00:00
attempt improve CCD
This commit is contained in:
parent
e78343ce39
commit
f5b5729972
2 changed files with 236 additions and 206 deletions
|
|
@ -577,6 +577,7 @@ class DtsObject extends GameObject {
|
||||||
hs.generateBoundingBox();
|
hs.generateBoundingBox();
|
||||||
chull.addSurface(hs);
|
chull.addSurface(hs);
|
||||||
chull.generateBoundingBox();
|
chull.generateBoundingBox();
|
||||||
|
chull.finalize();
|
||||||
hulls.push(chull);
|
hulls.push(chull);
|
||||||
}
|
}
|
||||||
return hulls;
|
return hulls;
|
||||||
|
|
|
||||||
441
src/Marble.hx
441
src/Marble.hx
|
|
@ -1,5 +1,6 @@
|
||||||
package src;
|
package src;
|
||||||
|
|
||||||
|
import collision.CollisionHull;
|
||||||
import dif.Plane;
|
import dif.Plane;
|
||||||
import shaders.marble.ClassicGlass;
|
import shaders.marble.ClassicGlass;
|
||||||
import shaders.marble.ClassicMetal;
|
import shaders.marble.ClassicMetal;
|
||||||
|
|
@ -1027,174 +1028,246 @@ class Marble extends GameObject {
|
||||||
+ relLocalVel.z * deltaT * 5,
|
+ relLocalVel.z * deltaT * 5,
|
||||||
Math.max(Math.max(sphereRadius.x, sphereRadius.y), sphereRadius.z) * 2);
|
Math.max(Math.max(sphereRadius.x, sphereRadius.y), sphereRadius.z) * 2);
|
||||||
|
|
||||||
var surfaces = obj.bvh == null ? obj.octree.boundingSearch(boundThing).map(x -> cast x) : obj.bvh.boundingSearch(boundThing);
|
var currentFinalPos = position.add(relVel.multiply(finalT)); // localpos.add(relLocalVel.multiply(finalT));
|
||||||
|
|
||||||
for (surf in surfaces) {
|
if (obj.go is DtsObject) {
|
||||||
var surface:CollisionSurface = cast surf;
|
var chull = cast(obj, CollisionHull);
|
||||||
|
var rayisecs = chull.rayCast(position, velocity);
|
||||||
var currentFinalPos = position.add(relVel.multiply(finalT)); // localpos.add(relLocalVel.multiply(finalT));
|
if (rayisecs.length != 0) {
|
||||||
|
var raymax = rayisecs[0];
|
||||||
var i = 0;
|
var mindist = raymax.point.distanceSq(position);
|
||||||
while (i < surface.indices.length) {
|
if (rayisecs.length > 1) {
|
||||||
var verts = surface.transformTriangle(i, obj.transform, invTform, @:privateAccess obj._transformKey);
|
for (i in 0...rayisecs.length) {
|
||||||
// var v0 = surface.points[surface.indices[i]].transformed(tform);
|
var dd = rayisecs[i].point.distanceSq(position);
|
||||||
// var v = surface.points[surface.indices[i + 1]].transformed(tform);
|
if (dd < mindist) {
|
||||||
// var v2 = surface.points[surface.indices[i + 2]].transformed(tform);
|
mindist = dd;
|
||||||
var v0 = verts.v1;
|
raymax = rayisecs[i];
|
||||||
var v = verts.v2;
|
}
|
||||||
var v2 = verts.v3;
|
}
|
||||||
// var v0 = surface.points[surface.indices[i]].transformed(obj.transform);
|
|
||||||
// var v = surface.points[surface.indices[i + 1]].transformed(obj.transform);
|
|
||||||
// var v2 = surface.points[surface.indices[i + 2]].transformed(obj.transform);
|
|
||||||
|
|
||||||
var triangleVerts = [v0, v, v2];
|
|
||||||
|
|
||||||
var surfaceNormal = verts.n; // surface.normals[surface.indices[i]].transformed3x3(obj.transform).normalized();
|
|
||||||
var surfaceD = -surfaceNormal.dot(v0);
|
|
||||||
|
|
||||||
// If we're going the wrong direction or not going to touch the plane, ignore...
|
|
||||||
if (surfaceNormal.dot(relVel) > -0.001 || surfaceNormal.dot(currentFinalPos) + surfaceD > radius) {
|
|
||||||
i += 3;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
var collidePoint = raymax.point;
|
||||||
|
var collideT = (collidePoint.sub(velocity.normalized().multiply(radius)).sub(position).length()) / velocity.length();
|
||||||
|
if (collideT < finalT && collideT > 0.0001) {
|
||||||
|
finalT = collideT;
|
||||||
|
currentFinalPos = position.add(relVel.multiply(finalT));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var surfaces = obj.bvh == null ? obj.octree.boundingSearch(boundThing).map(x -> cast x) : obj.bvh.boundingSearch(boundThing);
|
||||||
|
|
||||||
// var v0T = v0.transformed(obj.transform);
|
for (surf in surfaces) {
|
||||||
// var vT = v.transformed(obj.transform);
|
var surface:CollisionSurface = cast surf;
|
||||||
// var v2T = v2.transformed(obj.transform);
|
|
||||||
// var vN = surfaceNormal.transformed3x3(obj.transform);
|
|
||||||
testTriangles.push({
|
|
||||||
v: [v0, v, v2],
|
|
||||||
n: surfaceNormal,
|
|
||||||
edge: surf.edgeData != null ? surf.edgeData[Math.floor(i / 3)] : 0,
|
|
||||||
concavity: surface.edgeConcavities != null ? surface.edgeConcavities.slice(Math.floor(i / 3),
|
|
||||||
Math.floor(i / 3) + 3) : [false, false, false],
|
|
||||||
});
|
|
||||||
|
|
||||||
// Time until collision with the plane
|
currentFinalPos = position.add(relVel.multiply(finalT));
|
||||||
var collisionTime = (radius - position.dot(surfaceNormal) - surfaceD) / surfaceNormal.dot(relVel);
|
|
||||||
|
|
||||||
// Are we going to touch the plane during this time step?
|
var i = 0;
|
||||||
if (collisionTime >= 0.000001 && finalT >= collisionTime) {
|
while (i < surface.indices.length) {
|
||||||
var collisionPoint = position.add(relVel.multiply(collisionTime));
|
var verts = surface.transformTriangle(i, obj.transform, invTform, @:privateAccess obj._transformKey);
|
||||||
// var lastPoint = v2;
|
// var v0 = surface.points[surface.indices[i]].transformed(tform);
|
||||||
// var j = 0;
|
// var v = surface.points[surface.indices[i + 1]].transformed(tform);
|
||||||
// while (j < 3) {
|
// var v2 = surface.points[surface.indices[i + 2]].transformed(tform);
|
||||||
// var testPoint = surface.points[surface.indices[i + j]];
|
var v0 = verts.v1;
|
||||||
// if (testPoint != lastPoint) {
|
var v = verts.v2;
|
||||||
// var a = surfaceNormal;
|
var v2 = verts.v3;
|
||||||
// var b = lastPoint.sub(testPoint);
|
// var v0 = surface.points[surface.indices[i]].transformed(obj.transform);
|
||||||
// var planeNorm = b.cross(a);
|
// var v = surface.points[surface.indices[i + 1]].transformed(obj.transform);
|
||||||
// var planeD = -planeNorm.dot(testPoint);
|
// var v2 = surface.points[surface.indices[i + 2]].transformed(obj.transform);
|
||||||
// lastPoint = testPoint;
|
|
||||||
// // if we are on the far side of the edge
|
var triangleVerts = [v0, v, v2];
|
||||||
// if (planeNorm.dot(collisionPoint) + planeD >= 0.0)
|
|
||||||
// break;
|
var surfaceNormal = verts.n; // surface.normals[surface.indices[i]].transformed3x3(obj.transform).normalized();
|
||||||
// }
|
if (obj is DtsObject)
|
||||||
// j++;
|
surfaceNormal.multiply(-1);
|
||||||
// }
|
var surfaceD = -surfaceNormal.dot(v0);
|
||||||
// If we're inside the poly, just get the position
|
|
||||||
if (Collision.PointInTriangle(collisionPoint, v0, v, v2)) {
|
// If we're going the wrong direction or not going to touch the plane, ignore...
|
||||||
finalT = collisionTime;
|
if (surfaceNormal.dot(relVel) > -0.001 || surfaceNormal.dot(currentFinalPos) + surfaceD > radius) {
|
||||||
currentFinalPos = position.add(relVel.multiply(finalT));
|
|
||||||
found = true;
|
|
||||||
// iterationFound = true;
|
|
||||||
i += 3;
|
i += 3;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// We *might* be colliding with an edge
|
|
||||||
|
|
||||||
var lastVert = v2;
|
// var v0T = v0.transformed(obj.transform);
|
||||||
|
// var vT = v.transformed(obj.transform);
|
||||||
|
// var v2T = v2.transformed(obj.transform);
|
||||||
|
// var vN = surfaceNormal.transformed3x3(obj.transform);
|
||||||
|
testTriangles.push({
|
||||||
|
v: [v0, v, v2],
|
||||||
|
n: surfaceNormal,
|
||||||
|
edge: surf.edgeData != null ? surf.edgeData[Math.floor(i / 3)] : 0,
|
||||||
|
concavity: surface.edgeConcavities != null ? surface.edgeConcavities.slice(Math.floor(i / 3),
|
||||||
|
Math.floor(i / 3) + 3) : [false, false, false],
|
||||||
|
});
|
||||||
|
|
||||||
var radSq = radius * radius;
|
// Time until collision with the plane
|
||||||
for (iter in 0...3) {
|
var collisionTime = (radius - position.dot(surfaceNormal) - surfaceD) / surfaceNormal.dot(relVel);
|
||||||
var thisVert = triangleVerts[iter];
|
|
||||||
|
|
||||||
var vertDiff = lastVert.sub(thisVert);
|
// Are we going to touch the plane during this time step?
|
||||||
var posDiff = position.sub(thisVert);
|
if (collisionTime >= 0.000001 && finalT >= collisionTime) {
|
||||||
|
var collisionPoint = position.add(relVel.multiply(collisionTime));
|
||||||
var velRejection = vertDiff.cross(relVel);
|
// var lastPoint = v2;
|
||||||
var posRejection = vertDiff.cross(posDiff);
|
// var j = 0;
|
||||||
|
// while (j < 3) {
|
||||||
// Build a quadratic equation to solve for the collision time
|
// var testPoint = surface.points[surface.indices[i + j]];
|
||||||
var a = velRejection.lengthSq();
|
// if (testPoint != lastPoint) {
|
||||||
var b = 2 * posRejection.dot(velRejection);
|
// var a = surfaceNormal;
|
||||||
var c = (posRejection.lengthSq() - vertDiff.lengthSq() * radSq);
|
// var b = lastPoint.sub(testPoint);
|
||||||
|
// var planeNorm = b.cross(a);
|
||||||
var discriminant = b * b - (4 * a * c);
|
// var planeD = -planeNorm.dot(testPoint);
|
||||||
|
// lastPoint = testPoint;
|
||||||
// If it's not quadratic or has no solution, ignore this edge.
|
// // if we are on the far side of the edge
|
||||||
if (a == 0.0 || discriminant < 0.0) {
|
// if (planeNorm.dot(collisionPoint) + planeD >= 0.0)
|
||||||
lastVert = thisVert;
|
// break;
|
||||||
continue;
|
// }
|
||||||
}
|
// j++;
|
||||||
|
|
||||||
var oneOverTwoA = 0.5 / a;
|
|
||||||
var discriminantSqrt = Math.sqrt(discriminant);
|
|
||||||
|
|
||||||
// Solve using the quadratic formula
|
|
||||||
var edgeCollisionTime = (-b + discriminantSqrt) * oneOverTwoA;
|
|
||||||
var edgeCollisionTime2 = (-b - discriminantSqrt) * oneOverTwoA;
|
|
||||||
|
|
||||||
// Make sure the 2 times are in ascending order
|
|
||||||
if (edgeCollisionTime2 < edgeCollisionTime) {
|
|
||||||
var temp = edgeCollisionTime2;
|
|
||||||
edgeCollisionTime2 = edgeCollisionTime;
|
|
||||||
edgeCollisionTime = temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the collision doesn't happen on this time step, ignore this edge.
|
|
||||||
if (edgeCollisionTime2 <= 0.0001 || finalT <= edgeCollisionTime) {
|
|
||||||
lastVert = thisVert;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the collision hasn't already happened
|
|
||||||
if (edgeCollisionTime >= 0.000001) {
|
|
||||||
// if (edgeCollisionTime < 0.000001) {
|
|
||||||
// edgeCollisionTime = edgeCollisionTime2;
|
|
||||||
// }
|
// }
|
||||||
// if (edgeCollisionTime < 0.00001)
|
// If we're inside the poly, just get the position
|
||||||
// continue;
|
if (Collision.PointInTriangle(collisionPoint, v0, v, v2)) {
|
||||||
// if (edgeCollisionTime > finalT)
|
finalT = collisionTime;
|
||||||
// continue;
|
|
||||||
|
|
||||||
var edgeLen = vertDiff.length();
|
|
||||||
|
|
||||||
var relativeCollisionPos = position.add(relVel.multiply(edgeCollisionTime)).sub(thisVert);
|
|
||||||
|
|
||||||
var distanceAlongEdge = relativeCollisionPos.dot(vertDiff) / edgeLen;
|
|
||||||
|
|
||||||
// If the collision happens outside the boundaries of the edge, ignore this edge.
|
|
||||||
if (-radius > distanceAlongEdge || edgeLen + radius < distanceAlongEdge) {
|
|
||||||
lastVert = thisVert;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the collision is within the edge, resolve the collision and continue.
|
|
||||||
if (distanceAlongEdge >= 0.0 && distanceAlongEdge <= edgeLen) {
|
|
||||||
finalT = edgeCollisionTime;
|
|
||||||
currentFinalPos = position.add(relVel.multiply(finalT));
|
currentFinalPos = position.add(relVel.multiply(finalT));
|
||||||
lastContactPos = vertDiff.multiply(distanceAlongEdge / edgeLen).add(thisVert);
|
|
||||||
lastVert = thisVert;
|
|
||||||
found = true;
|
found = true;
|
||||||
// iterationFound = true;
|
// iterationFound = true;
|
||||||
|
i += 3;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// We *might* be colliding with an edge
|
||||||
|
|
||||||
// This is what happens when we collide with a corner
|
var lastVert = v2;
|
||||||
|
|
||||||
a = relVel.lengthSq();
|
var radSq = radius * radius;
|
||||||
|
for (iter in 0...3) {
|
||||||
|
var thisVert = triangleVerts[iter];
|
||||||
|
|
||||||
// Build a quadratic equation to solve for the collision time
|
var vertDiff = lastVert.sub(thisVert);
|
||||||
var posVertDiff = position.sub(thisVert);
|
var posDiff = position.sub(thisVert);
|
||||||
b = 2 * posVertDiff.dot(relVel);
|
|
||||||
c = posVertDiff.lengthSq() - radSq;
|
var velRejection = vertDiff.cross(relVel);
|
||||||
discriminant = b * b - (4 * a * c);
|
var posRejection = vertDiff.cross(posDiff);
|
||||||
|
|
||||||
|
// Build a quadratic equation to solve for the collision time
|
||||||
|
var a = velRejection.lengthSq();
|
||||||
|
var b = 2 * posRejection.dot(velRejection);
|
||||||
|
var c = (posRejection.lengthSq() - vertDiff.lengthSq() * radSq);
|
||||||
|
|
||||||
|
var discriminant = b * b - (4 * a * c);
|
||||||
|
|
||||||
|
// If it's not quadratic or has no solution, ignore this edge.
|
||||||
|
if (a == 0.0 || discriminant < 0.0) {
|
||||||
|
lastVert = thisVert;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var oneOverTwoA = 0.5 / a;
|
||||||
|
var discriminantSqrt = Math.sqrt(discriminant);
|
||||||
|
|
||||||
|
// Solve using the quadratic formula
|
||||||
|
var edgeCollisionTime = (-b + discriminantSqrt) * oneOverTwoA;
|
||||||
|
var edgeCollisionTime2 = (-b - discriminantSqrt) * oneOverTwoA;
|
||||||
|
|
||||||
|
// Make sure the 2 times are in ascending order
|
||||||
|
if (edgeCollisionTime2 < edgeCollisionTime) {
|
||||||
|
var temp = edgeCollisionTime2;
|
||||||
|
edgeCollisionTime2 = edgeCollisionTime;
|
||||||
|
edgeCollisionTime = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the collision doesn't happen on this time step, ignore this edge.
|
||||||
|
if (edgeCollisionTime2 <= 0.0001 || finalT <= edgeCollisionTime) {
|
||||||
|
lastVert = thisVert;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the collision hasn't already happened
|
||||||
|
if (edgeCollisionTime >= 0.000001) {
|
||||||
|
// if (edgeCollisionTime < 0.000001) {
|
||||||
|
// edgeCollisionTime = edgeCollisionTime2;
|
||||||
|
// }
|
||||||
|
// if (edgeCollisionTime < 0.00001)
|
||||||
|
// continue;
|
||||||
|
// if (edgeCollisionTime > finalT)
|
||||||
|
// continue;
|
||||||
|
|
||||||
|
var edgeLen = vertDiff.length();
|
||||||
|
|
||||||
|
var relativeCollisionPos = position.add(relVel.multiply(edgeCollisionTime)).sub(thisVert);
|
||||||
|
|
||||||
|
var distanceAlongEdge = relativeCollisionPos.dot(vertDiff) / edgeLen;
|
||||||
|
|
||||||
|
// If the collision happens outside the boundaries of the edge, ignore this edge.
|
||||||
|
if (-radius > distanceAlongEdge || edgeLen + radius < distanceAlongEdge) {
|
||||||
|
lastVert = thisVert;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the collision is within the edge, resolve the collision and continue.
|
||||||
|
if (distanceAlongEdge >= 0.0 && distanceAlongEdge <= edgeLen) {
|
||||||
|
finalT = edgeCollisionTime;
|
||||||
|
currentFinalPos = position.add(relVel.multiply(finalT));
|
||||||
|
lastContactPos = vertDiff.multiply(distanceAlongEdge / edgeLen).add(thisVert);
|
||||||
|
lastVert = thisVert;
|
||||||
|
found = true;
|
||||||
|
// iterationFound = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is what happens when we collide with a corner
|
||||||
|
|
||||||
|
a = relVel.lengthSq();
|
||||||
|
|
||||||
|
// Build a quadratic equation to solve for the collision time
|
||||||
|
var posVertDiff = position.sub(thisVert);
|
||||||
|
b = 2 * posVertDiff.dot(relVel);
|
||||||
|
c = posVertDiff.lengthSq() - radSq;
|
||||||
|
discriminant = b * b - (4 * a * c);
|
||||||
|
|
||||||
|
// If it's quadratic and has a solution ...
|
||||||
|
if (a != 0.0 && discriminant >= 0.0) {
|
||||||
|
oneOverTwoA = 0.5 / a;
|
||||||
|
discriminantSqrt = Math.sqrt(discriminant);
|
||||||
|
|
||||||
|
// Solve using the quadratic formula
|
||||||
|
edgeCollisionTime = (-b + discriminantSqrt) * oneOverTwoA;
|
||||||
|
edgeCollisionTime2 = (-b - discriminantSqrt) * oneOverTwoA;
|
||||||
|
|
||||||
|
// Make sure the 2 times are in ascending order
|
||||||
|
if (edgeCollisionTime2 < edgeCollisionTime) {
|
||||||
|
var temp = edgeCollisionTime2;
|
||||||
|
edgeCollisionTime2 = edgeCollisionTime;
|
||||||
|
edgeCollisionTime = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the collision doesn't happen on this time step, ignore this corner
|
||||||
|
if (edgeCollisionTime2 > 0.0001 && finalT > edgeCollisionTime) {
|
||||||
|
// Adjust to make sure very small negative times are counted as zero
|
||||||
|
if (edgeCollisionTime <= 0.0 && edgeCollisionTime > -0.0001)
|
||||||
|
edgeCollisionTime = 0.0;
|
||||||
|
|
||||||
|
// Check if the collision hasn't already happened
|
||||||
|
if (edgeCollisionTime >= 0.000001) {
|
||||||
|
// Resolve it and continue
|
||||||
|
finalT = edgeCollisionTime;
|
||||||
|
currentFinalPos = position.add(relVel.multiply(finalT));
|
||||||
|
lastContactPos = thisVert;
|
||||||
|
found = true;
|
||||||
|
// iterationFound = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// We still need to check the other corner ...
|
||||||
|
// Build one last quadratic equation to solve for the collision time
|
||||||
|
posVertDiff = position.sub(lastVert);
|
||||||
|
b = 2 * posVertDiff.dot(relVel);
|
||||||
|
c = posVertDiff.lengthSq() - radSq;
|
||||||
|
discriminant = b * b - (4 * a * c);
|
||||||
|
|
||||||
|
// If it's not quadratic or has no solution, then skip this corner
|
||||||
|
if (a == 0.0 || discriminant < 0.0) {
|
||||||
|
lastVert = thisVert;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// If it's quadratic and has a solution ...
|
|
||||||
if (a != 0.0 && discriminant >= 0.0) {
|
|
||||||
oneOverTwoA = 0.5 / a;
|
oneOverTwoA = 0.5 / a;
|
||||||
discriminantSqrt = Math.sqrt(discriminant);
|
discriminantSqrt = Math.sqrt(discriminant);
|
||||||
|
|
||||||
|
|
@ -1209,73 +1282,29 @@ class Marble extends GameObject {
|
||||||
edgeCollisionTime = temp;
|
edgeCollisionTime = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the collision doesn't happen on this time step, ignore this corner
|
if (edgeCollisionTime2 <= 0.0001 || finalT <= edgeCollisionTime) {
|
||||||
if (edgeCollisionTime2 > 0.0001 && finalT > edgeCollisionTime) {
|
lastVert = thisVert;
|
||||||
// Adjust to make sure very small negative times are counted as zero
|
continue;
|
||||||
if (edgeCollisionTime <= 0.0 && edgeCollisionTime > -0.0001)
|
|
||||||
edgeCollisionTime = 0.0;
|
|
||||||
|
|
||||||
// Check if the collision hasn't already happened
|
|
||||||
if (edgeCollisionTime >= 0.000001) {
|
|
||||||
// Resolve it and continue
|
|
||||||
finalT = edgeCollisionTime;
|
|
||||||
currentFinalPos = position.add(relVel.multiply(finalT));
|
|
||||||
lastContactPos = thisVert;
|
|
||||||
found = true;
|
|
||||||
// iterationFound = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// We still need to check the other corner ...
|
if (edgeCollisionTime <= 0.0 && edgeCollisionTime > -0.0001)
|
||||||
// Build one last quadratic equation to solve for the collision time
|
edgeCollisionTime = 0;
|
||||||
posVertDiff = position.sub(lastVert);
|
|
||||||
b = 2 * posVertDiff.dot(relVel);
|
if (edgeCollisionTime < 0.000001) {
|
||||||
c = posVertDiff.lengthSq() - radSq;
|
lastVert = thisVert;
|
||||||
discriminant = b * b - (4 * a * c);
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
finalT = edgeCollisionTime;
|
||||||
|
currentFinalPos = position.add(relVel.multiply(finalT));
|
||||||
|
|
||||||
// If it's not quadratic or has no solution, then skip this corner
|
|
||||||
if (a == 0.0 || discriminant < 0.0) {
|
|
||||||
lastVert = thisVert;
|
lastVert = thisVert;
|
||||||
continue;
|
found = true;
|
||||||
|
// iterationFound = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
oneOverTwoA = 0.5 / a;
|
i += 3;
|
||||||
discriminantSqrt = Math.sqrt(discriminant);
|
|
||||||
|
|
||||||
// Solve using the quadratic formula
|
|
||||||
edgeCollisionTime = (-b + discriminantSqrt) * oneOverTwoA;
|
|
||||||
edgeCollisionTime2 = (-b - discriminantSqrt) * oneOverTwoA;
|
|
||||||
|
|
||||||
// Make sure the 2 times are in ascending order
|
|
||||||
if (edgeCollisionTime2 < edgeCollisionTime) {
|
|
||||||
var temp = edgeCollisionTime2;
|
|
||||||
edgeCollisionTime2 = edgeCollisionTime;
|
|
||||||
edgeCollisionTime = temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (edgeCollisionTime2 <= 0.0001 || finalT <= edgeCollisionTime) {
|
|
||||||
lastVert = thisVert;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (edgeCollisionTime <= 0.0 && edgeCollisionTime > -0.0001)
|
|
||||||
edgeCollisionTime = 0;
|
|
||||||
|
|
||||||
if (edgeCollisionTime < 0.000001) {
|
|
||||||
lastVert = thisVert;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
finalT = edgeCollisionTime;
|
|
||||||
currentFinalPos = position.add(relVel.multiply(finalT));
|
|
||||||
|
|
||||||
lastVert = thisVert;
|
|
||||||
found = true;
|
|
||||||
// iterationFound = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
i += 3;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue