03-04-2013, 10:10 PM
What it does: It attracts objects to a certain point ('gravitypoint') instead of only pulling them in certain directions like default gravity.
What it has problems with:
Things it can't do yet but are possible with modification of the script:
Suggested by Void in the chat.
[lua]
gravitypoint = Vec2D(200, 0)
gravityxenabled = true
function gravitate(object)
if object.Fixed == false then
local xdist = gravitypoint.x - object.Position.x
local ydist = gravitypoint.y - object.Position.y
if gravityxenabled == true then
local velx = 10 - math.sqrt(math.abs(xdist) / 100)
if object.Position.x > gravitypoint.x then
velx = velx * -1
end
velx = velx * (object.Mass / 10)
--gravitypull.Velocity = Vec2D(velx, gravitypull.Velocity.y)
object.Velocity = Vec2D(object.Velocity.x + velx/10, object.Velocity.y)
print(object.Velocity.x)
end
local vely = 10 - math.sqrt(math.abs(ydist) / 1000)
if object.Position.y > gravitypoint.y then
vely = vely * -1
end
vely = vely * (object.Mass / 10)
--gravitypull.Velocity = Vec2D(gravitypull.Velocity.x, vely)
object.Velocity = Vec2D(object.Velocity.x, object.Velocity.y + vely/10)
print(object.Velocity.y)
end
end
function gravitateAll(object)
local stuff = object:getChildren()
for q = 1, #stuff do
if stuff[q] ~= nil then
if stuff[q].Type == "Box" or stuff[q].Type == "Circle" or stuff[q].Type == "Triangle" then
print(stuff[q].Name)
gravitate(stuff[q])
end
gravitateAll(stuff[q])
end
end
end
while true do
sleep(0.1)
gravitateAll(game.World)
end
[/lua]
What it has problems with:
- When things are close to the center of gravity, they will swing around it wildly. This is because the gravity gets more strong the closer one is to the center, and it overshoots. I'm not completely sure of a really good way to fix this.
- When things bump into other things in the way of the center of gravity, they always bounce away instead of just plowing in and coming to rest. Again, not entirely sure of a plausible fix.
Things it can't do yet but are possible with modification of the script:
- Multiple gravity sources. This could also probably be done with multiple scripts running this code, actually...
- Rotation of characters to always 'stand on' gravity sources, like Mario in Super Mario Galaxy.
Suggested by Void in the chat.
[lua]
gravitypoint = Vec2D(200, 0)
gravityxenabled = true
function gravitate(object)
if object.Fixed == false then
local xdist = gravitypoint.x - object.Position.x
local ydist = gravitypoint.y - object.Position.y
if gravityxenabled == true then
local velx = 10 - math.sqrt(math.abs(xdist) / 100)
if object.Position.x > gravitypoint.x then
velx = velx * -1
end
velx = velx * (object.Mass / 10)
--gravitypull.Velocity = Vec2D(velx, gravitypull.Velocity.y)
object.Velocity = Vec2D(object.Velocity.x + velx/10, object.Velocity.y)
print(object.Velocity.x)
end
local vely = 10 - math.sqrt(math.abs(ydist) / 1000)
if object.Position.y > gravitypoint.y then
vely = vely * -1
end
vely = vely * (object.Mass / 10)
--gravitypull.Velocity = Vec2D(gravitypull.Velocity.x, vely)
object.Velocity = Vec2D(object.Velocity.x, object.Velocity.y + vely/10)
print(object.Velocity.y)
end
end
function gravitateAll(object)
local stuff = object:getChildren()
for q = 1, #stuff do
if stuff[q] ~= nil then
if stuff[q].Type == "Box" or stuff[q].Type == "Circle" or stuff[q].Type == "Triangle" then
print(stuff[q].Name)
gravitate(stuff[q])
end
gravitateAll(stuff[q])
end
end
end
while true do
sleep(0.1)
gravitateAll(game.World)
end
[/lua]