Skip to content

Commit 92eac46

Browse files
committed
refactor simulation boundary handling to use half the simulation area
1 parent 4409bca commit 92eac46

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/renderer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function animate(now: number) {
4242
if (deltaTime > 1 / fps) {
4343
if (runSimulation === true) {
4444
let solver = Solver.getInstance()
45-
solver.solve(spheres, simulationArea, e);
45+
solver.solve(spheres, simulationArea/2, e);
4646
}
4747
then = now;
4848
renderer.render(scene, camera);
@@ -55,7 +55,7 @@ export function addBody(mass: number = 10, radius: number = 0.2) {
5555
const material = new THREE.MeshStandardMaterial({ color: color, flatShading: true });
5656
const sphere = new THREE.Mesh(geometry, material);
5757

58-
sphere.position.set(Utils.getRandomNumber(-simulationArea, simulationArea), Utils.getRandomNumber(-simulationArea, simulationArea), Utils.getRandomNumber(-simulationArea, simulationArea));
58+
sphere.position.set(Utils.getRandomNumber(-simulationArea/2, simulationArea/2), Utils.getRandomNumber(-simulationArea/2, simulationArea/2), Utils.getRandomNumber(-simulationArea/2, simulationArea/2));
5959
sphere.userData = {
6060
velocity: new THREE.Vector3(Utils.getRandomNumber(-0.05, 0.05), Utils.getRandomNumber(-0.05, 0.05), Utils.getRandomNumber(-0.05, 0.05)),
6161
mass: mass,

src/solver.ts

+20-19
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default class Solver {
1414
return Solver.instance;
1515
}
1616

17-
public solve(spheres: THREE.Mesh[], simulationArea: number, e: number): void {
17+
public solve(spheres: THREE.Mesh[], bouns: number, e: number): void {
1818
spheres.forEach((sphere) => {
1919
let objectAcceleration = new THREE.Vector3(0, 0, 0);
2020
spheres.forEach((other) => {
@@ -36,28 +36,29 @@ export default class Solver {
3636

3737
sphere.position.add(sphere.userData.velocity.clone().multiplyScalar(this.deltaT));
3838

39-
if (sphere.position.x + sphere.userData.radius > simulationArea) {
40-
sphere.position.x = simulationArea - sphere.userData.radius;
41-
sphere.userData.velocity.x *= 0.8;
42-
} else if (sphere.position.x - sphere.userData.radius < -simulationArea) {
43-
sphere.position.x = -simulationArea + sphere.userData.radius;
44-
sphere.userData.velocity.x *= 0.8;
39+
let wallBounceE : number = 0.8;
40+
if (sphere.position.x + sphere.userData.radius > bouns) {
41+
sphere.position.x = bouns - sphere.userData.radius;
42+
sphere.userData.velocity.x *= wallBounceE;
43+
} else if (sphere.position.x - sphere.userData.radius < -bouns) {
44+
sphere.position.x = -bouns + sphere.userData.radius;
45+
sphere.userData.velocity.x *= wallBounceE;
4546
}
4647

47-
if (sphere.position.y + sphere.userData.radius > simulationArea) {
48-
sphere.position.y = simulationArea - sphere.userData.radius;
49-
sphere.userData.velocity.y *= 0.8;
50-
} else if (sphere.position.y - sphere.userData.radius < -simulationArea) {
51-
sphere.position.y = -simulationArea + sphere.userData.radius;
52-
sphere.userData.velocity.y *= 0.8;
48+
if (sphere.position.y + sphere.userData.radius > bouns) {
49+
sphere.position.y = bouns - sphere.userData.radius;
50+
sphere.userData.velocity.y *= wallBounceE;
51+
} else if (sphere.position.y - sphere.userData.radius < -bouns) {
52+
sphere.position.y = -bouns + sphere.userData.radius;
53+
sphere.userData.velocity.y *= wallBounceE;
5354
}
5455

55-
if (sphere.position.z + sphere.userData.radius > simulationArea) {
56-
sphere.position.z = simulationArea - sphere.userData.radius;
57-
sphere.userData.velocity.z *= 0.8;
58-
} else if (sphere.position.z - sphere.userData.radius < -simulationArea) {
59-
sphere.position.z = -simulationArea + sphere.userData.radius;
60-
sphere.userData.velocity.z *= 0.8;
56+
if (sphere.position.z + sphere.userData.radius > bouns) {
57+
sphere.position.z = bouns - sphere.userData.radius;
58+
sphere.userData.velocity.z *= wallBounceE;
59+
} else if (sphere.position.z - sphere.userData.radius < -bouns) {
60+
sphere.position.z = -bouns + sphere.userData.radius;
61+
sphere.userData.velocity.z *= wallBounceE;
6162
}
6263

6364
let maxVel = 2.5;

0 commit comments

Comments
 (0)