Platformer: Jumping /gravity


HeXy

Still Fresh
Joined
Jul 3, 2006
Messages
32
Hiya all,

I've started very early work on a platform game in Fenix (yay!).

I've used the wonderful source from FenixOnFire: Tiles by Moogle as a base for it, as its level editor and scrolling/collisions (and animation!) would save me a ton of work.

The original source is for an 8-way top-down movement (as you may know).

I've changed this by simply altering the controls (up/down for movement become left/right and the rotating by using left/right has been remmed out).

Naturally, the next step is to get jumping & falling in there. I've had a go at it but it all seems to happen way too quickly :(

I set 2 new local variables up: jh (jump height) and jg (a flag set to 1 when max jump height is reached).

The theory is, that on pressing A (control) with jump height less than 50, the ys variable in Tiles is adjusted upwards (my understanding is that xs & ys are the position of the player). On reaching 50, the jg flag is set, and js and ys are incremented back down (thus completing the jump via gravity).

my code for this is;
CODE
//player wants to jump
while(key(_control) and jh<50 and jg!=1 )
//move!
while(jh<50)
jh += 1;
ys += -2;
end
//walking sequence
graph = ((graph - 3)+1) % 8 +3;

//and of course not standing anymore
standing = 0;
end

//player has finished jumping, initiate fallmode
while(jh>0 and jg==1)
jh += -1;
ys += 2;
end

//player not jumping, set fallmode off
if(jh==0)
jg=0;
end

//top of jump, so allow to fall
if(jh==50)
jg=1;
end



This code is added directly after the collisions bit of the original Tiles source.

I figured that once this is sorted, I can look at ensuring the player "lands" on a platform - as currently if a player were to jump onto a lower platform, on jump completion, the player would land in mid air :(

Am I going the right way with this? Do you think using Tiles as a base was a good idea?

If it helps, the current source is available here (please excuse the terrible sprites, I tend to develop the engine first n work on the graphics later).

Cheers for any help :)

HeXy
 
Not in the mood to figure you a good jumping engine here with acceleration n stuff, but you can check the GP32 file archive for Castle Of Doctor Malvado, a pretty decent jump&run written in Fenix with sources included (as far as I remember) - tho I fear they are commented in spanish...

Doesn`t have that Fire Whip demo or whatever it is called source included, too? I can`t really remember...
You can also check through the Booleansoup archive (look for the link in the Language Advice thread you made) for Jump&Run games... don`t really know if there are any with source included, but chances are good.
 
I had a quick look and I can see your problem. It is performing the jump but you aren't actually displaying it by using the frame; command. Basically everything within the 'while' loops is being performed before it reaches a frame; command to perform the jump. I think you will need to re-think your solution to this as the way you currently have it implemented the player can only jump up and down but wont move horizontaly whilst doing so.
 
the way i learnt to do this was using hardness maps, basically, create a copy of your level, change it to grey scale and then colour all the floors one colour, say colour 120. This map is then run in the background (so its not seen) and can be used to detect if the player is stood on a floor on not (can also put walls in too).

the code should be something like this (im writing this off the top of my head, so might not be perfect)

CODE


gravity = 10;
jump = false;

//then inside the player process
loop

while ((map_get_pixel(0,100,x,y) <> 120) AND jump == false)
y += gravity;
frame;
end

if(key(_control))
jump = true;
end

while (jump == true)
y-=gravity;
gravity --;
if (gravity < 0)
jump = false;
gravity = 10;
end
frame;
end

frame
end



or something along those lines, basically, use the map_get_pixel function (change the numbers in the bracket to match your fpg and hardness map) to check if the player is standing on the floor, and if he isnt, make him fall down (you can make up your own gravity acceleration if u like). When u jump u can esentially reverse the gravity to make the player go up. I used to have a tutorial on this, but that was back in the days of DIV and the website is no-more now.
 
i don't know anything about fenix, but a good basic jumping method would be:



If JumpButtonPressed then
YIncrement = JumpPower;
XIncrement = XSpeed;

While(Not a Collision)
{
YPos -= YIncrement;
XPos += XIncrement;
YIncrement -= Acceleration;
If (YIncrement>MaxSpeed) YIncrement = MaxSpeed;
else If (YIncrement<-MaxSpeed) YIncrement = -MaxSpeed;
}



This way you get a nice smooth jump, as opposed to up then down suddenly. (Its late, that psuedo-code may not be error free, but hopefully you get the idea...)
 
Geuben said:
...
This map is then run in the background (so its not seen)...
You don`t even need to run/place it in the background as long as the map is created and in memory, you have
access to it. The x and y you use in map_get_pixel are relative to the map, not the gp2x screen. Saves you one process of rendering the whole map.

Just in case you didn`t know, maybe I just misunderstood you.
 
Last edited by a moderator:
Geuben said:
the way i learnt to do this was using hardness maps, basically, create a copy of your level, change it to grey scale and then colour all the floors one colour, say colour 120. This map is then run in the background (so its not seen) and can be used to detect if the player is stood on a floor on not (can also put walls in too).

the code should be something like this (im writing this off the top of my head, so might not be perfect)

CODE
..snip..




or something along those lines, basically, use the map_get_pixel function (change the numbers in the bracket to match your fpg and hardness map) to check if the player is standing on the floor, and if he isnt, make him fall down (you can make up your own gravity acceleration if u like). When u jump u can esentially reverse the gravity to make the player go up. I used to have a tutorial on this, but that was back in the days of DIV and the website is no-more now.


You just boosted my game development by 1001%! Thanks! I was looking for a good way to check for level collisions, and you hit the spot right there. The same principle is used in FireWhip, but again, this is in Spanish so I couldn't make much sence of it.
 
Last edited by a moderator:
QUOTE
You don`t even need to run/place it in the background as long as the map is created and in memory, you have
access to it. The x and y you use in map_get_pixel are relative to the map, not the gp2x screen. Saves you one process of rendering the whole map.

Just in case you didn`t know, maybe I just misunderstood you.


yeah i that what i meant. just didn't explain it too well.
 
My personnal recommandation is use a static array with your pixel moving.
When your sprite go up, increase the position in this array and when your sprite go down decrease the array position.
With an array you are always sure to return to the original position.

;)
 
Why would you want to return to the exact same position as before the jump o_O That would only be the case when jumping up on normal ground while not moving... (only reason I can imagin is jumping under a Mario-like ?-Block, and even then it`s not necessary to land in the same place again... which would also work with a good algorithm of jumping...
 
Back
Top