Skip to content

Commit

Permalink
Update gravity.js
Browse files Browse the repository at this point in the history
  • Loading branch information
GrimReaper2654 committed Nov 8, 2023
1 parent 205a960 commit 8734e24
Showing 1 changed file with 129 additions and 72 deletions.
201 changes: 129 additions & 72 deletions gravity.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async function load() {
replacehtml(`<canvas id="main" width="${display.x}" height="${display.y}" style="position: absolute; top: 0; left: 0; z-index: 1;"></canvas>`);
await sleep(100);
game();
}
};

function randchoice(list, remove=false) { // chose 1 from a list and update list
let length = list.length;
Expand Down Expand Up @@ -116,6 +116,9 @@ function getDist(sPos, tPos) {

function correctAngle(a) {
a = a%(Math.PI*2);
if (a < 0) {
a += Math.PI*2;
}
return a;
};

Expand Down Expand Up @@ -184,11 +187,15 @@ function aim(initial, final) {
} else if (diff.x > 0 && diff.y < 0) {
return Math.PI*2 - angle;
}
throw `You f*cked up again: ${angle}`;
throw `You f*cked up again (aim): ${angle}`;
};

function toComponent(m, r) {
return {x: m * Math.sin(r), y: -m * Math.cos(r)};
return {x: m * Math.cos(r), y: m * Math.sin(r)};
};

function toPol(i, j) {
return {m: Math.sqrt(i**2+j**2), r: aim({x: 0, y: 0}, {x: i, y: j})};
};

function roman(number) {
Expand Down Expand Up @@ -328,9 +335,17 @@ function vMath(v1, v2, mode) {
}
};

function toDeg(rad) {
return rad * 180 / Math.PI;
};

function toRad(deg) {
return deg / 180 * Math.PI;
};

function mass(p) {
return sphereVolume(p.r*data.constants.scale)*p.p;
}
};

const data = {
constants: {
Expand All @@ -342,6 +357,7 @@ const data = {
timeDilation: 100, // 1 second irl = x seconds ig
zoom: 2, // zoom factor
g: 0.00000000006674, // gravitation constant, source: https://en.wikipedia.org/wiki/Gravitational_constant
collisionDampening: 1.25,
},
particlePhysics: {
x: 0, // x coordinate
Expand Down Expand Up @@ -409,10 +425,17 @@ function handleGravityField(particles) {
// This is not intended to simulate motion
// It is ment to show the magnitude of the force of gravity
//console.log(particles[i].ax, particles[i].ay);
particles[i].x = particles[i].ax*data.constants.scale;
particles[i].y = particles[i].ay*data.constants.scale;
particles[i].x = particles[i].px+particles[i].ax*data.constants.scale;
particles[i].y = particles[i].py+particles[i].ay*data.constants.scale;
//particles[i].x = particles[i].ax*data.constants.scale;
//particles[i].y = particles[i].ay*data.constants.scale;
//console.log(`dist: ${getDist({x: 0, y: 0}, particles[i])}, sf: ${sf}`);
//console.log(particles[i].ax * data.constants.scale**2, particles[i].ay * data.constants.scale**2);
if (toPol(particles[i].ax, particles[i].ay).m > 0.00000063) {
particles[i].ax = Infinity;
particles[i].ay = Infinity;
}
particles[i].x = particles[i].px + particles[i].ax * data.constants.scale;
particles[i].y = particles[i].py + particles[i].ay * data.constants.scale;
//console.log(particles[i].x-particles[i].px, particles[i].y-particles[i].py);
}
return particles;
};
Expand Down Expand Up @@ -470,7 +493,7 @@ function handleParticleMotion(particles) {
return newParticles;
};

function resetPos(particles) {
function resetGravityField(particles) {
for (let i = 0; i < particles.length; i++) {
particles[i].x = particles[i].px;
particles[i].y = particles[i].py;
Expand All @@ -492,14 +515,16 @@ function handleForces(effectParticles, affectedParticles, force, type) {
if (effectParticles==undefined) {
for (let i = 0; i < affectedParticles.length-1; i++) {
for (let j = i+1; j < affectedParticles.length; j++) {
/*
if (type == 'contact') {
console.log(getDist(affectedParticles[i], affectedParticles[j]), (affectedParticles[i].r + affectedParticles[j].r)*data.constants.scale);
if (getDist(affectedParticles[i], affectedParticles[j]) <= (affectedParticles[i].r + affectedParticles[j].r)*data.constants.scale) {
console.log(`${affectedParticles[i].id} and ${affectedParticles[j].id}`);
console.log(getDist(affectedParticles[i], affectedParticles[j]), (affectedParticles[i].r + affectedParticles[j].r)*data.constants.upSize);
if (getDist(affectedParticles[i], affectedParticles[j]) <= (affectedParticles[i].r + affectedParticles[j].r)) {
console.log('should apply contact force');
}
}
}*/
//console.log(affectedParticles[i].id, affectedParticles[j].id);
if (type == 'contact'? getDist(affectedParticles[i], affectedParticles[j]) <= (affectedParticles[i].r + affectedParticles[j].r)*data.constants.scale : getDist(affectedParticles[i], affectedParticles[j]) > 0) {
if (type == 'contact'? getDist(affectedParticles[i], affectedParticles[j]) <= (affectedParticles[i].r + affectedParticles[j].r)*data.constants.upSize : getDist(affectedParticles[i], affectedParticles[j]) > 0) {
console.log(`Applying ${type} force: between ${affectedParticles[i].id} and between ${affectedParticles[j].id}`);
let res = force(affectedParticles[i], affectedParticles[j]);
affectedParticles[i] = res[0];
Expand All @@ -511,7 +536,7 @@ function handleForces(effectParticles, affectedParticles, force, type) {
for (let i = 0; i < effectParticles.length; i++) {
for (let j = 0; j < affectedParticles.length; j++) {
//console.log(effectParticles[i].id, affectedParticles[j].id);
if (type == 'contact'? getDist(effectParticles[i], affectedParticles[j]) <= (effectParticles[i].r + affectedParticles[j].r)*data.constants.scale : getDist(effectParticles[i], affectedParticles[j]) > 0) {
if (type == 'contact'? getDist(effectParticles[i], affectedParticles[j]) <= (effectParticles[i].r + affectedParticles[j].r)*data.constants.upSize : getDist(effectParticles[i], affectedParticles[j]) > 0) {
//console.log(`Applying ${type} force between different arrays: between ${effectParticles[i].id} and between ${affectedParticles[j].id}`);
let res = force(effectParticles[i], affectedParticles[j]);
affectedParticles[j] = res[1];
Expand All @@ -522,11 +547,50 @@ function handleForces(effectParticles, affectedParticles, force, type) {
return affectedParticles;
};

function normalForce(p1, p2) {
// gravity, but reverse
// f = g * (m1 * m2) / r^2
let f = data.constants.g * mass(p1) * mass(p2) / (getDist(p1, p2)*data.constants.scale)**2;
// f = m * a
let a1 = f / mass(p1);
let a2 = f / mass(p2);
let r1 = correctAngle(aim(p1, p2));
let r2 = correctAngle(r1+Math.PI);
let va1 = {x: a1*Math.cos(r1), y: a1*Math.sin(r1)};
let va2 = {x: a2*Math.cos(r2), y: a2*Math.sin(r2)};
p1.ax -= va1.x;
p1.ay -= va1.y;
p2.ax -= va2.x;
p2.ay -= va2.y;
return [p1, p2];
};

function collision(p1, p2) {
p1.vx = (p1.vx * (mass(p1) - mass(p2)) + (2 * mass(p2) * p2.vx)) / (mass(p1) + mass(p2));
p1.vy = (p1.vy * (mass(p1) - mass(p2)) + (2 * mass(p2) * p2.vx)) / (mass(p1) + mass(p2));
p2.vx = (p2.vx * (mass(p2) - mass(p1)) + (2 * mass(p1) * p1.vx)) / (mass(p1) + mass(p2));
p2.vy = (p2.vy * (mass(p2) - mass(p1)) + (2 * mass(p1) * p1.vx)) / (mass(p1) + mass(p2));
let v1 = toPol(p1.vx, p1.vy);
let v2 = toPol(p1.vx, p1.vy);
let v1N = toComponent(toPol(p1.vx, p1.vy).m, correctAngle(v1.r-(aim(p1, p2)-Math.PI/2)));
v1N = vMath(v1N, data.constants.collisionDampening, '*');
console.log(v1N);
let v1p1C = toComponent(v1N.x, correctAngle(aim(p1, p2)-Math.PI/2));
let v1p2C = toComponent(v1N.y, correctAngle(aim(p1, p2)));

let v2N = toComponent(toPol(p2.vx, p2.vy).m, correctAngle(v2.r-(aim(p2, p1)+Math.PI/2)));
v2N = vMath(v2N, data.constants.collisionDampening, '*');
console.log(v2N);
let v2p2C = toComponent(v2N.x, correctAngle(aim(p2, p1)-Math.PI/2));
let v2p1C = toComponent(v2N.y, correctAngle(aim(p2, p1)));
p1.x -= p1.vx + p1.ax;
p1.y -= p1.vy + p1.ay;
p2.x -= p2.vx + p2.ax;
p2.y -= p2.vy + p2.ay;
p1.vx = v1p1C.x;
p1.vy = v1p1C.y;
p2.vx = v1p2C.x;
p2.vy = v1p2C.y;
p1.vx += v2p1C.x;
p1.vy += v2p1C.y;
p2.vx += v2p2C.x;
p2.vy += v2p2C.y;
return [p1, p2];
};

Expand All @@ -545,7 +609,7 @@ function gravity(p1, p2) {
let va2 = {x: a2*Math.cos(r2), y: a2*Math.sin(r2)};
//console.log(r1/Math.PI*180,r2/Math.PI*180);
if (va1.x == Infinity || va1.y == Infinity || va2.x == Infinity || va2.y == Infinity) {
console.log('Shit');
console.log('Sh*t');
console.log(mass(p1), mass(p2), getDist(p1, p2));
console.log(f, a1, a2, r1, r2);
}
Expand All @@ -556,6 +620,28 @@ function gravity(p1, p2) {
return [p1, p2];
};

function gravityVisualisation(p2, p1) {
if (p1.ignoreOtherForces == true) {
return [p2, p1];
}
// f = g * (m1 * m2) / r^2
let f = data.constants.g * mass(p1) * mass(p2) / (getDist(p1, p2)*data.constants.scale)**2;
// f = m * a
let a1 = f / mass(p1);
//console.log(f);
//console.log(f/a1, f/a2);
let r1 = correctAngle(aim(p1, p2));
//console.log(r1,r2);
let va1 = {x: a1*Math.cos(r1), y: a1*Math.sin(r1)};
//console.log(r1/Math.PI*180,r2/Math.PI*180);
//console.log(nV);
//console.log(va1.x*data.constants.scale, va1.y*data.constants.scale);
p1.ax += va1.x;
p1.ay += va1.y;
//console.log(toPol(p1.ax, p1.ay).m * data.constants.scale);
return [p2, p1];
};

function renderParticles(particles) {
for (let i = 0; i < particles.length; i++) {
//console.log(t%FPT/FPT);
Expand All @@ -570,10 +656,11 @@ function physics() {
//console.log(getDist(particles[0], particles[0]));
particles = resetAccel(particles);
gravityField = resetAccel(gravityField);
gravityField = resetPos(gravityField);
//particles = handleForces(undefined, JSON.parse(JSON.stringify(particles)), collision, 'contact');
gravityField = resetGravityField(gravityField);
particles = handleForces(undefined, JSON.parse(JSON.stringify(particles)), gravity, 'non-contact');
gravityField = handleForces(JSON.parse(JSON.stringify(particles)), JSON.parse(JSON.stringify(gravityField)), gravity, 'non-contact');
particles = handleForces(undefined, JSON.parse(JSON.stringify(particles)), normalForce, 'contact');
particles = handleForces(undefined, JSON.parse(JSON.stringify(particles)), collision, 'contact');
gravityField = handleForces(JSON.parse(JSON.stringify(particles)), JSON.parse(JSON.stringify(gravityField)), gravityVisualisation, 'non-contact');
particles = handleParticleMotion(particles);
gravityField = handleGravityField(gravityField);
};
Expand All @@ -583,43 +670,10 @@ function graphics() {
clearCanvas('main');
grid('main', 50, defaultCenter, {colour: 'rgba(255,255,255,0.05)', width: 2});
renderParticles(gravityField);
//renderPastParticle(gravityField);
renderParticles(particles);
};

/*
let particle1 = JSON.parse(JSON.stringify(data.particlePhysics));
particle1.p = 5514;
particle1.r = 6.371;
particle1.x = 200;
particle1.y = -200;
particle1.vy = -0.5;
particle1.id = 'earth1';
let particle2 = JSON.parse(JSON.stringify(data.particlePhysics));
particle2.p = 5514;
particle2.r = 6.371;
particle2.x = -200;
particle2.y = -200;
particle2.vy = 0.5;
particle2.id = 'earth2';
particle1.style.fill = {r: 255, g: 0, b: 0, a: 1};
particle2.style.fill = {r: 0, g: 255, b: 0, a: 1};
particles.push(JSON.parse(JSON.stringify(particle1)));
particles.push(JSON.parse(JSON.stringify(particle2)));
particle1.style.fill = {r: 255, g: 255, b: 255, a: 1};
particle2.style.fill = {r: 0, g: 0, b: 255, a: 1};
particle1.y = 200;
particle2.y = 200;
particles2.push(JSON.parse(JSON.stringify(particle2)));
particles2.push(JSON.parse(JSON.stringify(particle1)));
*/

function addSolarSystem() {
let particle1 = JSON.parse(JSON.stringify(data.particlePhysics));
particle1.p = 1410;
Expand Down Expand Up @@ -659,7 +713,7 @@ function addSolarSystem() {
particle1.r = 0.006371;
particle1.x = 0;
particle1.y = 150;
particle1.vx = 0.0297848;
particle1.vx = 0.0297848; //0.0297848
particle1.id = 'Earth';
particle1.style.fill = {r: 0, g: 255, b: 0, a: 1};
particle1.style.stroke.colour = {r: 0, g: 0, b: 255, a: 1};
Expand Down Expand Up @@ -687,34 +741,35 @@ function addSolarSystem() {
particle1.style.stroke.colour = {r: 200, g: 0, b: 0, a: 1};
particle1.style.stroke.width = 3;
particles.push(JSON.parse(JSON.stringify(particle1)));
}
};

function addTestingObjects() {
let particle1 = JSON.parse(JSON.stringify(data.particlePhysics));
particle1.p = 1410;
particle1.r = 0.0696;
particle1.x = -200;

particle1.p = 1500;
particle1.r = 0.07;
particle1.x = 100;
particle1.y = 0;
particle1.vx = 0;
particle1.vy = 0.001;
particle1.id = 'Sol1';
particle1.vy = 0.01;
particle1.id = 'Sol2';
particle1.style.fill = {r: 255, g: 255, b: 0, a: 1};
particle1.style.stroke.colour = {r: 255, g: 200, b: 0, a: 1};
particle1.style.stroke.width = 5;
particles.push(JSON.parse(JSON.stringify(particle1)));

particle1.p = 1410;
particle1.r = 0.05;
particle1.x = 200;
particle1.p = 1500;
particle1.r = 0.07;
particle1.x = -100;
particle1.y = 0;
particle1.vx = 0;
particle1.vy = -0.001;
particle1.id = 'Sol2';
particle1.vy = -0.01;
particle1.id = 'Sol1';
particle1.style.fill = {r: 255, g: 255, b: 0, a: 1};
particle1.style.stroke.colour = {r: 255, g: 200, b: 0, a: 1};
particle1.style.stroke.width = 5;
particles.push(JSON.parse(JSON.stringify(particle1)));
}
};

const TPS = 20;
const FPS = 60;
Expand All @@ -730,9 +785,9 @@ async function game() {
particle2.style.fill = {r: 255, g: 255, b: 255, a: 1};
particle2.style.stroke.colour = {r: 200, g: 200, b: 200, a: 1};
particle2.style.stroke.width = 1;
gravityField = gravityGrid(20, particle2, 5);
addSolarSystem();
//addTestingObjects();
gravityField = gravityGrid(25, particle2, 20);
//addSolarSystem();
addTestingObjects();
console.log(particles);
graphics();
await sleep(1000);
Expand All @@ -748,3 +803,5 @@ async function game() {
};


let x = NaN;
console.log(x == NaN);

0 comments on commit 8734e24

Please sign in to comment.