summaryrefslogtreecommitdiff
path: root/client/lib/systems/Physics.ts
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-07-20 20:47:32 -0700
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-07-20 20:47:32 -0700
commit72c6c7de12e9833f52bf2d0718d70f044f8ab57e (patch)
tree152f5f31d59011bb8c617bfbcfc44cc8f47ecad5 /client/lib/systems/Physics.ts
parent0fd9fb097552686f2257c1aa689d797e80057bd1 (diff)
downloadjumpstorm-72c6c7de12e9833f52bf2d0718d70f044f8ab57e.tar.gz
jumpstorm-72c6c7de12e9833f52bf2d0718d70f044f8ab57e.zip
a bit of refactoring; importing engine into bun for server
Diffstat (limited to 'client/lib/systems/Physics.ts')
-rw-r--r--client/lib/systems/Physics.ts94
1 files changed, 0 insertions, 94 deletions
diff --git a/client/lib/systems/Physics.ts b/client/lib/systems/Physics.ts
deleted file mode 100644
index 319ae29..0000000
--- a/client/lib/systems/Physics.ts
+++ /dev/null
@@ -1,94 +0,0 @@
-import { System, SystemNames } from ".";
-import {
- Acceleration,
- BoundingBox,
- ComponentNames,
- Forces,
- Gravity,
- Velocity,
- Mass,
- Jump,
-} from "../components";
-import { PhysicsConstants } from "../config";
-import type { Entity } from "../entities";
-import type { Force2D } from "../interfaces";
-
-export class Physics extends System {
- constructor() {
- super(SystemNames.Physics);
- }
-
- public update(
- dt: number,
- entityMap: Map<number, Entity>,
- componentEntities: Map<string, Set<number>>
- ): void {
- componentEntities.get(ComponentNames.Forces)?.forEach((entityId) => {
- const entity = entityMap.get(entityId);
-
- const mass = entity.getComponent<Mass>(ComponentNames.Mass).mass;
- const forces = entity.getComponent<Forces>(ComponentNames.Forces).forces;
- const velocity = entity.getComponent<Velocity>(ComponentNames.Velocity);
- const inertia = entity.getComponent<Moment>(
- ComponentNames.Moment
- ).inertia;
-
- // F_g = mg, applied only until terminal velocity is reached
- if (entity.hasComponent(ComponentNames.Gravity)) {
- const gravity = entity.getComponent<Gravity>(ComponentNames.Gravity);
- if (velocity.dCartesian.dy <= gravity.terminalVelocity) {
- forces.push({
- fCartesian: {
- fy: mass * PhysicsConstants.GRAVITY,
- },
- });
- }
- }
-
- // ma = Σ(F), Iα = Σ(T)
- const sumOfForces = forces.reduce(
- (accum: Force2D, { fCartesian, torque }: Force2D) => ({
- fCartesian: {
- fx: accum.fCartesian.fx + (fCartesian?.fx ?? 0),
- fy: accum.fCartesian.fy + (fCartesian?.fy ?? 0),
- },
- torque: accum.torque + (torque ?? 0),
- }),
- { fCartesian: { fx: 0, fy: 0 }, torque: 0 }
- );
-
- // integrate accelerations
- const [ddy, ddx] = [
- sumOfForces.fCartesian.fy,
- sumOfForces.fCartesian.fx,
- ].map((x) => x / mass);
- velocity.dCartesian.dx += ddx * dt;
- velocity.dCartesian.dy += ddy * dt;
- velocity.dTheta += (sumOfForces.torque * dt) / inertia;
- // clear the forces
- entity.getComponent<Forces>(ComponentNames.Forces).forces = [];
-
- // maybe we fell off the floor
- if (ddy > 0 && entity.hasComponent(ComponentNames.Jump)) {
- entity.getComponent<Jump>(ComponentNames.Jump).canJump = false;
- }
- });
-
- componentEntities.get(ComponentNames.Velocity)?.forEach((entityId) => {
- const entity = entityMap.get(entityId);
- const velocity = entity.getComponent<Velocity>(ComponentNames.Velocity);
- const boundingBox = entity.getComponent<BoundingBox>(
- ComponentNames.BoundingBox
- );
-
- // integrate velocity
- boundingBox.center.x += velocity.dCartesian.dx * dt;
- boundingBox.center.y += velocity.dCartesian.dy * dt;
- boundingBox.rotation += velocity.dTheta * dt;
- boundingBox.rotation =
- (boundingBox.rotation < 0
- ? 360 + boundingBox.rotation
- : boundingBox.rotation) % 360;
- });
- }
-}