is it here that nothing to do with the number of asteroids that you want to put a Gravity?
function LevelSetup()
-- ** None of these are strictly necessary for Gravity... change as you like. **
SetBackdropColour(0,0,0)
Globals.Agents.MaxSpeed=1000
Globals.Agents.MinSpeed=500
Globals.Mines.MinSpeed=1200
Globals.Mines.MaxSpeed=1200
Globals.Mines.MinHealth=2000
Globals.Mines.MaxHealth=2000
Globals.Asteroids.MaxTrees=3
Globals.Asteroids.MinRadius=125
Globals.Asteroids.MaxRadius=725
Globals.Asteroids.RadiusPowerRule=1.5
Globals.Asteroids.MinCoreHealth=50
Globals.Asteroids.MaxCoreHealth=900
Globals.Asteroids.CoreHealthPower=1
Globals.Asteroids.MinSendDistance=60000
Globals.Asteroids.MaxSendDistance=60000
Globals.Asteroids.SendPowerRule=1.4
Globals.Asteroids.SpawnCap=40
Globals.Asteroids.SeedlingCap=1000
Globals.G.Asteroids=(0)
Globals.AI.GraceTimer=(9999999)
Globals.G.EnemyFactionsMin=(0)
Globals.G.EnemyFactionsMax=(0)
Globals.G.MinAsteroidSeparation=100
Globals.G.MaxAsteroidNeighbourDist=60000
Globals.G.GreysProbability=0
Globals.Structures.FlowerProbability=(0.1)
SetVignetteAlpha(0)
-- **
-- ** Initialise Gravity Variables. No need to change anything here.
AccelerationX = {}
AccelerationY = {}
MomentumX = {}
MomentumY = {}
density = {}
CoordX = {}
CoordY = {}
roidradius = {}
Timer = 0
-- **
-- ** G is the gravitational constant. Affects how powerful gravity is in the entire map. Change this as you like. **
G = 0.1
-- **
-- ****************************************
-- **********Asteroid Creation*************
-- ****************************************
--
-- How To.
-- * First we declare the asteroid ID to the Gravity Engine.
-- EG:
-- roid = 0
-- * Next comes the Acceleration array slot init. These should always be set to 0.
-- EG:
-- AccelerationX[roid] = 0
-- AccelerationY[roid] = 0
-- * MomentumX and MomentumY declare the initial velocity of the asteroid.
-- EG:
-- MomentumX[roid] = 10
-- MomentumY[roid] = 0
-- This example would produce an asteroid that is drifting east when the game begins.
-- * Density governs how dense the asteroid is. An asteroid that was made of metal has a higher density than an asteroid made of gas. Higher density means stronger gravity per unit of radius.
-- EG:
-- density[roid] = 1
-- * The coordinates of the asteroid when the game begins.
-- EG:
-- CoordX[roid] = 12000
-- CoordY[roid] = -8000
-- * Finally, we set the radius of the asteroid. Bigger asteroids have more gravity, but also weigh more so move in a "heavier" fashion.
-- EG:
-- roidradius[roid] = 400
-- Now we have declared all necessary gravity variables for the new asteroid, we can create it.
-- EG:
-- a = AddAsteroidWithAttribs(CoordX[roid],CoordY[roid],0.5,0.5,1)
-- a.Owner = 1
-- a.TreeCap = 2
-- a:SetRadius(roidradius[roid])
-- a:Reveal(1)
-- a.Moveable = False
-- any other commands you would like to run on this asteroid...
-- There are 3 different possible gravity behaviours; well-only, and full gravity, and static.
-- The asteroids MUST be created in the correct sections.
-- Please see below examples of all three different classes of asteroids.
-- ***
-- 1. THE BELOW ASTEROIDS HAVE A GRAVITY WELL BUT DO NOT THEMSELVES MOVE
-- ***
-- Asteroid 0 - A Sun
-- gravity variables
roid = 0
AccelerationX[roid] = 0
AccelerationY[roid] = 0
MomentumX[roid] = 0
MomentumY[roid] = 0
density[roid] = 4
CoordX[roid] = 0
CoordY[roid] = 0
roidradius[roid] = 900
-- Creation
a = AddAsteroidWithAttribs(CoordX[roid],CoordY[roid],0.5,0.5,1)
a.Owner = 1
a.TreeCap = 0
a:SetRadius(roidradius[roid])
a:Reveal(1)
a.Moveable = False
a:AddSeedlings(90)
-- ** counter for gravity behaviour divisions, do not remove **
wellonlythreshold = roid + 1
-- **
-- ***
-- 2. THE BELOW ASTEROIDS HAVE A GRAVITY WELL, AND MOVE
-- ***
-- Asteroid 1 - A moving asteroid
-- gravity variables
roid = 1
AccelerationX[roid] = 10
AccelerationY[roid] = 0
MomentumX[roid] = 0
MomentumY[roid] = 6.5
density[roid] = 0.8
CoordX[roid] = 6000
CoordY[roid] = 0
roidradius[roid] = 350
-- Creation
a = AddAsteroidWithAttribs(CoordX[roid],CoordY[roid],0.1,1,1)
a.Owner = 2
a.TreeCap = 2
a:SetRadius(roidradius[roid])
a:Reveal(1)
a.Moveable = False
-- Asteroid 3 - A moving asteroid
-- gravity variables
roid = 4
AccelerationX[roid] = 0
AccelerationY[roid] = 0
MomentumX[roid] = 0
MomentumY[roid] = 6.5
density[roid] = 0.8
CoordX[roid] = 8000
CoordY[roid] = -10
roidradius[roid] = 300
-- Creation
a = AddAsteroidWithAttribs(CoordX[roid],CoordY[roid],0.1,1,1)
a.Owner = 2
a.TreeCap = 2
a:SetRadius(roidradius[roid])
a:Reveal(1)
a.Moveable = False
-- ** counter for gravity behaviour divisions, do not remove **
gravroidsthreshold = roid
-- **
-- 3. THE BELOW ASTEROIDS DO NOT MOVE AND DO NOT HAVE A GRAVITY WELL
-- Asteroid 2 - Invisible to Gravity
-- gravity variables
roid = 2
AccelerationX[roid] = 0
AccelerationY[roid] = 0
MomentumX[roid] = 0
MomentumY[roid] = 0
density[roid] = 0.1
CoordX[roid] = -1200
CoordY[roid] = -3700
roidradius[roid] = 475
-- Creation
a = AddAsteroidWithAttribs(CoordX[roid],CoordY[roid],0.85,0.3,0.77)
a.Owner = 3
a.TreeCap = 4
a:SetRadius(roidradius[roid])
a:Reveal(1)
a.Moveable = False
-- spacer asteroid, used to make sure the level is big enough for asteroids to wander about on long, eliptical orbits. Change to taste.
a = AddAsteroidWithAttribs(55000,5000,0.5,0.5,0.5)
a.Owner = 0
a.TreeCap = 4
a:SetRadius(1)
a.Moveable = False
roidnumber = roid
-- END ASTEROID CREATION
end
function LevelLogic()
-- Zoom the camera
SetCameraZoom(9)
-- *** Set the send distances you want for each asteroid here.
-- *** I know you normally do it in Level Setup but in gravityland we do it here.
GetAsteroid(0).SendDistance = 12000
GetAsteroid(1).SendDistance = 3000
GetAsteroid(2).SendDistance = 5000
-- *** End setting of send distances
while GameRunning() do
-- *** YOUR LOOPED COMMANDS GO HERE *** --
-- *** YOUR LOOPED COMMANDS END HERE *** ---
-- START GRAVITY ENGINE
-- change things below this line at your own peril!!
-- Rate Limiter - necessary to pause gravity simulation if the game is paused.
if GetGameTime() > Timer + 0.0 then
Timer = GetGameTime()
-- Get values for the the array
for i = 0, gravroidsthreshold do
for j = i + 1, gravroidsthreshold do
-- calculate Fgx and Fgy between i and j, then...
Fgx = (G * ((roidradius[i] * roidradius[i]) * math.pi * density[i]) * ((roidradius[j] * roidradius[j]) * math.pi * density[j])) / ((CoordX[j] - CoordX[i])^2 + (CoordY[j] - CoordY[i])^2)
Fgy = (G * ((roidradius[i] * roidradius[i]) * math.pi * density[i]) * ((roidradius[j] * roidradius[j]) * math.pi * density[j])) / ((CoordX[j] - CoordX[i])^2 + (CoordY[j] - CoordY[i])^2)
-- now we have the force of gravity x and y, as a scalar. we must find the direction to point in:
xdiff = CoordX[j] - CoordX[i]
ydiff = CoordY[j] - CoordY[i]
-- find the length of the vector..
vectorlength = math.sqrt(xdiff^2 + ydiff^2)
-- divide the vectors by the length to normalise
NormalisedVectorX = xdiff / vectorlength
NormalisedVectorY = ydiff / vectorlength
-- these normalised vectors are values between 0 and 1 that give us a direction :>
-- ..add the appropriate amount of acceleration
AccelerationX[i] = AccelerationX[i] + (NormalisedVectorX * Fgx / ((roidradius[i] * roidradius[i]) * math.pi) * density[i])
AccelerationY[i] = AccelerationY[i] + (NormalisedVectorY * Fgy / ((roidradius[i] * roidradius[i]) * math.pi) * density[i])
xdiff = CoordX[i] - CoordX[j]
ydiff = CoordY[i] - CoordY[j]
NormalisedVectorX = xdiff / vectorlength
NormalisedVectorY = ydiff / vectorlength
AccelerationX[j] = AccelerationX[j] + (NormalisedVectorX * Fgx / ((roidradius[j] * roidradius[j]) * math.pi) * density[j])
AccelerationY[j] = AccelerationY[j] + (NormalisedVectorY * Fgy / ((roidradius[j] * roidradius[j]) * math.pi) * density[j])
end
end
for pass = wellonlythreshold,gravroidsthreshold do
MomentumX[pass] = MomentumX[pass] + AccelerationX[pass]
MomentumY[pass] = MomentumY[pass] + AccelerationY[pass]
-- MOVE GETASTEROID(PASS) by MomentumX and MomentumY
CoordX[pass] = CoordX[pass] + MomentumX[pass]
CoordY[pass] = CoordY[pass] + MomentumY[pass]
GetAsteroid(pass):MoveTo(CoordX[pass], CoordY[pass])
AccelerationX[pass] = 0
AccelerationY[pass] = 0
end
end
-- END GRAVITY ENGINE
coroutine.yield()
end
end
Edit : before I forget, can i put one Gravity asteroid in a levelmap? or must it be two?