Skip to content

Commit

Permalink
feat: cast and use non null operator less
Browse files Browse the repository at this point in the history
  • Loading branch information
TheHappyKoala committed Jan 14, 2024
1 parent df3332e commit 8116a7f
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 76 deletions.
28 changes: 14 additions & 14 deletions src/physics/integrators/euler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ class Euler {
for (let i = 0; i < massesLength; i++) {
let massI = masses[i];

positionVectors[i] = massI!.position;
positionVectors[i] = massI.position;

velocityVectors[i] = massI!.velocity;
velocityVectors[i] = massI.velocity;
}

return { positionVectors, velocityVectors };
Expand All @@ -58,13 +58,13 @@ class Euler {
const massesLength = p.length;

for (let i = 0; i < massesLength; i++) {
const positionVectorI = p[i]!;
const velocityVectorI = v[i]!;
const positionVectorI = p[i];
const velocityVectorI = v[i];

const mass = this.masses[i];

mass!.position = positionVectorI;
mass!.velocity = velocityVectorI;
mass.position = positionVectorI;
mass.velocity = velocityVectorI;
}
}

Expand All @@ -73,11 +73,11 @@ class Euler {
const massesLength = velocityVectors.length;

for (let i = 0; i < massesLength; i++) {
let velocityVectorI = velocityVectors[i]!;
let velocityVectorI = velocityVectors[i];
let mass = this.masses[i];

positionVectors[i] = this.positionVector
.set(mass!.position)
.set(mass.position)
.addScaledVector(dt, velocityVectorI)
.toObject();
}
Expand All @@ -92,11 +92,11 @@ class Euler {
for (let i = 0; i < massesLength; i++) {
this.accelerationVector.set({ x: 0, y: 0, z: 0 });

let positionVectorI = positionVectors[i]!;
let positionVectorI = positionVectors[i];

for (let j = 0; j < massesLength; j++) {
if (i !== j && this.masses[j]!.m > 0) {
let positionVectorJ = positionVectors[j]!;
if (i !== j && this.masses[j].m > 0) {
let positionVectorJ = positionVectors[j];

let distanceParams = this.getDistanceParams(
positionVectorI,
Expand All @@ -105,7 +105,7 @@ class Euler {
let distance = Math.sqrt(distanceParams.dSquared);

let fact =
(this.g * this.masses[j]!.m) / (distanceParams.dSquared * distance);
(this.g * this.masses[j].m) / (distanceParams.dSquared * distance);

this.accelerationVector.addScaledVector(fact, {
x: distanceParams.dx,
Expand All @@ -126,11 +126,11 @@ class Euler {
const massesLength = accelerationVectors.length;

for (let i = 0; i < massesLength; i++) {
let accelerationVectorI = accelerationVectors[i]!;
let accelerationVectorI = accelerationVectors[i];
let mass = this.masses[i];

velocityVectors[i] = this.velocityVector
.set(mass!.velocity)
.set(mass.velocity)
.addScaledVector(dt, accelerationVectorI)
.toObject();
}
Expand Down
48 changes: 24 additions & 24 deletions src/physics/integrators/rk4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,38 +48,38 @@ class RK4 extends Euler {
for (let i = 0; i < massesLength; i++) {
accelerationVectors[i] = {
x:
accelerationVectors1[i]!.x / 6 +
accelerationVectors2[i]!.x / 3 +
accelerationVectors3[i]!.x / 3 +
accelerationVectors4[i]!.x / 6,
accelerationVectors1[i].x / 6 +
accelerationVectors2[i].x / 3 +
accelerationVectors3[i].x / 3 +
accelerationVectors4[i].x / 6,
y:
accelerationVectors1[i]!.y / 6 +
accelerationVectors2[i]!.y / 3 +
accelerationVectors3[i]!.y / 3 +
accelerationVectors4[i]!.y / 6,
accelerationVectors1[i].y / 6 +
accelerationVectors2[i].y / 3 +
accelerationVectors3[i].y / 3 +
accelerationVectors4[i].y / 6,
z:
accelerationVectors1[i]!.z / 6 +
accelerationVectors2[i]!.z / 3 +
accelerationVectors3[i]!.z / 3 +
accelerationVectors4[i]!.z / 6,
accelerationVectors1[i].z / 6 +
accelerationVectors2[i].z / 3 +
accelerationVectors3[i].z / 3 +
accelerationVectors4[i].z / 6,
};

velocityVectors[i] = {
x:
stateVectors.velocityVectors[i]!.x / 6 +
velocityVectors2[i]!.x / 3 +
velocityVectors3[i]!.x / 3 +
velocityVectors4[i]!.x / 6,
stateVectors.velocityVectors[i].x / 6 +
velocityVectors2[i].x / 3 +
velocityVectors3[i].x / 3 +
velocityVectors4[i].x / 6,
y:
stateVectors.velocityVectors[i]!.y / 6 +
velocityVectors2[i]!.y / 3 +
velocityVectors3[i]!.y / 3 +
velocityVectors4[i]!.y / 6,
stateVectors.velocityVectors[i].y / 6 +
velocityVectors2[i].y / 3 +
velocityVectors3[i].y / 3 +
velocityVectors4[i].y / 6,
z:
stateVectors.velocityVectors[i]!.z / 6 +
velocityVectors2[i]!.z / 3 +
velocityVectors3[i]!.z / 3 +
velocityVectors4[i]!.z / 6,
stateVectors.velocityVectors[i].z / 6 +
velocityVectors2[i].z / 3 +
velocityVectors3[i].z / 3 +
velocityVectors4[i].z / 6,
};
}

Expand Down
4 changes: 4 additions & 0 deletions src/scene/manifestations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ class ManifestationManager {

this.scene.remove(massToBeDeleted);

this.manifestations[i]?.dispose();
if (this.manifestations[i]!.object3D) {
this.manifestations[i]!.object3D = undefined;
}
this.manifestations.splice(i, 1);
}
}
Expand Down
106 changes: 81 additions & 25 deletions src/scene/manifestations/manifestation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class Manifestation {
public mass: ScenarioMassType;
protected textureLoader: THREE.TextureLoader;
protected trailVertices: number;
public object3D: THREE.Object3D;
public orbit: EllipseCurve | null;
public object3D: THREE.Object3D | undefined;
public orbit: EllipseCurve | undefined;

constructor(mass: ScenarioMassType, textureLoader: THREE.TextureLoader) {
this.mass = mass;
Expand All @@ -19,7 +19,7 @@ class Manifestation {
this.object3D = new THREE.Object3D();
this.object3D.name = this.mass.name;

this.orbit = null;
this.orbit = undefined;

this.trailVertices = 3000;
}
Expand All @@ -40,7 +40,9 @@ class Manifestation {
const sphere = new THREE.Mesh(geometry, material);
sphere.name = "sphere";

this.object3D.add(sphere);
if (this.object3D) {
this.object3D.add(sphere);
}

return this;
}
Expand All @@ -59,17 +61,20 @@ class Manifestation {
"green",
);

this.object3D.add(this.orbit.object3D);
this.object3D!.add(this.orbit.object3D!);

return this;
}

public removeOrbit() {
this.object3D.remove(this.orbit!.object3D);
if (this.orbit) {
this.orbit.dispose();

this.orbit!.dispose();
this.object3D!.remove(this.orbit.object3D!);
this.orbit.object3D = undefined;

this.orbit = null;
this.orbit = undefined;
}
}

public updateOrbit(
Expand All @@ -87,7 +92,7 @@ class Manifestation {

const ellipse = getEllipse(a, e);

const orbit = this.object3D.getObjectByName("ellipse") as THREE.Object3D;
const orbit = this.object3D!.getObjectByName("ellipse") as THREE.Object3D;

orbit.position.z = (rotatingReferenceFrame.z - primaryPosition.z) * scale;

Expand All @@ -109,7 +114,7 @@ class Manifestation {

const points = [];

const spherePosition = this.object3D.getObjectByName("sphere")!.position;
const spherePosition = this.object3D!.getObjectByName("sphere")!.position;

for (let i = 0; i < verticesLength; i++) {
points.push(spherePosition);
Expand All @@ -127,32 +132,45 @@ class Manifestation {

mesh.frustumCulled = false;

this.object3D.add(mesh);
this.object3D!.add(mesh);
}

public removeTrail() {
const trail = this.object3D.getObjectByName("trail");
let trail = this.object3D!.getObjectByName("trail") as
| THREE.Line
| undefined;

if (trail) {
// @ts-ignore
trail.geometry.dispose();
let geometry = trail.geometry as
| THREE.BufferGeometry<THREE.NormalBufferAttributes>
| undefined;

if (geometry) {
geometry.dispose();
geometry = undefined;
}

let material = trail.material as THREE.Material | undefined;

if (material) {
material.dispose();
material = undefined;
}

// @ts-ignore
trail.material.dispose();
this.object3D!.remove(trail);

this.object3D.remove(trail);
trail = undefined;
}
}

public drawTrail() {
const trail = this.object3D.getObjectByName("trail");
const trail = this.object3D!.getObjectByName("trail") as THREE.Line;

// @ts-ignore
const geometry = trail.geometry;
const positions = geometry.attributes.position.array;
const positions = geometry.attributes["position"]!.array;

for (let i = positions.length - 1; i > 2; i--) {
positions[i] = positions[i - 3];
positions[i] = positions[i - 3] as number;
}

const { x, y, z } = this.mass.rotatedPosition as VectorType;
Expand All @@ -165,15 +183,53 @@ class Manifestation {
}

public setPosition() {
const object3D = this.object3D;

const { x, y, z } = this.mass.rotatedPosition as VectorType;

const sphere = object3D.getObjectByName("sphere");
sphere?.position.set(x, y, z);
const sphere = this.object3D!.getObjectByName("sphere") as THREE.Mesh;
sphere.position.set(x, y, z);

return this;
}

public dispose() {
this.removeOrbit();

this.removeTrail();

let sphereMesh = this.object3D!.getObjectByName("sphere") as
| THREE.Mesh
| undefined;

if (sphereMesh) {
let sphereMaterial = sphereMesh.material as THREE.Material | undefined;

if (sphereMaterial) {
//@ts-ignore
let sphereMaterialMap = sphereMesh.material.map;

if (sphereMaterialMap) {
sphereMaterialMap.dispose();
sphereMaterialMap = undefined;
}

sphereMaterial.dispose();
sphereMaterial = undefined;
}

let sphereGeometry = sphereMesh.geometry as
| THREE.BufferGeometry<THREE.NormalBufferAttributes>
| undefined;

if (sphereGeometry) {
sphereGeometry.dispose();
sphereGeometry = undefined;
}

this.object3D!.remove(sphereMesh);

sphereMesh = undefined;
}
}
}

export default Manifestation;
Loading

0 comments on commit 8116a7f

Please sign in to comment.