Page 1 of 1
Gravity well in Love?
Posted: Wed Jul 22, 2009 2:37 pm
by ac3raven
Is it possible to make a gravity well in Love? Basically just a circle that pulls objects toward itself with a simulated force of gravity. And then to move that circle around using the keyboard.
Re: Gravity well in Love?
Posted: Wed Jul 22, 2009 3:11 pm
by Tenoch
Everything is possible
You can either code it yourself if you know basic physics, or use the love.physics module, which will require you to know basic physics too, since you'll have to compute the forces yourself.
But in any case, LÖVE is mainly an input/output engine. It notifies you of keyboard/mouse input and draws stuff where you ask it to. Your imagination and your mastering of algorithms is the limit...
Re: Gravity well in Love?
Posted: Wed Jul 22, 2009 3:19 pm
by ac3raven
So I guess I'll take a look at love gravity modules or something. Any direction?
Re: Gravity well in Love?
Posted: Wed Jul 22, 2009 6:06 pm
by bartbes
There is no 'gravity module', Box2D has some gravity, but that is in a direction, not to a coordinate.
One of the things you can do manually is checking the angle, and distributing the speed increase the correct way. (sin and cos are your friends)
Anyway, you fell in a black hole here. (bad, bad, pun, not worthy of it's own existence)
Re: Gravity well in Love?
Posted: Thu Jul 23, 2009 12:44 am
by appleide
Re: Gravity well in Love?
Posted: Tue Jul 28, 2009 10:00 pm
by Arthurio
I don't think you can use 'gravity' for what you want to do because the box2D gravity is global and directional.
Try body:applyForce(fx, fy)
http://love2d.org/docs/Body_applyForce_1.html
edit: I think you need to do this every update for every object that is near the gravity well
Re: Gravity well in Love?
Posted: Tue Jul 28, 2009 10:59 pm
by Matkins
Strangely enough this is exactly what I've been working on. Do not use the love.physics functions for this. Initialize your own tables for bodies and your gravity well data:
Code: Select all
width = love.graphics.getWidth()
height = love.graphics.getHeight()
body = {}
for i=1, 50 do
body[i] = {
mass = 3,
x = math.random(width),
y = math.random(height),
velocityX = 0, velocityY = 0
}
end
blackHole = { mass = 10, x = width*0.5, y = height*0.5 }
G = 2.5
Also I set G to 2.5, this will be used as the gravitational constant, the actual value of G in reality is absolutely tiny. 2.5 is MASSIVE, but we're working on a much smaller time frame here so we need bigger G.
Here are two functions I wrote that do the physics:
Code: Select all
-- grav() takes two masses and a distance and returns the gravitational force between them (Newton's equation)
function grav(m1, m2, r)
local F = G*((m1*m2)/r^2)
return F
end
Code: Select all
-- vect() takes x and y coordinates of two points and returns the distance between them and the 2D unit vector
function vect(m1x, m1y, m2x, m2y)
local dx = m2x - m1x
local dy = m2y - m1y
local d = math.sqrt(dx^2 + dy^2)
local vx = dx/d
local vy = dy/d
return d, vx, vy
end
Then you can update the speeds, directions and positions of the bodies like this:
Code: Select all
function update(dt)
for i=1, #body do
blackHoleDist, vectX, vectY = vect(body[i].x, body[i].y, blackHole.x, blackHole.y)
f = grav(body[i].mass, blackHole.mass, blackHoleDist)
body[i].velocityX = body[i].velocityX + (vectX*f)
body[i].velocityY = body[i].velocityY + (vectY*f)
body[i].x = body[i].x + (body[i].velocityX*dt)
body[i].y = body[i].y + (body[i].velocityY*dt)
end
end
Also you could move the gravity well around just by changing the blackHole.x and blackHole.y values. I hope that helps.