6

I've found an interesting article on adding radial gravity to box2d.

http://www.vellios.com/2010/06/06/box2d-and-radial-gravity-code/

To port this to lua though I need to calculate distance squared and normalize distance.

Love2d doesn't seem to have the functions to extract the appropriate vector, which is a shame.

Unless my math is lacking and somebody could help me out.

I can alway switch to box2d but love2d seemed like a neat solution.

greatwolf
  • 20,287
  • 13
  • 71
  • 105
Joseph Le Brech
  • 6,541
  • 11
  • 49
  • 84

1 Answers1

5

I've found how to do it using the HUMP library.

Like this.

ship = bodies[1]
shipVec = vector(ship:getX(),ship:getY())
planet = bodies[2]
planetVec = vector(planet:getX(),planet:getY())
distance = planetVec – shipVec
force = 250 / distance:len2()
normforce = force*distance
bodies[1]:applyImpulse(normforce.x, normforce.y,ship:getX(),ship:getY())
Joseph Le Brech
  • 6,541
  • 11
  • 49
  • 84