-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrajectory.js
More file actions
160 lines (152 loc) · 5.12 KB
/
trajectory.js
File metadata and controls
160 lines (152 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Deep clone a collection of body objects for simulation.
* Only clones necessary fields for gravity prediction.
*/
function cloneBodies(bodies) {
return bodies.map(b => ({
x: b.x,
y: b.y,
vx: b.vx,
vy: b.vy,
mass: b.mass,
width: b.width,
height: b.height,
color: b.color,
isRocket: b.isRocket
}));
}
/**
* Simulates all bodies together using a simple Euler integrator.
* Returns an array of arrays: positions[i][j] is body i's position at step j.
*/
function computeFutureTrajectoriesAll(bodies, G, steps, stepSize) {
let futureBodies = cloneBodies(bodies);
let tracks = futureBodies.map(b => [{x: b.x, y: b.y}]);
for (let s = 0; s < steps; s++) {
// Compute accelerations for all
let accels = futureBodies.map(() => ({ax: 0, ay: 0}));
for (let i = 0; i < futureBodies.length; i++) {
let b = futureBodies[i];
for (let j = 0; j < futureBodies.length; j++) {
if (i === j) continue;
let other = futureBodies[j];
let dx = other.x - b.x;
let dy = other.y - b.y;
let distSq = dx*dx + dy*dy;
let dist = Math.sqrt(distSq);
if (dist < 1) continue;
let a = (G * other.mass) / distSq;
accels[i].ax += a * dx / dist;
accels[i].ay += a * dy / dist;
}
}
// Update all positions and velocities
for (let i = 0; i < futureBodies.length; i++) {
let b = futureBodies[i], a = accels[i];
b.vx += a.ax * stepSize;
b.vy += a.ay * stepSize;
b.x += b.vx * stepSize;
b.y += b.vy * stepSize;
tracks[i].push({x: b.x, y: b.y});
}
}
return tracks;
}
/**
* Trajectory tracker for any body.
* Stores past trail and predicts future path.
*/
class Trajectory {
/**
* @param {Body} body - The target body to track.
* @param {number} maxPast - How many trail points to store.
* @param {number} futSteps - Predict how many steps ahead.
* @param {number} futStepSz - Prediction step size (dt).
*/
constructor(body, maxPast = 10000, futSteps = 15000, futStepSz = 0.2) {
this.bodyRef = body; // Target body reference (can be used for index, etc)
this.maxPastLength = maxPast;
this.points = []; // Past trail
this.futureSteps = futSteps;
this.futureStepSize = futStepSz;
this.futurePoints = []; // Predicted future positions (array of {x, y})
}
// Record current body position to trail
addPoint(x, y) {
this.points.push({ x, y });
if (this.points.length > this.maxPastLength) {
this.points.shift();
}
}
// Predict future using only static other bodies (single-body prediction)
computeFutureFixed(bodies, G) {
// Predicts this.bodyRef in the field of static others.
const body = this.bodyRef;
let fx = body.vx, fy = body.vy;
let px = body.x, py = body.y;
const m = body.mass;
this.futurePoints = [];
for (let i = 0; i < this.futureSteps; i++) {
let ax = 0, ay = 0;
for (const other of bodies) {
if (other === body) continue;
const dx = other.x - px;
const dy = other.y - py;
const distSq = dx * dx + dy * dy;
const dist = Math.sqrt(distSq);
if (dist < 1) continue; // avoid singularity
const a = (G * other.mass) / distSq;
ax += a * dx / dist;
ay += a * dy / dist;
}
fx += ax * this.futureStepSize;
fy += ay * this.futureStepSize;
px += fx * this.futureStepSize;
py += fy * this.futureStepSize;
this.futurePoints.push({ x: px, y: py });
}
}
/**
* Predicts future using n-body mutual simulation.
*
* @param {Array} bodies - All bodies in the universe (real ones; will be cloned).
* @param {number} G - Gravitational constant.
*/
computeFutureInteracting(bodies, G) {
// Find index of this body in the universe, to extract predicted track
let idx = bodies.findIndex(b => b === this.bodyRef);
if (idx < 0) return; // Not in bodies array
let tracks = computeFutureTrajectoriesAll(bodies, G, this.futureSteps, this.futureStepSize);
this.futurePoints = (tracks[idx] || []);
}
// Draw past path (white semi-transparent)
drawPast(ctx, offsetX = 0, offsetY = 0) {
if (this.points.length < 2) return;
ctx.save();
ctx.strokeStyle = 'rgba(255,255,255,0.35)';
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(this.points[0].x - offsetX, this.points[0].y - offsetY);
for (let i = 1; i < this.points.length; i++) {
ctx.lineTo(this.points[i].x - offsetX, this.points[i].y - offsetY);
}
ctx.stroke();
ctx.restore();
}
// Draw future path (dotted cyan)
drawFuture(ctx, offsetX = 0, offsetY = 0) {
if (this.futurePoints.length < 2) return;
ctx.save();
ctx.strokeStyle = 'rgba(0,255,255,0.7)';
ctx.lineWidth = 1;
ctx.setLineDash([5, 5]); // dotted line
ctx.beginPath();
ctx.moveTo(this.futurePoints[0].x - offsetX, this.futurePoints[0].y - offsetY);
for (let i = 1; i < this.futurePoints.length; i++) {
ctx.lineTo(this.futurePoints[i].x - offsetX, this.futurePoints[i].y - offsetY);
}
ctx.stroke();
ctx.setLineDash([]);
ctx.restore();
}
}