Add moveAndSlide

This commit is contained in:
Schluffe
2026-03-25 21:59:04 +01:00
parent d08afa5d4a
commit ba78c98a74
2 changed files with 36 additions and 21 deletions
+6 -11
View File
@@ -14,10 +14,12 @@ import {
type Collision,
createCollider,
moveAndCollide,
moveAndSlide,
Shape,
} from "#/engine/system/physics.ts";
import {
createVector,
getDotProduct,
normalize,
scale,
type Vector,
@@ -55,24 +57,17 @@ process((id, delta) => {
velocity.x += keyboardInput.arrowRight ? 1 : 0;
velocity.y -= keyboardInput.arrowUp ? 1 : 0;
velocity.y += keyboardInput.arrowDown ? 1 : 0;
normalize(velocity);
scale(velocity, speed);
moveAndSlide(id, velocity, delta);
let cScale = 1;
const collisions = moveAndCollide(id, velocity, delta);
for (const collision of collisions) {
cScale = Math.min(0, collision.time - 0.001);
}
position.x += velocity.x * delta * cScale;
position.y += velocity.y * delta * cScale;
position.x += velocity.x * delta;
position.y += velocity.y * delta;
velocity.x = 0;
velocity.y = 0;
fillRect(viewport.ctx, collider.aabb, "orange");
strokeRect(viewport.ctx, collider.aabb, "red");
if (collisions.some((c: Collision) => c.cid.length > 0)) {
fillRect(viewport.ctx, collider.aabb, "red");
}
});