this.attract = function () {
for (let i = 0; i < numofplanets; i) {
var r = Math.sqrt(Math.pow(blackhole.x - planet[i].x, 2) + Math.pow(blackhole.y - planet[i].y, 2));
planet[i].acclx = (blackhole.mass(blackhole.x - planet[i].x)) / Math.pow(r, 3);
planet[i].accly = (blackhole.mass - (blackhole.y - planet[i].y)) / Math.pow(r, 3);
// planet[i].acclx=(blackhole.x-planet[i].x)/60000; without force implementation in which planets get sucked inside the blackhole
// planet[i].accly=(blackhole.y-planet[i].y)/60000; without force implementation in which planets get sucked inside the blackhole)
planet[i].speedy += planet[i].accly;
planet[i].x += planet[i].speedx;
planet[i].y += planet[i].speedy;
}
}
i am trying to create gravity simulation model for a game in JavaScript.i need planets to move around a black hole in elliptical orbits. first i calculate acceleration and then add it to velocity and according to that i calculate the next position of planet. i first tried with just calculating distance vector between planets and black hole but in that scenario planets directly gets sucked inside the black hole and i need them to move in elliptical orbits. i tried calculating acceleration due to gravity but i think formula is wrong so can you correct it or provide some other implementation.