# Initial force to achieve orbital velocity

I'm attempting to get asteroid to orbit a black hole that is asserting gravity. Some parts currently work, like the black hole asserting gravity, attracting asteroids in and destroying them. And sometimes the asteroids, or smaller chunks end up in orbit around the black hole. But my desire is to have them orbiting at random distances at the start of the game.

Gravity

First please note, I am certain the math behind a lot of this is off, I'm hacking a concept at the moment and once enough pieces are in place I intent to come back through and apply proper scale and units to everything. Next, it's written in JS but concepts should be fairly universal.

  function attractBodyToBlackHole(body) {
var sx = body.position;
var sy = body.position;
var bx = blackHoleBody.position;
var by = blackHoleBody.position;

var angleToBody = Math.atan2(sx - bx, sy - by);
var distanceToBody = Math.sqrt(Math.pow(sx - bx, 2) + Math.pow(sy - by, 2));

var magGrav = 0.8 - (distanceToBody / 14);
if(magGrav < 0) {
magGrav = 0;
}

body.force -= magGrav*Math.cos(angleToBody);
body.force -= magGrav*Math.sin(angleToBody);
}


This function is applied to each object on p2's step, attracting it closer to the black hole.

Asteroids

function addAsteroid() {
var bx = blackHoleBody.position;
var by = blackHoleBody.position;

var a = Math.random() * (Math.PI * 2);
var dist = 3 + (Math.random() * 3);
var x = dist * Math.cos(a);
var y = dist * Math.sin(a);

var vx = ?;
var vy = ?;

var asteroidBody = new p2.Body({
mass:10,
position:[x,y],
velocity: [ vx, vy ]
});
}


So my question is, how can I calculate velocity x/y (or force x/y ideally) to get these objects, based on their position, distance and the force of attraction from the black hole?

• var magGrav = 0.8 - (distanceToBody / 14); <- this is very wrong. You won't be able to come up with correct velocities while this is your calculation for gravity. – Seth Battin Nov 14 '14 at 6:11
• Right, I should have dropped that, it doesn't actually affect this particular bodies. That was actually only affecting the player, so gravity was turned off past a certain distance. – Morgan ARR Allen Nov 14 '14 at 6:28
• Can you expand on why this is wrong? My thinking here was that the gravity would diminish with distance but I guess now it should be fixed. – Morgan ARR Allen Nov 14 '14 at 7:05
• Gravity uses a famous inverse square law, whereas your formula is linear in distance and depends on the masses of the two objects. It is possible to calculate a speed for each distance which would cause asteroids to orbit the black hole, but it will not be a standard formula, as you are using non-standard gravity. – user41442 Nov 14 '14 at 22:58

I know this is gamedev not game playing, but the Kerbal Space Program wiki has all the math and then some. More importantly, it explains it quite nicely in "plain English" if you drill down.

Here's the basic mathematics of how to get something into orbit: http://wiki.kerbalspaceprogram.com/wiki/Tutorial:_Basic_Orbiting_(Math)

I apologize for not copying the whole thing but it's quite long.

Strong starting points:

The relation between orbital speed and acceleration: The distance between the craft and the body: Note that there are quite a few different types of orbits and ways to get into orbit.

Newton's cannonball explains basic orbiting well:

http://en.wikipedia.org/wiki/Newton%27s_cannonball