|
| 1 | +/* |
| 2 | +Toolbox Aid |
| 3 | +David Quesenberry |
| 4 | +04/15/2026 |
| 5 | +collision3d.js |
| 6 | +*/ |
| 7 | +function toFinite(value, fallback = 0) { |
| 8 | + return Number.isFinite(value) ? value : fallback; |
| 9 | +} |
| 10 | + |
| 11 | +function getAabb3D(bounds) { |
| 12 | + return { |
| 13 | + x: toFinite(bounds?.x, 0), |
| 14 | + y: toFinite(bounds?.y, 0), |
| 15 | + z: toFinite(bounds?.z, 0), |
| 16 | + width: Math.max(0, toFinite(bounds?.width, 0)), |
| 17 | + height: Math.max(0, toFinite(bounds?.height, 0)), |
| 18 | + depth: Math.max(0, toFinite(bounds?.depth, 0)), |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +export function isAabbColliding3D(a, b) { |
| 23 | + const left = getAabb3D(a); |
| 24 | + const right = getAabb3D(b); |
| 25 | + |
| 26 | + return ( |
| 27 | + left.x < right.x + right.width && |
| 28 | + left.x + left.width > right.x && |
| 29 | + left.y < right.y + right.height && |
| 30 | + left.y + left.height > right.y && |
| 31 | + left.z < right.z + right.depth && |
| 32 | + left.z + left.depth > right.z |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +export function resolveAabbCollision3D(body, obstacle) { |
| 37 | + if (!body || !obstacle || body === obstacle) { |
| 38 | + return { |
| 39 | + collided: false, |
| 40 | + axis: null, |
| 41 | + overlap: 0, |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + const moving = getAabb3D(body); |
| 46 | + const fixed = getAabb3D(obstacle); |
| 47 | + |
| 48 | + if (!isAabbColliding3D(moving, fixed)) { |
| 49 | + return { |
| 50 | + collided: false, |
| 51 | + axis: null, |
| 52 | + overlap: 0, |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + const movingCenterX = moving.x + moving.width / 2; |
| 57 | + const movingCenterY = moving.y + moving.height / 2; |
| 58 | + const movingCenterZ = moving.z + moving.depth / 2; |
| 59 | + const fixedCenterX = fixed.x + fixed.width / 2; |
| 60 | + const fixedCenterY = fixed.y + fixed.height / 2; |
| 61 | + const fixedCenterZ = fixed.z + fixed.depth / 2; |
| 62 | + |
| 63 | + const deltaX = movingCenterX - fixedCenterX; |
| 64 | + const deltaY = movingCenterY - fixedCenterY; |
| 65 | + const deltaZ = movingCenterZ - fixedCenterZ; |
| 66 | + const overlapX = (moving.width + fixed.width) / 2 - Math.abs(deltaX); |
| 67 | + const overlapY = (moving.height + fixed.height) / 2 - Math.abs(deltaY); |
| 68 | + const overlapZ = (moving.depth + fixed.depth) / 2 - Math.abs(deltaZ); |
| 69 | + |
| 70 | + let axis = 'x'; |
| 71 | + let overlap = overlapX; |
| 72 | + |
| 73 | + if (overlapY < overlap) { |
| 74 | + axis = 'y'; |
| 75 | + overlap = overlapY; |
| 76 | + } |
| 77 | + |
| 78 | + if (overlapZ < overlap) { |
| 79 | + axis = 'z'; |
| 80 | + overlap = overlapZ; |
| 81 | + } |
| 82 | + |
| 83 | + if (axis === 'x') { |
| 84 | + body.x += deltaX < 0 ? -overlap : overlap; |
| 85 | + body.velocityX = 0; |
| 86 | + } else if (axis === 'y') { |
| 87 | + body.y += deltaY < 0 ? -overlap : overlap; |
| 88 | + body.velocityY = 0; |
| 89 | + } else { |
| 90 | + body.z += deltaZ < 0 ? -overlap : overlap; |
| 91 | + body.velocityZ = 0; |
| 92 | + } |
| 93 | + |
| 94 | + return { |
| 95 | + collided: true, |
| 96 | + axis, |
| 97 | + overlap, |
| 98 | + }; |
| 99 | +} |
| 100 | + |
| 101 | +export function resolveAabbCollisions3D(body, obstacles = []) { |
| 102 | + if (!Array.isArray(obstacles) || obstacles.length === 0) { |
| 103 | + return { |
| 104 | + collided: false, |
| 105 | + collisionCount: 0, |
| 106 | + axes: [], |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | + const axes = []; |
| 111 | + for (const obstacle of obstacles) { |
| 112 | + const result = resolveAabbCollision3D(body, obstacle); |
| 113 | + if (result.collided) { |
| 114 | + axes.push(result.axis); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return { |
| 119 | + collided: axes.length > 0, |
| 120 | + collisionCount: axes.length, |
| 121 | + axes, |
| 122 | + }; |
| 123 | +} |
0 commit comments