optimize raycast

This commit is contained in:
RandomityGuy 2024-07-14 20:00:01 +05:30
parent 80aa53997c
commit d74b8d81be
3 changed files with 18 additions and 9 deletions

View file

@ -155,8 +155,8 @@ class CollisionSurface implements IOctreeObject implements IBVHObject {
if (t < bestT) { if (t < bestT) {
bestT = t; bestT = t;
intersections.push({ intersections.push({
point: ip, point: ip.clone(),
normal: n, normal: n.clone(),
object: cast this, object: cast this,
t: t t: t
}); });

View file

@ -128,27 +128,36 @@ class Grid {
var cell = origin.sub(this.bounds.getMin().toVector()); var cell = origin.sub(this.bounds.getMin().toVector());
cell.x /= this.cellSize.x; cell.x /= this.cellSize.x;
cell.y /= this.cellSize.y; cell.y /= this.cellSize.y;
var destCell = origin.add(direction.multiply(bestT)).sub(this.bounds.getMin().toVector());
destCell.x /= this.cellSize.x;
destCell.y /= this.cellSize.y;
var stepX, outX, X = Math.floor(cell.x); var stepX, outX, X = Math.floor(cell.x);
var stepY, outY, Y = Math.floor(cell.y); var stepY, outY, Y = Math.floor(cell.y);
var destX = Math.max(Math.floor(destCell.x), 0);
var destY = Math.max(Math.floor(destCell.y), 0);
if ((X < 0) || (X >= CELL_DIV.x) || (Y < 0) || (Y >= CELL_DIV.y)) if ((X < 0) || (X >= CELL_DIV.x) || (Y < 0) || (Y >= CELL_DIV.y))
return []; return [];
var cb = new Vector(); var cb = new Vector();
if (direction.x > 0) { if (direction.x > 0) {
stepX = 1; stepX = 1;
outX = CELL_DIV.x; outX = destX;
if (outX == X)
outX = Math.min(CELL_DIV.x, outX + 1);
cb.x = this.bounds.xMin + (X + 1) * this.cellSize.x; cb.x = this.bounds.xMin + (X + 1) * this.cellSize.x;
} else { } else {
stepX = -1; stepX = -1;
outX = -1; outX = destX - 1;
cb.x = this.bounds.xMin + X * this.cellSize.x; cb.x = this.bounds.xMin + X * this.cellSize.x;
} }
if (direction.y > 0.0) { if (direction.y > 0.0) {
stepY = 1; stepY = 1;
outY = CELL_DIV.y; outY = destY;
if (outY == Y)
outY = Math.min(CELL_DIV.y, outY + 1);
cb.y = this.bounds.yMin + (Y + 1) * this.cellSize.y; cb.y = this.bounds.yMin + (Y + 1) * this.cellSize.y;
} else { } else {
stepY = -1; stepY = -1;
outY = -1; outY = destY - 1;
cb.y = this.bounds.yMin + Y * this.cellSize.y; cb.y = this.bounds.yMin + Y * this.cellSize.y;
} }
var tmax = new Vector(); var tmax = new Vector();

View file

@ -93,10 +93,10 @@ class SphereCollisionEntity extends CollisionEntity {
contact.collider = this; contact.collider = this;
contact.friction = 1; contact.friction = 1;
contact.restitution = 1; contact.restitution = 1;
contact.velocity = this.velocity.clone(); contact.velocity.load(this.velocity);
contact.otherObject = this.go; contact.otherObject = this.go;
contact.point = position.add(normDist); contact.point.load(position.add(normDist));
contact.normal = normDist.multiply(-1); contact.normal.load(normDist.multiply(-1));
contact.force = 0; contact.force = 0;
contact.contactDistance = contact.point.distance(position); contact.contactDistance = contact.point.distance(position);
contacts.push(contact); contacts.push(contact);