jumpstorm/engine/systems/Collision.ts

237 lines
7.3 KiB
TypeScript
Raw Normal View History

2023-08-25 18:48:17 -04:00
import { SystemNames, System } from '.';
2023-07-19 23:38:24 -04:00
import {
Mass,
BoundingBox,
ComponentNames,
Jump,
Velocity,
2023-08-25 18:48:17 -04:00
Forces
} from '../components';
import { Game } from '../Game';
import { Miscellaneous, PhysicsConstants } from '../config';
import { Entity } from '../entities';
import type { Coord2D, Dimension2D, Velocity2D } from '../interfaces';
import { BoxedEntry, RefreshingCollisionFinderBehavior } from '../structures';
2023-07-19 23:38:24 -04:00
export class Collision extends System {
private static readonly COLLIDABLE_COMPONENT_NAMES = [
2023-07-19 23:38:24 -04:00
ComponentNames.Collide,
2023-08-25 18:48:17 -04:00
ComponentNames.TopCollidable
2023-07-19 23:38:24 -04:00
];
private collisionFinder: RefreshingCollisionFinderBehavior;
2023-07-19 23:38:24 -04:00
constructor(refreshingCollisionFinder: RefreshingCollisionFinderBehavior) {
2023-07-19 23:38:24 -04:00
super(SystemNames.Collision);
this.collisionFinder = refreshingCollisionFinder;
2023-07-19 23:38:24 -04:00
}
2023-08-12 15:49:16 -04:00
public update(_dt: number, game: Game) {
this.collisionFinder.clear();
2023-07-19 23:38:24 -04:00
const entitiesToAddToCollisionFinder: Entity[] = [];
Collision.COLLIDABLE_COMPONENT_NAMES.map((componentName) =>
game.forEachEntityWithComponent(componentName, (entity) => {
if (!entity.hasComponent(ComponentNames.BoundingBox)) {
return;
}
entitiesToAddToCollisionFinder.push(entity);
2023-08-25 18:48:17 -04:00
})
2023-07-19 23:38:24 -04:00
);
this.insertEntitiesAndUpdateBounds(entitiesToAddToCollisionFinder);
this.findCollidingEntitiesAndCollide(entitiesToAddToCollisionFinder, game);
}
private insertEntitiesAndUpdateBounds(entities: Entity[]) {
const collisionFinderInsertions: BoxedEntry[] = [];
const topLeft: Coord2D = { x: Infinity, y: Infinity };
const bottomRight: Coord2D = { x: -Infinity, y: -Infinity };
entities.forEach((entity) => {
2023-07-19 23:38:24 -04:00
const boundingBox = entity.getComponent<BoundingBox>(
2023-08-25 18:48:17 -04:00
ComponentNames.BoundingBox
2023-07-19 23:38:24 -04:00
);
2023-08-12 15:49:16 -04:00
let dimension = { ...boundingBox.dimension };
if (boundingBox.rotation != 0) {
dimension = boundingBox.getOutscribedBoxDims();
}
const { center } = boundingBox;
const topLeftBoundingBox = boundingBox.getTopLeft();
const bottomRightBoundingBox = boundingBox.getBottomRight();
topLeft.x = Math.min(topLeftBoundingBox.x, topLeft.x);
topLeft.y = Math.min(topLeftBoundingBox.y, topLeft.y);
bottomRight.x = Math.max(bottomRightBoundingBox.x, bottomRight.x);
bottomRight.y = Math.max(bottomRightBoundingBox.y, bottomRight.y);
collisionFinderInsertions.push({
2023-08-13 18:47:58 -04:00
id: entity.id,
dimension,
2023-08-25 18:48:17 -04:00
center
2023-08-13 18:47:58 -04:00
});
2023-07-19 23:38:24 -04:00
});
// set bounds first
if (entities.length > 0) {
this.collisionFinder.setTopLeft(topLeft);
this.collisionFinder.setDimension({
width: bottomRight.x - topLeft.x,
2023-08-25 18:48:17 -04:00
height: bottomRight.y - topLeft.y
});
}
// then, begin insertions
collisionFinderInsertions.forEach((boxedEntry: BoxedEntry) =>
2023-08-25 18:48:17 -04:00
this.collisionFinder.insert(boxedEntry)
2023-07-19 23:38:24 -04:00
);
}
private findCollidingEntitiesAndCollide(entities: Entity[], game: Game) {
const collidingEntities = this.getCollidingEntities(entities, game);
2023-07-19 23:38:24 -04:00
collidingEntities.forEach(([entityAId, entityBId]) => {
const [entityA, entityB] = [entityAId, entityBId].map((id) =>
2023-08-25 18:48:17 -04:00
game.entities.get(id)
2023-07-19 23:38:24 -04:00
);
2023-08-12 15:49:16 -04:00
if (entityA && entityB) {
this.performCollision(entityA, entityB);
}
2023-07-19 23:38:24 -04:00
});
}
private performCollision(entityA: Entity, entityB: Entity) {
const [entityABoundingBox, entityBBoundingBox] = [entityA, entityB].map(
2023-08-25 18:48:17 -04:00
(entity) => entity.getComponent<BoundingBox>(ComponentNames.BoundingBox)
2023-07-19 23:38:24 -04:00
);
let velocity: Velocity2D = { dCartesian: { dx: 0, dy: 0 }, dTheta: 0 };
2023-07-19 23:38:24 -04:00
if (entityA.hasComponent(ComponentNames.Velocity)) {
velocity = entityA.getComponent<Velocity>(
2023-08-25 18:48:17 -04:00
ComponentNames.Velocity
).velocity;
2023-07-19 23:38:24 -04:00
}
if (
entityA.hasComponent(ComponentNames.Collide) &&
entityB.hasComponent(ComponentNames.TopCollidable) &&
entityABoundingBox.center.y <= entityBBoundingBox.center.y &&
velocity.dCartesian.dy >= 0 // don't apply "floor" logic when coming through the bottom
2023-07-19 23:38:24 -04:00
) {
if (entityBBoundingBox.rotation != 0) {
throw new Error(
2023-08-25 18:48:17 -04:00
`entity with id ${entityB.id} has TopCollidable component and a non-zero rotation. that is not (yet) supported.`
2023-07-19 23:38:24 -04:00
);
}
// remove previous velocity in the y axis
2023-08-12 15:49:16 -04:00
if (velocity) velocity.dCartesian.dy = 0;
2023-07-19 23:38:24 -04:00
// apply normal force
if (entityA.hasComponent(ComponentNames.Gravity)) {
const mass = entityA.getComponent<Mass>(ComponentNames.Mass).mass;
const F_n = -mass * PhysicsConstants.GRAVITY;
entityA.getComponent<Forces>(ComponentNames.Forces).forces.push({
2023-08-12 15:49:16 -04:00
fCartesian: { fy: F_n, fx: 0 },
2023-08-25 18:48:17 -04:00
torque: 0
2023-07-19 23:38:24 -04:00
});
}
// reset the entities' jump
if (entityA.hasComponent(ComponentNames.Jump)) {
entityA.getComponent<Jump>(ComponentNames.Jump).canJump = true;
}
entityABoundingBox.center.y =
entityBBoundingBox.center.y -
entityBBoundingBox.dimension.height / 2 -
this.getDyToPushOutOfFloor(entityABoundingBox, entityBBoundingBox);
}
}
private getCollidingEntities(
collidableEntities: Entity[],
2023-08-25 18:48:17 -04:00
game: Game
): [string, string][] {
const collidingEntityIds: [string, string][] = [];
2023-07-19 23:38:24 -04:00
for (const entity of collidableEntities) {
const boundingBox = entity.getComponent<BoundingBox>(
2023-08-25 18:48:17 -04:00
ComponentNames.BoundingBox
2023-07-19 23:38:24 -04:00
);
const neighborIds = this.collisionFinder.getNeighborIds({
id: entity.id,
dimension: boundingBox.dimension,
2023-08-25 18:48:17 -04:00
center: boundingBox.center
});
2023-08-12 15:49:16 -04:00
for (const neighborId of neighborIds) {
2023-08-12 15:49:16 -04:00
const neighbor = game.getEntity(neighborId);
if (!neighbor) return;
const neighborBoundingBox = neighbor.getComponent<BoundingBox>(
2023-08-25 18:48:17 -04:00
ComponentNames.BoundingBox
2023-08-12 15:49:16 -04:00
);
if (boundingBox.isCollidingWith(neighborBoundingBox)) {
collidingEntityIds.push([entity.id, neighborId]);
}
}
2023-07-19 23:38:24 -04:00
}
return collidingEntityIds;
}
// ramblings: https://excalidraw.com/#json=z-xD86Za4a3duZuV2Oky0,KaGe-5iHJu1Si8inEo4GLQ
2023-07-19 23:38:24 -04:00
private getDyToPushOutOfFloor(
entityBoundingBox: BoundingBox,
2023-08-25 18:48:17 -04:00
floorBoundingBox: BoundingBox
2023-07-19 23:38:24 -04:00
): number {
const {
dimension: { width, height },
2023-08-25 18:48:17 -04:00
center: { x }
2023-07-19 23:38:24 -04:00
} = entityBoundingBox;
2023-08-12 15:49:16 -04:00
const outScribedRectangle = entityBoundingBox.getOutscribedBoxDims();
2023-07-19 23:38:24 -04:00
2023-08-12 15:49:16 -04:00
let rads = entityBoundingBox.getRotationInPiOfUnitCircle();
let dx = (width * Math.cos(rads) - height * Math.sin(rads)) / 2;
if (rads >= Math.PI / 2) {
2023-07-19 23:38:24 -04:00
rads -= Math.PI / 2;
dx = (height * Math.cos(rads) - width * Math.sin(rads)) / 2;
}
2023-08-12 15:49:16 -04:00
const clippedX = x + dx; // x coordinate of the vertex below the surface (if existant)
let boundedCollisionX = 0; // bounded x on the surface from width
2023-07-19 23:38:24 -04:00
if (x >= floorBoundingBox.center.x) {
boundedCollisionX = Math.min(
floorBoundingBox.center.x + floorBoundingBox.dimension.width / 2,
2023-08-25 18:48:17 -04:00
clippedX
2023-07-19 23:38:24 -04:00
);
return (
2023-08-12 15:49:16 -04:00
outScribedRectangle.height / 2 -
2023-07-19 23:38:24 -04:00
Math.max((clippedX - boundedCollisionX) * Math.tan(rads), 0)
);
}
boundedCollisionX = Math.max(
floorBoundingBox.center.x - floorBoundingBox.dimension.width / 2,
2023-08-25 18:48:17 -04:00
clippedX
2023-07-19 23:38:24 -04:00
);
return (
2023-08-12 15:49:16 -04:00
outScribedRectangle.height / 2 -
Math.max((boundedCollisionX - clippedX) * Math.tan(Math.PI / 2 - rads), 0)
2023-07-19 23:38:24 -04:00
);
}
}