2DWorlds Forums
Partially-complete gravity well script! - Printable Version

+- 2DWorlds Forums (http://2dworlds.buildism.net/forum)
+-- Forum: 2DWorlds (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=4)
+--- Forum: 2DWorlds Discussion (http://2dworlds.buildism.net/forum/forumdisplay.php?fid=10)
+--- Thread: Partially-complete gravity well script! (/showthread.php?tid=673)

Pages: 1 2


Partially-complete gravity well script! - Ghosty - 03-04-2013

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:
  • 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]


RE: Partially-complete gravity well script! - Oak - 03-05-2013

Useless.
At least make something useful like helping me with my game.
=|


RE: Partially-complete gravity well script! - Ghosty - 03-05-2013

Useless?

I can tell you've never played Super Mario Galaxy then.


RE: Partially-complete gravity well script! - Oak - 03-05-2013

(03-05-2013, 12:55 AM)Ghosty Wrote: Useless?

I can tell you've never played Super Mario Galaxy then.

Well your inner-instincts must not be functioning properly then.


RE: Partially-complete gravity well script! - Leonartist - 03-07-2013

How could this script be applied to a game?


RE: Partially-complete gravity well script! - Oak - 03-07-2013

I think you'd copy and paste the script into an object such as a triangle or box in the editor, well this is at least what I'd do.


RE: Partially-complete gravity well script! - Ghosty - 03-09-2013

While it would work if you put it inside an object, it wouldn't do anything involving that object without some modifications. Right now, it's static -- you'd have to make a fixed object, then change the first line of the script to the position of that object.


RE: Partially-complete gravity well script! - Omega - 03-10-2013

I think this pretty neat, Gravity sounds like a cool idea if you're making an SMG game or maybe a solar system of sorts. Well done!

I just have a question- is there a way you could make the script have a limit on how far the gravity goes, so I could kind of make an atmosphere environment instead of just massive gravity..?


RE: Partially-complete gravity well script! - Oak - 03-10-2013

(03-10-2013, 05:31 AM)Omega Wrote: I think this pretty neat, Gravity sounds like a cool idea if you're making an SMG game or maybe a solar system of sorts. Well done!

I just have a question- is there a way you could make the script have a limit on how far the gravity goes, so I could kind of make an atmosphere environment instead of just massive gravity..?

Usually what I do is play around and experiment with the script if I wanna modify something, of course in the editor. I know this answer is stupid, and I might be wrong.. But I think you have to lower the number which is currently at 10 where it says: " velx = velx * (object.Mass / 10)
--gravitypull.Velocity = Vec2D(velx, gravitypull.Velocity.y)".

Like I said though, I'm not too sure.


RE: Partially-complete gravity well script! - Omega - 03-10-2013

You think that lowering the gravity on an object will make it have a certain border? I mean, that makes sense, but I want it to be as though, say, I have a gravity force of 25, and not 10 (for a super heavy/dense atmospheric planet) but I also want a cut off point to where it won't be screwing with other planets or things. If I just set the gravity to lower, the place where the player will stand will be so weak you just can't stabilize.

So, kinda like a "Border Feature" without super-weak gravity at the player's position.