Orbital System tool.

I swear there used to be one somewhere, but here’s the thing.

It would be a tool, where you designate an object as the “center of gravity.” I don’t know if such a thing is actually possible, so I’m thinking it would merely just be “select a thing for everything to revolve around.” You could set the radius in the tool menu, where the “gravitational force” will affect certain objects . It might just be like faux gravity, although a directional gravity tool that also has options for a circular orbit would be really cool.

You might also select certain objects to revolve around the central object, and maybe even the number of orbits (for example, 5 orbits means that 5 objects can be attracted and bound by gravitational pull.)

I have no idea why I’d like this addon, to be honest although I could see this useful for contraptions, dioramas, and spacebuild applications. The science teachers using GMod could also do atomic structure using blue barrels as an electron, a giant sphere for a nucleus, and so forth. I think physics in Gmod gives me a hardon or something. :V

Bumping, but only because I’m going to try and do this myself. Does anyone have any idea how to get objects to levitate and rotate around another?

Well, you could start by disabling their gravity, then applying force towards the central object. As for keeping them in orbit, it could work with just that if source has centrifugal forces, you would just have to get them moving fast enough.

You need to use physobj:SetVelocity, then use the proper math to apply correct velocity in the vector direction it needs to go to orbit the centre of gravity based on the orbiting entities position.

If you find out how to do the gravity tell me I’ve always been curious( I’ve never tried looking. )

Fair enough. I think this’ll be my pet project for a while as I pick up Lua. I’m honestly thinking that the gravity per se would not work, but it would be more like “fake gravity” causing objects to rotate around a set object. It might cause problems because the last time I saw anything modify gravity, it was meant to make it so the entire map could be inverted, sent sideways, and such.

If you’re picking up Lua, don’t do this unless you’re good at math( assuming you need a fairly decent level of math to do this), i can’t even do algebra so

Heres a quick equation to get you started with the gravity, run this once a second to get true gravity, for more accurate gravity run this more often and multiply the 600 by the change in time since this was last run.
[lua]// Ag = 600 units/sec^2
local gravityorigin = Vector(0,0,0) //where the gravity originates from
local forcedir = (ent:GetPos() - gravityorigin):Normalize()
ent:SetVelocity(ent:GetVelocity() + forcedir*600) //accelerate it at 600 units/sec^2 towards the origin[/lua]

I’m fairly decent with math. The only reason I tanked calculus and AP physics was my lack of studying.

Also, I’m assuming that the gravity code only affects a certain radius, not the entire map, like the direction gravity thing.

So this actually seems fairly simple. I’d need to code a tool that would designate the center of gravity, and then define the radius of its gravitational pull.

Although this raises more questions. Would this cause objects closer to the center to orbit relatively faster than those at the very edge of gravitational pull? Would heavier objects be affected the same as lighter ones?

When dealing with gravity, mass does not change anything about velocity or acceleration, so a heavy object would just have more energy and force acting on it than a lighter one.

The physics behind orbits (in real life) are really interesting. When something is in orbit, it is actually perpetually falling to earth. It is going just fast enough that it falls to the planet at the same rate that the curve of the earth falls away from the satellite.

To better visualize this, look at what happens when you throw a baseball up at an angle; it travels in a parabolic arc. If you kept throwing the baseball faster and faster, eventually it would be going so fast, that the length and height of that parabolic arc would be the size of the earth. If air resistance wasn’t a factor, the ball would continue to travel at this speed forever and would begin to orbit the earth.

[editline]14th December 2011[/editline]

Got a basic prototype done

Here’s the think function that makes it tick. It’s really simple and fairly cheap to run.

[lua]local last = 0; --last time it was run, used to calculate delta time (change in time)
function ENT:Think() --do gravity
local scalar = (SysTime() - last)*-600 --do multiplication now rather than later multiple times over
last = SysTime()
for k,v in pairs(ents.FindInSphere(self:GetPos(),5000)) do–everything in a 5000 unit radius is attracted
local phys = v:GetPhysicsObject()
if phys and phys:IsValid() then --no non physics objects
phys:EnableGravity(false)–dont fall to the floor
phys:EnableDrag(false)–dont slow down
phys:AddVelocity((v:GetPos() - self:GetPos()):Normalize()*scalar)–“gravity”

All I did to get those barrels orbiting was fling them with the physgun away from the planet at an angle. If you fling them just right, you can get a nice tight circular orbit, otherwise it will be large, oblong, and elliptical.

This is all extremely useful, thanks a bunch.

What happens when objects collide in each others’ orbit?

On a different set of questions:

Would I be able to set the actual velocity of satellites orbiting the object?

Can I designate objects currently in orbit as satellites, so that they remain indefinitely in orbit (for that matter, will objects eventually fall out of orbit?)

How easy would you say this would be into making a tool?

I’m sorry about the number of questions I’m asking, but I’m just trying to be as informed as possible. I figured reverse-engineering the lua involved would help me learn how it works.

He gave you everything you need to do to make a swep out of this. I’ll make a more ‘sweppy’ example with what he supplied

[editline]16th December 2011[/editline]

–Quick example.

– A table to store all planets.
planets = {};

function AddPlanet( entity, pullRadius )
if ( entity == nil ) then return end;
pullRadius = pullRadius or 5000;

local newPlanet = { entity = entity, radius = pullRadius, last = 0 };
table.insert( planets, newPlanet );


function PlanetExists(ent)
for _,p in pairs( planets ) do
if ( p.entity == ent ) then
return true;

return false;


– You’d want to do this part in a tool.
– I don’t know how to get into the tool menu or whatever but you’d need to add a option for radius.
function SWEP:PrimaryAttack()
local player = self.Owner;
local trace = player:GetEyeTrace();

if ( trace.Entity == nil ) then return end;
if ( PlanetExists(trace.Entity) == true ) then return end;

AddPlanet( trace.Entity, 5000 ); -- Make a tool option for the 2nd argument(radius) here.

    -- Just for general use.
trace.Entity.IsPlanet == true;


function ThinkHook()
for _,p in pairs( planets ) do
if ( p.entity != nil ) then

		local scalar = (SysTime() - p.last)*-600
		p.last = SysTime();
		for k,v in pairs( ents.FindInSphere(p.entity:GetPos(),5000)) do
			local phys = v:GetPhysicsObject();
			if ( phys and phys:IsValid() ) then
				phys:AddVelocity( (v:GetPos() - p.entity:GetPos() ):Normalize()*scalar );

hook.Add(“Think”, “PlanetThink”, ThinkHook );