Skip to content

Commit

Permalink
fix: Fix issues with torpedos not hitting their targets.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderson1993 committed Oct 19, 2024
1 parent 31e1524 commit 9be6c21
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 30 deletions.
10 changes: 9 additions & 1 deletion server/src/classes/FlightDataModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export class FlightDataModel extends FSDataStore {
const colliderDesc = await generateColliderDesc(
path.join(thoriumPath, ship.assets.model),
ship.mass,
ship.length,
);
if (!colliderDesc) return;
this.ecs.colliderCache.set(ship.assets.model, colliderDesc);
Expand Down Expand Up @@ -166,7 +167,11 @@ export class FlightDataModel extends FSDataStore {
}
}

async function generateColliderDesc(filePath: string, mass: number) {
async function generateColliderDesc(
filePath: string,
mass: number,
size: number,
) {
try {
const ConvexHull = await import("three-stdlib").then(
(res) => res.ConvexHull,
Expand All @@ -176,6 +181,9 @@ async function generateColliderDesc(filePath: string, mass: number) {
if (!gltf) {
throw new Error("Failed to load gltf");
}
// This properly scales the collider to the size of the ship
// gltf.scene.children[0].scale.multiplyScalar(size / 1000);

hull.setFromObject(gltf.scene.children[0]);
const vertices = [];
for (const vertex of hull.vertices) {
Expand Down
2 changes: 1 addition & 1 deletion server/src/init/rapier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export function generateRigidBody(
const worldPosition = getWorldPosition(tempVector);
universeToWorld(tempVector, worldPosition);

const torpedoRadius = 0.002;
const torpedoRadius = 0.02;
const torpedoMass = entity.components.mass?.mass || 1500;

const bodyDesc = new RAPIER.RigidBodyDesc(
Expand Down
42 changes: 34 additions & 8 deletions server/src/spawners/ship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export async function spawnShip(
const entity = spawnShipSystem(
shipId,
systemPlugin,
params.playerShip,
system.overrides,
);
if (entity.components.isBattery) {
Expand All @@ -148,10 +149,15 @@ export async function spawnShip(
("shieldCount" in systemPlugin && systemPlugin.shieldCount) ||
1;
for (let i = 0; i < shieldCount; i++) {
const entity = spawnShipSystem(shipId, systemPlugin, {
...system.overrides,
direction: shieldDirections[i],
});
const entity = spawnShipSystem(
shipId,
systemPlugin,
params.playerShip,
{
...system.overrides,
direction: shieldDirections[i],
},
);
if (shieldCount > 1) {
entity.updateComponent("identity", {
name: `${capitalCase(shieldDirections[i])} ${
Expand All @@ -165,7 +171,12 @@ export async function spawnShip(
}
case "phasers": {
phaseCapacitorCount += 1;
const phaser = spawnShipSystem(shipId, systemPlugin, system.overrides);
const phaser = spawnShipSystem(
shipId,
systemPlugin,
params.playerShip,
system.overrides,
);

systemEntities.push(phaser);

Expand All @@ -175,7 +186,12 @@ export async function spawnShip(
system.overrides || {},
) as PhasersPlugin;

const capacitor = spawnShipSystem(shipId, { type: "battery" }, {});
const capacitor = spawnShipSystem(
shipId,
{ type: "battery" },
params.playerShip,
{},
);
capacitor.updateComponent("identity", {
name: `Phase Capacitor ${phaseCapacitorCount}`,
});
Expand All @@ -198,7 +214,12 @@ export async function spawnShip(
}
default: {
// TODO: Set up power from reactors and batteries
const entity = spawnShipSystem(shipId, systemPlugin, system.overrides);
const entity = spawnShipSystem(
shipId,
systemPlugin,
params.playerShip,
system.overrides,
);
systemEntities.push(entity);
break;
}
Expand Down Expand Up @@ -236,7 +257,12 @@ export async function spawnShip(
);
if (systemPlugin instanceof ReactorPlugin) {
Array.from({ length: systemPlugin.reactorCount }).forEach(() => {
const sys = spawnShipSystem(shipId, systemPlugin, system.overrides);
const sys = spawnShipSystem(
shipId,
systemPlugin,
params.playerShip,
system.overrides,
);
const maxOutput = reactorPower * systemPlugin.powerMultiplier;
sys.updateComponent("isReactor", {
maxOutput,
Expand Down
37 changes: 20 additions & 17 deletions server/src/spawners/shipSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { mergeDeep } from "../utils/mergeDeep";
export function spawnShipSystem(
shipId: number,
systemPlugin: Partial<BaseShipSystemPlugin>,
isPlayerShip?: boolean,
overrides: Record<string, any> = {},
) {
const entity = new Entity();
Expand Down Expand Up @@ -41,23 +42,25 @@ export function spawnShipSystem(
defaultPower,
maxSafePower,
} = systemPlugin;
if (flags.includes("heat"))
entity.addComponent("heat", {
powerToHeat: overrides.powerToHeat || powerToHeat,
heatDissipationRate:
overrides.heatDissipationRate || heatDissipationRate,
maxHeat: overrides.maxHeat || maxHeat,
maxSafeHeat: overrides.maxSafeHeat || maxSafeHeat,
nominalHeat: overrides.nominalHeat || nominalHeat,
heat: overrides.nominalHeat || nominalHeat,
});
if (flags.includes("power"))
entity.addComponent("power", {
requiredPower: overrides.requiredPower || requiredPower,
defaultPower: overrides.defaultPower || defaultPower,
maxSafePower: overrides.maxSafePower || maxSafePower,
});
if (flags.includes("efficiency")) entity.addComponent("efficiency");
if (isPlayerShip) {
if (flags.includes("heat"))
entity.addComponent("heat", {
powerToHeat: overrides.powerToHeat || powerToHeat,
heatDissipationRate:
overrides.heatDissipationRate || heatDissipationRate,
maxHeat: overrides.maxHeat || maxHeat,
maxSafeHeat: overrides.maxSafeHeat || maxSafeHeat,
nominalHeat: overrides.nominalHeat || nominalHeat,
heat: overrides.nominalHeat || nominalHeat,
});
if (flags.includes("power"))
entity.addComponent("power", {
requiredPower: overrides.requiredPower || requiredPower,
defaultPower: overrides.defaultPower || defaultPower,
maxSafePower: overrides.maxSafePower || maxSafePower,
});
if (flags.includes("efficiency")) entity.addComponent("efficiency");
}
}

return entity;
Expand Down
1 change: 1 addition & 0 deletions server/src/spawners/torpedo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export function spawnTorpedo(launcher: Entity) {
maxRange: flags?.torpedoCasing?.maxRange || 25000,
});
torpedoEntity.addComponent("mass", { mass: 1500 });
torpedoEntity.addComponent("physicsWorld");

return torpedoEntity;
}
Expand Down
7 changes: 4 additions & 3 deletions server/src/systems/PhysicsMovementSystem.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Euler, Object3D, Quaternion, Vector3 } from "three";
import { Box3, Euler, Object3D, Quaternion, Vector3 } from "three";
import { type Entity, System } from "../utils/ecs";
import { RAPIER, getWorldPosition } from "../init/rapier";
import { KM_TO_LM, M_TO_KM } from "@server/utils/unitTypes";
Expand All @@ -8,7 +8,7 @@ import {
universeToWorld,
worldToUniverse,
} from "@server/init/rapier";
import type { World } from "@thorium-sim/rapier3d-node";
import type { RigidBody, World } from "@thorium-sim/rapier3d-node";
import {
handleCollisionDamage,
handleTorpedoDamage,
Expand Down Expand Up @@ -331,7 +331,8 @@ export class PhysicsMovementSystem extends System {
world.step(eventQueue);

// Copy over the properties of each of the bodies to the entities
world.bodies.forEach((body: any) => {
world.bodies.forEach((body: RigidBody) => {
// @ts-expect-error - A very narrow type
const entity = this.ecs.getEntityById(body.userData?.entityId);
// No need to update entities that aren't in the collision step.
if (!entity || !this.collisionStepEntities.has(entity.id)) return;
Expand Down
1 change: 1 addition & 0 deletions server/src/systems/PhysicsWorldPositionSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class PhysicsWorldPositionSystem extends System {
entities.forEach((entities) => {
const iterator = entities.values();
const id = iterator.next().value;
if (!id) return;
this.ecs.getEntityById(id)?.updateComponent("physicsWorld", {
enabled: true,
});
Expand Down

0 comments on commit 9be6c21

Please sign in to comment.