move and slide

This commit is contained in:
Schluffe
2026-03-30 22:54:41 +02:00
parent ba78c98a74
commit 25d0f950e6
3 changed files with 21 additions and 6 deletions
+12 -2
View File
@@ -7,6 +7,7 @@ import {
getDistance,
getDotProduct,
invScale,
scale,
setUnitNormal,
type Vector,
} from "#/engine/Vector.ts";
@@ -283,8 +284,17 @@ export function moveAndSlide(
): void {
const collisions = moveAndCollide(id, velocity, delta);
for (const collision of collisions) {
const time = collision.time;
if (time === -Infinity) {
continue; // TODO
}
const dot = getDotProduct(velocity, collision.normal);
velocity.x -= collision.normal.x * dot;
velocity.y -= collision.normal.y * dot;
const ts = Math.min(0, time - 0.001);
const tsx = velocity.x * ts;
const tsy = velocity.y * ts;
const rt = 1 - time;
velocity.x -= collision.normal.x * dot * rt + tsx;
velocity.y -= collision.normal.y * dot * rt + tsy;
}
}