gravity_loop()
set background = 1
while(src)
if(jumping) //if Jumping...
on_ground = 0
vel_y -= gravity
if(vel_y > 0)
step(src,NORTH,vel_y)
set_dir(direction)
else if(vel_y <= 0)
if(!step(src,SOUTH,-vel_y))
set_dir(direction)
jumping = 0
else if(step(src,SOUTH,1)) //if falling...
vel_y -= gravity
step(src,SOUTH,-vel_y)
set_dir(direction)
else if(!on_ground) //hit ground
if(jumping) jumping = 0
if(vel_y != 0) vel_y = 0
set_dir(direction)
on_ground = 1
sleep(world.tick_lag)
EDIT: Seems I sorted the glitchy looking behaviour when I edited my code just now after spotting something. Still, any advice on improvements/errors spotted would be welcome.
This is my little gravity loop, it works, although it looks a little glitchy when my mob is falling (from stepping off a platform), as opposed to the smooth looking drop when he jumps off a platform. The only reason I can see why this might be happening is the if(step(src,SOUTH,gravity)), just wondering if there is a better way to go about this?
The current setup is to have this running when the player logs in. Hitting the Jump key will set vel_y to 15, the gravity loop takes care of the rest.
Please point out any other errors or inefficiencies, still getting the hang of platformer physics.
Basically, I rigged up a bounding box to hang out below the object with falling physics attached:
And then I check if the length of the colliding objects list is zero after each move. If it's zero, I kick in a loop that applies velocity to the object in a southerly direction, accelerating over time. Gravity is applied per-tick to the existing velocity vector, and the object falls until it is grounded again.
But I never have a gravity loop running on an object that isn't actually falling, so it's a bit more efficient than your design.
Note: This uses DatumPoint, and CodeHooks, two libraries I have written.