0
\$\begingroup\$

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[0];
    var sy = body.position[1];
    var bx = blackHoleBody.position[0];
    var by = blackHoleBody.position[1];

    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[1] -= magGrav*Math.cos(angleToBody);
    body.force[0] -= 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[0];
  var by = blackHoleBody.position[1];

  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?

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Seth Battin Nov 14 '14 at 6:11
  • \$\begingroup\$ 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. \$\endgroup\$ – Morgan ARR Allen Nov 14 '14 at 6:28
  • \$\begingroup\$ 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. \$\endgroup\$ – Morgan ARR Allen Nov 14 '14 at 7:05
  • \$\begingroup\$ 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. \$\endgroup\$ – user41442 Nov 14 '14 at 22:58
2
\$\begingroup\$

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 relation between orbital speed and acceleration:

The distance between the craft and the body:

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.