UrbanInkShack's picture
It's supposed to be based on vector math of a gyroscope that utilizes the zero with 360 vectors for reference with 180 being negative and 180 being positive... Creating a gyroscope for any know vector in space
5a5134c verified
Raw History Blame Contribute Delete
13.3 kB
// 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;
});