Poll:
You do not have permission to vote in this poll.
Total 0 vote(s) 0%
* You voted for this item. [Show Results]

Partially-complete gravity well script!
#1
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]
[Image: iwn8gk.gif]
Reply
#2
Useless.
At least make something useful like helping me with my game.
=|
[Image: m2m2gSSDCz-2.png]

Well I WAS the walrus.
Reply
#3
Useless?

I can tell you've never played Super Mario Galaxy then.
[Image: iwn8gk.gif]
Reply
#4
(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.
[Image: m2m2gSSDCz-2.png]

Well I WAS the walrus.
Reply
#5
How could this script be applied to a game?
http://2dworldsfansite.weebly.com
[SPOILER][Image: NDIBirc.gif][/SPOILER]
Reply
#6
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.
[Image: m2m2gSSDCz-2.png]

Well I WAS the walrus.
Reply
#7
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.
[Image: iwn8gk.gif]
Reply
#8
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..?
[spoiler] [spoiler] [spoiler] [spoiler] [spoiler] [spoiler] [spoiler] [spoiler] [spoiler] [spoiler] [spoiler] [spoiler] ɥɔǝǝʌpuɐs ǝʞıן ı ıoq ɥɐǝʎ [/spoiler] [/spoiler] [/spoiler] [/spoiler] [/spoiler] [/spoiler] [/spoiler] [/spoiler] [/spoiler] [/spoiler] [/spoiler] [/spoiler]
Reply
#9
(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.
[Image: m2m2gSSDCz-2.png]

Well I WAS the walrus.
Reply
#10
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.
[spoiler] [spoiler] [spoiler] [spoiler] [spoiler] [spoiler] [spoiler] [spoiler] [spoiler] [spoiler] [spoiler] [spoiler] ɥɔǝǝʌpuɐs ǝʞıן ı ıoq ɥɐǝʎ [/spoiler] [/spoiler] [/spoiler] [/spoiler] [/spoiler] [/spoiler] [/spoiler] [/spoiler] [/spoiler] [/spoiler] [/spoiler] [/spoiler]
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)