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) {
bestT = t;
intersections.push({
point: ip,
normal: n,
point: ip.clone(),
normal: n.clone(),
object: cast this,
t: t
});

View file

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

View file

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