Search code examples
luagravitylove2d

Love2d and radial gravity


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.


Solution

  • 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())