Gravity well in Love?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
ac3raven
Citizen
Posts: 55
Joined: Tue May 19, 2009 1:14 am

Gravity well in Love?

Post by ac3raven » Wed Jul 22, 2009 2:37 pm

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.

User avatar
Tenoch
Citizen
Posts: 76
Joined: Mon Jul 21, 2008 7:49 am

Re: Gravity well in Love?

Post by Tenoch » Wed Jul 22, 2009 3:11 pm

Everything is possible :megagrin:

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

User avatar
ac3raven
Citizen
Posts: 55
Joined: Tue May 19, 2009 1:14 am

Re: Gravity well in Love?

Post by ac3raven » Wed Jul 22, 2009 3:19 pm

So I guess I'll take a look at love gravity modules or something. Any direction?

User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Gravity well in Love?

Post by bartbes » Wed Jul 22, 2009 6:06 pm

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)

User avatar
appleide
Party member
Posts: 323
Joined: Fri Jun 27, 2008 2:50 pm

Re: Gravity well in Love?

Post by appleide » Thu Jul 23, 2009 12:44 am


Arthurio
Prole
Posts: 8
Joined: Sun Jul 12, 2009 7:12 pm

Re: Gravity well in Love?

Post by Arthurio » Tue Jul 28, 2009 10:00 pm

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

Matkins
Citizen
Posts: 53
Joined: Mon Mar 23, 2009 5:12 pm

Re: Gravity well in Love?

Post by Matkins » Tue Jul 28, 2009 10:59 pm

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.

Post Reply

Who is online

Users browsing this forum: Nelvin, slime and 8 guests