1st working swept

This commit is contained in:
Schluffe
2026-03-22 17:49:12 +01:00
parent f5fc9224b9
commit f65951830d
5 changed files with 101 additions and 33 deletions
+6 -11
View File
@@ -38,10 +38,10 @@ export function strokeRect(
ctx.globalAlpha = alpha; ctx.globalAlpha = alpha;
ctx.lineWidth = lineWidth; ctx.lineWidth = lineWidth;
ctx.strokeRect( ctx.strokeRect(
rect.position.x - 0.5, rect.position.x + 0.5,
rect.position.y - 0.5, rect.position.y + 0.5,
rect.width, rect.width - 1,
rect.height, rect.height - 1,
); );
ctx.restore(); ctx.restore();
} }
@@ -53,13 +53,8 @@ export function fillRect(
alpha: number = 1.0, alpha: number = 1.0,
): void { ): void {
ctx.save(); ctx.save();
ctx.strokeStyle = color; ctx.fillStyle = color;
ctx.globalAlpha = alpha; ctx.globalAlpha = alpha;
ctx.fillRect( ctx.fillRect(rect.position.x, rect.position.y, rect.width, rect.height);
rect.position.x - 0.5,
rect.position.y - 0.5,
rect.width,
rect.height,
);
ctx.restore(); ctx.restore();
} }
+75 -16
View File
@@ -1,8 +1,9 @@
import type { Circle } from "#/engine/Circle.ts"; import type { Circle } from "#/engine/Circle.ts";
import type { Polygon } from "#/engine/Polygon.ts"; import type { Polygon } from "#/engine/Polygon.ts";
import { createRect, type Rect } from "#/engine/Rect.ts"; import { createRect, fillRect, type Rect, strokeRect } from "#/engine/Rect.ts";
import $ from "#/engine/Scene.ts"; import $ from "#/engine/Scene.ts";
import { createVector, type Vector } from "#/engine/Vector.ts"; import { createVector, type Vector } from "#/engine/Vector.ts";
import { viewport } from "#/engine/Viewport.ts";
export enum Shape { export enum Shape {
Cirle, Cirle,
@@ -22,7 +23,13 @@ type CollisionBuffer = {
slice: Array<Collision>; slice: Array<Collision>;
}; };
type Collide = (collision: Collision, cid: string) => void; type Collide = (
collision: Collision,
cid: string,
self: Collider,
velocity: Vector,
other: Collider,
) => void;
export type Collider = { export type Collider = {
shape: Shape; shape: Shape;
@@ -73,25 +80,77 @@ export function createCollider(
}; };
} }
const infAABB = createRect(createVector(), 0, 0); const inflAABB = createRect(createVector(), 0, 0);
function sweptAABB(self: Collider, other: Collider, velocity: Vector): void { function sweptAABB(
infAABB.position.x = other.aabb.position.x - self.aabb.width / 2; self: Collider,
infAABB.position.y = other.aabb.position.y - self.aabb.height / 2; velocity: Vector,
infAABB.width = other.aabb.width - self.aabb.width; other: Collider,
infAABB.height = other.aabb.height - self.aabb.height; ): null | number {
inflAABB.position.x = other.aabb.position.x - self.aabb.width;
inflAABB.position.y = other.aabb.position.y - self.aabb.height;
inflAABB.width = other.aabb.width + self.aabb.width;
inflAABB.height = other.aabb.height + self.aabb.height;
// let txNear = (inflAABB.position.x - self.aabb.position.x) / velocity.x;
let txFar =
(inflAABB.position.x + inflAABB.width - self.aabb.position.x) / velocity.x;
let tyNear = (inflAABB.position.y - self.aabb.position.y) / velocity.y;
let tyFar =
(inflAABB.position.y + inflAABB.height - self.aabb.position.y) / velocity.y;
if (txNear > txFar) {
[txNear, txFar] = [txFar, txNear];
}
if (tyNear > tyFar) {
[tyNear, tyFar] = [tyFar, tyNear];
}
// no collision at all
if (txNear > tyFar || tyNear > txFar) {
return null;
}
const thNear = Math.max(txNear, tyNear);
const thFar = Math.min(txFar, tyFar);
// thMax < 0, collision is behind a frame
// thMin > 1, collision is ahead a frame
if (thFar < 0 || thNear > 1) {
return null;
}
return thNear;
} }
function collideWithCirle(collision: Collision, cid: string) { function collideWithCirle(
collision: Collision,
cid: string,
self: Collider,
velocity: Vector,
other: Collider,
) {
collision.cid = cid; collision.cid = cid;
} }
function collideWithRect(collision: Collision, cid: string) { function collideWithRect(
collision.cid = cid; collision: Collision,
cid: string,
self: Collider,
velocity: Vector,
other: Collider,
) {
const th = sweptAABB(self, velocity, other);
collision.cid = th !== null ? cid : "";
} }
function collideWithPolygon(collision: Collision, cid: string) { function collideWithPolygon(
collision: Collision,
cid: string,
self: Collider,
velocity: Vector,
other: Collider,
) {
collision.cid = cid; collision.cid = cid;
} }
@@ -172,15 +231,15 @@ export function moveAndCollide(
const collision = next(self.buffer); const collision = next(self.buffer);
switch (self.shape) { switch (self.shape) {
case CIRCLE: { case CIRCLE: {
circleCollide[other.shape](collision, cid); circleCollide[other.shape](collision, cid, self, velocity, other);
break; break;
} }
case RECT: { case RECT: {
rectCollide[other.shape](collision, cid); rectCollide[other.shape](collision, cid, self, velocity, other);
break; break;
} }
case POLYGON: { case POLYGON: {
polygonCollide[other.shape](collision, cid); polygonCollide[other.shape](collision, cid, self, velocity, other);
break; break;
} }
} }
+13 -3
View File
@@ -1,7 +1,7 @@
import { defineEntity } from "#/engine/Entity.ts"; import { defineEntity } from "#/engine/Entity.ts";
import { keyboardInput } from "#/engine/Keyboard.ts"; import { keyboardInput } from "#/engine/Keyboard.ts";
import { pointer } from "#/engine/Pointer.ts"; import { pointer } from "#/engine/Pointer.ts";
import { createRect } from "#/engine/Rect.ts"; import { createRect, fillRect, strokeRect } from "#/engine/Rect.ts";
import $ from "#/engine/Scene.ts"; import $ from "#/engine/Scene.ts";
import { import {
createSprite, createSprite,
@@ -9,6 +9,8 @@ import {
defineSpriteSheet, defineSpriteSheet,
} from "#/engine/Sprite.ts"; } from "#/engine/Sprite.ts";
import { import {
type Collider,
type Collision,
createCollider, createCollider,
moveAndCollide, moveAndCollide,
Shape, Shape,
@@ -19,6 +21,7 @@ import {
scale, scale,
type Vector, type Vector,
} from "#/engine/Vector.ts"; } from "#/engine/Vector.ts";
import { viewport } from "#/engine/Viewport.ts";
import charIdle from "#/game/assets/char-idle.png"; import charIdle from "#/game/assets/char-idle.png";
const speed = 0.05; const speed = 0.05;
@@ -35,6 +38,7 @@ const process = defineEntity("hero", {
process((id, delta) => { process((id, delta) => {
const position = $<Vector>("position").get(id)!.value; const position = $<Vector>("position").get(id)!.value;
const velocity = $<Vector>("velocity").get(id)!.value; const velocity = $<Vector>("velocity").get(id)!.value;
const collider = $<Collider>("collider").get(id)!.value;
if (pointer.isDown) { if (pointer.isDown) {
position.x = pointer.position.x; position.x = pointer.position.x;
@@ -49,12 +53,18 @@ process((id, delta) => {
normalize(velocity); normalize(velocity);
scale(velocity, speed); scale(velocity, speed);
const collisions = moveAndCollide(id, velocity, delta);
// console.log(collisions);
position.x += velocity.x * delta; position.x += velocity.x * delta;
position.y += velocity.y * delta; position.y += velocity.y * delta;
velocity.x = 0; velocity.x = 0;
velocity.y = 0; velocity.y = 0;
const collisions = moveAndCollide(id, velocity, delta); fillRect(viewport.ctx, collider.aabb, "orange");
// console.log(collisions); strokeRect(viewport.ctx, collider.aabb, "red");
if (collisions.some((c: Collision) => c.cid.length > 0)) {
fillRect(viewport.ctx, collider.aabb, "red");
}
}); });
+5 -1
View File
@@ -1,5 +1,5 @@
import { defineEntity } from "#/engine/Entity.ts"; import { defineEntity } from "#/engine/Entity.ts";
import { createRect } from "#/engine/Rect.ts"; import { createRect, fillRect, strokeRect } from "#/engine/Rect.ts";
import $ from "#/engine/Scene.ts"; import $ from "#/engine/Scene.ts";
import { import {
createSprite, createSprite,
@@ -12,6 +12,7 @@ import {
Shape, Shape,
} from "#/engine/system/physics.ts"; } from "#/engine/system/physics.ts";
import { createVector, type Vector } from "#/engine/Vector.ts"; import { createVector, type Vector } from "#/engine/Vector.ts";
import { viewport } from "#/engine/Viewport";
import portal from "#/game/assets/portal.png"; import portal from "#/game/assets/portal.png";
const portalSrc = await defineSpriteSheet(portal, 3, 1); const portalSrc = await defineSpriteSheet(portal, 3, 1);
@@ -30,4 +31,7 @@ process((id, delta) => {
cPos.x = position.x; cPos.x = position.x;
cPos.y = position.y; cPos.y = position.y;
fillRect(viewport.ctx, collider.aabb, "orange");
strokeRect(viewport.ctx, collider.aabb, "red");
}); });
+2 -2
View File
@@ -16,8 +16,8 @@
"name": "portal", "name": "portal",
"init": { "init": {
"position": { "position": {
"x": 16, "x": 24,
"y": 16 "y": 24
} }
} }
}, },