File size: 13,268 Bytes
5a5134c
 
626eae2
 
 
5a5134c
626eae2
 
 
5a5134c
626eae2
 
 
 
 
 
 
 
 
 
 
 
5a5134c
626eae2
 
5a5134c
626eae2
 
 
 
5a5134c
626eae2
 
5a5134c
626eae2
5a5134c
 
 
 
626eae2
 
 
 
 
 
5a5134c
626eae2
5a5134c
626eae2
 
5a5134c
626eae2
5a5134c
626eae2
5a5134c
626eae2
5a5134c
 
 
 
 
 
626eae2
 
5a5134c
 
 
626eae2
 
 
5a5134c
 
 
626eae2
 
 
5a5134c
626eae2
5a5134c
 
626eae2
 
5a5134c
 
626eae2
 
 
 
5a5134c
 
 
626eae2
 
5a5134c
626eae2
 
5a5134c
626eae2
5a5134c
 
 
 
 
 
 
626eae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a5134c
 
 
 
 
626eae2
 
5a5134c
626eae2
5a5134c
 
626eae2
 
5a5134c
 
626eae2
 
5a5134c
 
 
 
 
 
 
 
 
626eae2
 
 
 
 
 
 
 
 
 
 
 
 
 
5a5134c
626eae2
 
5a5134c
626eae2
 
5a5134c
 
626eae2
5a5134c
626eae2
5a5134c
 
626eae2
5a5134c
626eae2
 
 
5a5134c
 
 
 
 
626eae2
 
5a5134c
626eae2
 
 
 
5a5134c
 
 
626eae2
 
 
 
 
 
 
 
5a5134c
 
 
 
 
 
 
 
 
626eae2
 
 
 
 
 
 
 
5a5134c
626eae2
 
5a5134c
626eae2
5a5134c
 
 
626eae2
 
 
5a5134c
 
626eae2
5a5134c
626eae2
 
 
5a5134c
 
 
 
 
 
 
 
 
 
 
 
626eae2
 
5a5134c
626eae2
5a5134c
 
626eae2
5a5134c
626eae2
 
 
 
 
5a5134c
 
626eae2
5a5134c
 
626eae2
5a5134c
 
626eae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a5134c
 
 
 
626eae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a5134c
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333

// Vector Gyroscope Physics Component
class VectorPhysicsEngine {
    constructor() {
        this.vectors = [];
        this.gyroBodies = [];
        this.pulsars = [];
        this.isRunning = false;
        this.animationId = null;
        this.netTorque = 0;
    }

    initVectorField(canvasId) {
        const canvas = document.getElementById(canvasId);
        if (!canvas) return;
        
        const ctx = canvas.getContext('2d');
        canvas.width = canvas.offsetWidth;
        canvas.height = canvas.offsetHeight;
        
        const centerX = canvas.width / 2;
        const centerY = canvas.height / 2;
        const radius = Math.min(centerX, centerY) * 0.6;
        const numVectors = 360;
        
        // Create gyroscopic vectors
        this.vectors = [];
        for (let i = 0; i < numVectors; i++) {
            const angle = (i * Math.PI * 2) / numVectors;
            const isPositive = i < 180;
            const length = radius * (0.7 + Math.random() * 0.3);
            this.vectors.push({
                angle,
                length,
                positive: isPositive,
                x: centerX + Math.cos(angle) * length,
                y: centerY + Math.sin(angle) * length,
                torque: isPositive ? 0.5 : -0.5,
                spin: 0
            });
        }

        const draw = () => {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            
            // Draw center gyroscope pivot
            ctx.beginPath();
            ctx.arc(centerX, centerY, 12, 0, Math.PI * 2);
            ctx.fillStyle = '#f59326';
            ctx.fill();
            ctx.font = 'bold 16px Inter';
            ctx.fillStyle = '#fff';
            ctx.fillText('G', centerX - 6, centerY + 6);
            
            // Draw vectors with gyroscopic rotation
            this.vectors.forEach(vector => {
                const spinOffset = vector.spin * 0.1;
                const currentAngle = vector.angle + spinOffset;
                const endX = centerX + Math.cos(currentAngle) * vector.length;
                const endY = centerY + Math.sin(currentAngle) * vector.length;
                
                // Draw vector line
                ctx.beginPath();
                ctx.moveTo(centerX, centerY);
                ctx.lineTo(endX, endY);
                ctx.strokeStyle = vector.positive ? 'rgba(14, 165, 233, 0.8)' : 'rgba(168, 85, 247, 0.8)';
                ctx.lineWidth = vector.positive ? 2.5 : 1.5;
                ctx.stroke();
                
                // Draw arrowhead
                const headLength = 12;
                const dx = endX - centerX;
                const dy = endY - centerY;
                const angle = Math.atan2(dy, dx);
                
                ctx.beginPath();
                ctx.moveTo(endX, endY);
                ctx.lineTo(
                    endX - headLength * Math.cos(angle - Math.PI / 6),
                    endY - headLength * Math.sin(angle - Math.PI / 6)
                );
                ctx.lineTo(
                    endX - headLength * Math.cos(angle + Math.PI / 6),
                    endY - headLength * Math.sin(angle + Math.PI / 6)
                );
                ctx.closePath();
                ctx.fillStyle = vector.positive ? '#0ea5e9' : '#a855f7';
                ctx.fill();
                
                // Update spin (precession)
                vector.spin += vector.torque * 0.01;
            });
            
            // Draw labels
            ctx.font = 'bold 16px Inter';
            ctx.fillStyle = '#0ea5e9';
            ctx.fillText('180 Positive (CCW)', centerX + radius + 40, centerY - 60);
            ctx.fillStyle = '#a855f7';
            ctx.fillText('180 Negative (CW)', centerX + radius + 40, centerY + 60);
            
            // Draw torque indicator
            this.netTorque = this.vectors.reduce((sum, v) => sum + v.torque, 0);
            ctx.font = '14px Inter';
            ctx.fillStyle = this.netTorque > 0 ? '#0ea5e9' : '#a855f7';
            ctx.fillText(`Net Torque: ${this.netTorque.toFixed(3)}`, 20, 30);
            
            this.animationId = requestAnimationFrame(draw);
        };
        
        draw();
        
        window.addEventListener('resize', () => {
            canvas.width = canvas.offsetWidth;
            canvas.height = canvas.offsetHeight;
        });
    }
    initThreeBodySimulation(canvasId) {
        const canvas = document.getElementById(canvasId);
        if (!canvas) return;
        
        const ctx = canvas.getContext('2d');
        canvas.width = canvas.offsetWidth;
        canvas.height = canvas.offsetHeight;
        
        // Initialize gyroscopic bodies (representing vectors)
        this.gyroBodies = [
            { x: canvas.width * 0.3, y: canvas.height * 0.4, vx: 0, vy: 1.2, mass: 15, color: '#0ea5e9', positive: true },
            { x: canvas.width * 0.7, y: canvas.height * 0.4, vx: 0, vy: -1.2, mass: 15, color: '#a855f7', positive: false },
            { x: canvas.width * 0.5, y: canvas.height * 0.7, vx: 1.2, vy: 0, mass: 20, color: '#f59326', positive: null }
        ];
        
        // Initialize torque pulsars
        this.pulsars = [
            { x: canvas.width * 0.2, y: canvas.height * 0.2, radius: 10, frequency: 0.04, phase: 0, torque: 0.3 },
            { x: canvas.width * 0.8, y: canvas.height * 0.8, radius: 10, frequency: 0.06, phase: Math.PI, torque: -0.3 }
        ];
        
        const G = 0.3; // gyroscopic coupling constant
        const damping = 0.99;
        
        const updateStability = () => {
            // Calculate stability based on angular momentum balance
            let totalTorque = 0;
            this.gyroBodies.forEach(b => {
                totalTorque += b.positive === true ? 0.5 : (b.positive === false ? -0.5 : 0);
            });
            this.pulsars.forEach(p => {
                totalTorque += p.torque * Math.sin(p.phase);
            });
            const stability = Math.max(0, Math.min(100, 100 - Math.abs(totalTorque) * 20));
            
            const bar = document.getElementById('stabilityBar');
            const text = document.getElementById('stabilityText');
            if (bar && text) {
                bar.style.width = `${stability}%`;
                text.textContent = `${stability.toFixed(1)}% Stable`;
            }
        };
        
        const simulate = () => {
            if (!this.isRunning) return;
            
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            
            // Update pulsars (torque sources)
            this.pulsars.forEach(p => {
                p.phase += p.frequency;
                p.radius = 10 + Math.sin(p.phase) * 6;
            });
            
            // Calculate gyroscopic forces
            for (let i = 0; i < this.gyroBodies.length; i++) {
                let fx = 0, fy = 0;
                const body = this.gyroBodies[i];
                
                // Coupling with other bodies
                for (let j = 0; j < this.gyroBodies.length; j++) {
                    if (i === j) continue;
                    const other = this.gyroBodies[j];
                    const dx = other.x - body.x;
                    const dy = other.y - body.y;
                    const distance = Math.sqrt(dx * dx + dy * dy);
                    const force = G * body.mass * other.mass / (distance * distance + 1);
                    // Torque direction depends on positive/negative
                    const sign = (body.positive === other.positive) ? 1 : -1;
                    fx += sign * force * dx / distance;
                    fy += sign * force * dy / distance;
                }
                
                // Pulsar torque influences
                this.pulsars.forEach(p => {
                    const dx = p.x - body.x;
                    const dy = p.y - body.y;
                    const distance = Math.sqrt(dx * dx + dy * dy);
                    const torqueInfluence = p.torque * Math.sin(p.phase) / (distance + 1);
                    fx += torqueInfluence * dx / distance;
                    fy += torqueInfluence * dy / distance;
                });
                
                // Update velocity and position
                body.vx = (body.vx + fx / body.mass) * damping;
                body.vy = (body.vy + fy / body.mass) * damping;
                body.x += body.vx;
                body.y += body.vy;
                
                // Boundary reflection with gyroscopic spin
                if (body.x < 0 || body.x > canvas.width) {
                    body.vx *= -0.8;
                    if (body.positive !== null) body.positive = !body.positive;
                }
                if (body.y < 0 || body.y > canvas.height) {
                    body.vy *= -0.8;
                    if (body.positive !== null) body.positive = !body.positive;
                }
                body.x = Math.max(10, Math.min(canvas.width - 10, body.x));
                body.y = Math.max(10, Math.min(canvas.height - 10, body.y));
            }
            
            // Draw pulsars
            this.pulsars.forEach(p => {
                ctx.beginPath();
                ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
                ctx.fillStyle = 'rgba(245, 147, 38, 0.9)';
                ctx.fill();
                
                // Torque waves
                ctx.beginPath();
                ctx.arc(p.x, p.y, p.radius + 8, 0, Math.PI * 2);
                ctx.strokeStyle = 'rgba(245, 147, 38, 0.5)';
                ctx.lineWidth = 3;
                ctx.stroke();
            });
            
            // Draw gyroscopic bodies
            this.gyroBodies.forEach(body => {
                ctx.beginPath();
                ctx.arc(body.x, body.y, 14, 0, Math.PI * 2);
                ctx.fillStyle = body.color;
                ctx.fill();
                
                // Direction indicator (arrow)
                const angle = Math.atan2(body.vy, body.vx);
                ctx.beginPath();
                ctx.moveTo(body.x, body.y);
                ctx.lineTo(
                    body.x + 20 * Math.cos(angle),
                    body.y + 20 * Math.sin(angle)
                );
                ctx.strokeStyle = '#ffffff';
                ctx.lineWidth = 2;
                ctx.stroke();
                
                // Glow effect
                ctx.beginPath();
                ctx.arc(body.x, body.y, 25, 0, Math.PI * 2);
                const gradient = ctx.createRadialGradient(
                    body.x, body.y, 14,
                    body.x, body.y, 25
                );
                gradient.addColorStop(0, body.color + 'a0');
                gradient.addColorStop(1, body.color + '00');
                ctx.fillStyle = gradient;
                ctx.fill();
            });
            
            // Draw gyroscopic connections
            ctx.strokeStyle = 'rgba(255, 255, 255, 0.15)';
            ctx.lineWidth = 1;
            for (let i = 0; i < this.gyroBodies.length; i++) {
                for (let j = i + 1; j < this.gyroBodies.length; j++) {
                    ctx.beginPath();
                    ctx.moveTo(this.gyroBodies[i].x, this.gyroBodies[i].y);
                    ctx.lineTo(this.gyroBodies[j].x, this.gyroBodies[j].y);
                    ctx.stroke();
                }
            }
            
            updateStability();
            
            this.animationId = requestAnimationFrame(simulate);
        };
        
        // Start simulation
        this.isRunning = true;
        simulate();
        
        // Button handlers
        document.getElementById('startSim')?.addEventListener('click', () => {
            this.isRunning = true;
            if (!this.animationId) {
                simulate();
            }
        });
        
        document.getElementById('resetSim')?.addEventListener('click', () => {
            this.isRunning = false;
            if (this.animationId) {
                cancelAnimationFrame(this.animationId);
                this.animationId = null;
            }
            this.initThreeBodySimulation(canvasId);
        });
        
        document.getElementById('addPulsar')?.addEventListener('click', () => {
            const newPulsar = {
                x: Math.random() * canvas.width,
                y: Math.random() * canvas.height,
                radius: 10,
                frequency: 0.03 + Math.random() * 0.05,
                phase: Math.random() * Math.PI * 2,
                torque: Math.random() > 0.5 ? 0.4 : -0.4
            };
            this.pulsars.push(newPulsar);
        });
        
        window.addEventListener('resize', () => {
            canvas.width = canvas.offsetWidth;
            canvas.height = canvas.offsetHeight;
        });
    }
}
// Initialize when page loads
document.addEventListener('DOMContentLoaded', () => {
    const vectorEngine = new VectorPhysicsEngine();
    
    // Initialize visualizations
    vectorEngine.initVectorField('vectorFieldCanvas');
    vectorEngine.initThreeBodySimulation('threeBodyCanvas');
    
    // Make engine globally available
    window.VectorPhysicsEngine = vectorEngine;
});