Gravity well in Love?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Gravity well in Love?
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?
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...
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...
"When in doubt, use brute force." Ken Thompson
Re: Gravity well in Love?
So I guess I'll take a look at love gravity modules or something. Any direction?
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Gravity well in Love?
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)
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?
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
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?
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:
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:
Then you can update the speeds, directions and positions of the bodies like this:
Also you could move the gravity well around just by changing the blackHole.x and blackHole.y values. I hope that helps.
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
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
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
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 6 guests