Hello everyone. I'm going to be working on a group project for school pretty soon. Someone mentioned using Lua for it, so I went looking for game libraries and found this one. I've got a decent understanding of things, but I've got a question about making multiple gravitational bodies.
Say I've got a ship flying around in space, and there are two planets in this space. What would I do to make both of these planets exert gravity on the ship from all sides? My gut says to make multiple Worlds, but I'm not sure if that's possible.
Thanks!
Gravity in Space
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- TechnoCat
- Inner party member
- Posts: 1611
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: Gravity in Space
yay psuedo code
Then take the gravity and use http://love2d.org/wiki/Body:applyForce if you are using box2d.
Code: Select all
for each ship
gravity <- vector (0,0)
for each planet
gravity <- gravity + planetGravity(ship)
Re: Gravity in Space
So I would need to create a custom gravity function for the planets to use? I don't think that's a problem, I just want to make sure I understand.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Gravity in Space
Hi!
If multiple objects exert gravity on something, the acceleration vectors of both of them are added, e.g. a pull to the right with 2 m/s² and another pull to the right with 2 m/s² add up to 4 m/s². And a pull to the right with 2 m/s² and a pull to the left with 2 m/s² add up to 0 m/s², so no acceleration. If your velocity is also 0, that means you'll stay where you are. There are zones in actual outer space where this is the case (cue someone linking the Wikipedia article, I couldn't find it).
But if you use Box2D (i.e. love.physics), that happens automatically. If you write your own physics simulation, you'll have to add vectors anyway.
You need to calculate the gravity for each planet yourself anyway, since Box2D itself uses only simple gravity (i.e. in a direction, not to one or multiple points in space).
No.Rakuen wrote:My gut says to make multiple Worlds, but I'm not sure if that's possible.
If multiple objects exert gravity on something, the acceleration vectors of both of them are added, e.g. a pull to the right with 2 m/s² and another pull to the right with 2 m/s² add up to 4 m/s². And a pull to the right with 2 m/s² and a pull to the left with 2 m/s² add up to 0 m/s², so no acceleration. If your velocity is also 0, that means you'll stay where you are. There are zones in actual outer space where this is the case (cue someone linking the Wikipedia article, I couldn't find it).
But if you use Box2D (i.e. love.physics), that happens automatically. If you write your own physics simulation, you'll have to add vectors anyway.
You need to calculate the gravity for each planet yourself anyway, since Box2D itself uses only simple gravity (i.e. in a direction, not to one or multiple points in space).
Help us help you: attach a .love.
Re: Gravity in Space
Alright, thank you very much!
- BlackBulletIV
- Inner party member
- Posts: 1261
- Joined: Wed Dec 29, 2010 8:19 pm
- Location: Queensland, Australia
- Contact:
Re: Gravity in Space
Here's a little function (untested - however I pulled it out of my GravityWell class which is tested) that makes a gravity well at a certain point and calculates how much force should be added to a physics body (I shared this in the General/Share Your Utils thread):
However, you'll probably want to wrap that in a class. If you want an idea of how I've done it (although it's integrated into my framework Grace), and want to see a demo, take a look at this thread: http://love2d.org/forums/viewtopic.php?f=5&t=2422.
Code: Select all
--[[
body is a physics body.
x/y is the position of the well.
power is the power you want applied. Something in the hundreds is good.
epsilon is the minimum distance for which gravity is calculated.
Particles closer than this distance experience a gravity force as if
they were this distance away. This stops the gravity effect blowing
up as distances get small.
--]]
function gravityWell(body, x, y, power, epsilon)
local lx = x - body:getX()
local ly = y - body:getY()
local ldSq = lx * lx + ly * ly
power = power * 10000 or 100000
epsilon = epsilon * epsilon or 50 * 50
if ldSq ~= 0 then
local ld = math.sqrt(ldSq)
if ldSq < epsilon then ldSq = epsilon end
local lfactor = (power * love.timer.getDelta()) / (ldSq * ld)
local oldX, oldY = body:getLinearVelocity()
body:setLinearVelocity(oldX + lx * lfactor, oldY + ly * lfactor)
end
end
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 1 guest