Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 539
Joseph Le Brech Avatar asked Jan 25 '11 09:01

Joseph Le Brech


1 Answers

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())
like image 84
Joseph Le Brech Avatar answered Sep 28 '22 13:09

Joseph Le Brech