WIP
This commit is contained in:
@@ -2,7 +2,7 @@ import type { Circle } from "#/engine/Circle.ts";
|
|||||||
import type { Polygon } from "#/engine/Polygon.ts";
|
import type { Polygon } from "#/engine/Polygon.ts";
|
||||||
import { createRect, fillRect, type Rect, strokeRect } 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, getDistance, type Vector } from "#/engine/Vector.ts";
|
||||||
import { viewport } from "#/engine/Viewport.ts";
|
import { viewport } from "#/engine/Viewport.ts";
|
||||||
|
|
||||||
export enum Shape {
|
export enum Shape {
|
||||||
@@ -42,8 +42,9 @@ export type Collision = {
|
|||||||
cid: string;
|
cid: string;
|
||||||
normal: Vector;
|
normal: Vector;
|
||||||
position: Vector;
|
position: Vector;
|
||||||
depth: 0;
|
depth: number;
|
||||||
angle: 0;
|
angle: number;
|
||||||
|
time: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function createCollider(
|
export function createCollider(
|
||||||
@@ -114,13 +115,13 @@ function sweptAABB(
|
|||||||
const thNear = Math.max(txNear, tyNear);
|
const thNear = Math.max(txNear, tyNear);
|
||||||
const thFar = Math.min(txFar, tyFar);
|
const thFar = Math.min(txFar, tyFar);
|
||||||
|
|
||||||
// thMax < 0, collision is behind a frame
|
// thFar < 0, collision is behind a frame
|
||||||
// thMin > 1, collision is ahead a frame
|
// thNear > 1, collision is ahead a frame
|
||||||
if (thFar < 0 || thNear > 1) {
|
if (thFar < 0 || thNear > 1) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return thNear;
|
return thNear; // -Infinity if velocity is 0
|
||||||
}
|
}
|
||||||
|
|
||||||
function collideWithCirle(
|
function collideWithCirle(
|
||||||
@@ -140,8 +141,10 @@ function collideWithRect(
|
|||||||
velocity: Vector,
|
velocity: Vector,
|
||||||
other: Collider,
|
other: Collider,
|
||||||
) {
|
) {
|
||||||
const th = sweptAABB(self, velocity, other);
|
collision.cid = cid;
|
||||||
collision.cid = th !== null ? cid : "";
|
const inflX = other.aabb.position.x - self.aabb.width;
|
||||||
|
const inflY = other.aabb.position.y - self.aabb.height;
|
||||||
|
collision.depth = getDistance(createVector(inflX, inflY), self.aabb.position);
|
||||||
}
|
}
|
||||||
|
|
||||||
function collideWithPolygon(
|
function collideWithPolygon(
|
||||||
@@ -202,6 +205,7 @@ function createCollision(): Collision {
|
|||||||
position: createVector(),
|
position: createVector(),
|
||||||
depth: 0,
|
depth: 0,
|
||||||
angle: 0,
|
angle: 0,
|
||||||
|
time: 0,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -228,7 +232,13 @@ export function moveAndCollide(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const other = collider.value;
|
const other = collider.value;
|
||||||
|
const tHit = sweptAABB(self, velocity, other);
|
||||||
|
if (tHit === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const collision = next(self.buffer);
|
const collision = next(self.buffer);
|
||||||
|
collision.time = tHit;
|
||||||
switch (self.shape) {
|
switch (self.shape) {
|
||||||
case CIRCLE: {
|
case CIRCLE: {
|
||||||
circleCollide[other.shape](collision, cid, self, velocity, other);
|
circleCollide[other.shape](collision, cid, self, velocity, other);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
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 { lerp } from "#/engine/lib/lerp";
|
||||||
import { pointer } from "#/engine/Pointer.ts";
|
import { pointer } from "#/engine/Pointer.ts";
|
||||||
import { createRect, fillRect, strokeRect } from "#/engine/Rect.ts";
|
import { createRect, fillRect, strokeRect } from "#/engine/Rect.ts";
|
||||||
import $ from "#/engine/Scene.ts";
|
import $ from "#/engine/Scene.ts";
|
||||||
@@ -36,9 +37,13 @@ const process = defineEntity("hero", {
|
|||||||
});
|
});
|
||||||
|
|
||||||
process((id, delta) => {
|
process((id, delta) => {
|
||||||
const position = $<Vector>("position").get(id)!.value;
|
const positions = $<Vector>("position");
|
||||||
|
const position = positions.get(id)!.value;
|
||||||
|
|
||||||
|
const colliders = $<Collider>("collider");
|
||||||
|
const collider = colliders.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;
|
||||||
@@ -53,11 +58,21 @@ process((id, delta) => {
|
|||||||
normalize(velocity);
|
normalize(velocity);
|
||||||
scale(velocity, speed);
|
scale(velocity, speed);
|
||||||
|
|
||||||
|
let cScale = 1;
|
||||||
const collisions = moveAndCollide(id, velocity, delta);
|
const collisions = moveAndCollide(id, velocity, delta);
|
||||||
// console.log(collisions);
|
for (const collision of collisions) {
|
||||||
|
// const _collider = colliders.get(collision.cid)!.value;
|
||||||
|
// const _position = positions.get(collision.cid)!.value;
|
||||||
|
const time = collision.time;
|
||||||
|
if (time === -Infinity) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
cScale = Math.min(0, time - 0.001);
|
||||||
|
console.log(cScale);
|
||||||
|
}
|
||||||
|
|
||||||
position.x += velocity.x * delta;
|
position.x += velocity.x * delta * cScale;
|
||||||
position.y += velocity.y * delta;
|
position.y += velocity.y * delta * cScale;
|
||||||
|
|
||||||
velocity.x = 0;
|
velocity.x = 0;
|
||||||
velocity.y = 0;
|
velocity.y = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user