Archived

This topic is now archived and is closed to further replies.

s_cloudx

How do I make a 2d side-scrolling platform game?

Recommended Posts

I'm trying to make a 2d side-scrolling platform game (a la castlevania). How do they make the character jump? stand on the platform? I am having trouble implementing them. Can someone here, teach me how? or maybe point me to a tutorial. I would be very grateful to any advice. Thanks in advance! Edited by - s_cloudx on February 5, 2002 9:21:16 PM

Share this post


Link to post
Share on other sites
Ignore the many assholes who frequent this board.

If your main character has a "world y-coordinate" (preferrably at his feet) and every surface/floor has one of its own, then for a character to be standing on a surface he should be located at the given surface''s y-coordinate. Jumping is a question of temporarily incrementing and then decrementing the character''s y-coordinate.

A good approximation is to use a function like y = b - x2 over the interval [-sqrt(b), +sqrt(b)]. By that I mean choosing, say, to start the jump time at -3 and incrementing it until it gets to 3. In that case, the jump function would be jump_y = 9 - (jump_time * jump_time).
jump_time | jump_y
-3 | 0
-2 | 5
-1 | 8
0 | 9
1 | 8
2 | 5
3 | 0

As you can see, adding jump_y to your character''s y-coordinate would give you a nice non-linear jump, realistically slowing down near the top and then speeding up again on the way down.

If this isn''t clear enough, post back and point out any areas that may be confusing.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Actually, to do it the Mario way, this is probably a good way to do it... do this each frame:

if( Key[KEY_JUMP] is pressed )
{
Player.Speed.y = -10;
Jumping = true;
}

if( Jumping )
{
Player.JumpDist += 10;
if( Player.JumpDist >= 80 ) // max jump height
Jumping = false;
}
else // Falling
{
Player.Speed.y += Gravity;
Player.y += Player.Speed.y;
}


~CGameProgrammer( );

Share this post


Link to post
Share on other sites
At last! I was able to get my character to jump. But I did it in a different way. You guys gave me the idea. Anyways, from memory, this is my code:

//Possible actions the player can do
enum PlayerAction = {WALKING, RUNNING, JUMPING, FALLING ...}

struct
{
//location of character
int x,y;

//jump related valuables
int Gravity;
int maxJumpHeight;

PlayerAction PA;

} Player;

//Default gravity. change this to make an anti-gravity room or
//heavy gravity room.
int Default_Gravity = 2;

void Init_Player()
{

//initialize the current gravity to the default
Player.Gravity = Default_Gravity;

//the maximum height in pixel the character can jump
Player.maxJumpHeight = -10;

}

void Controls()
{

//if player pushes Jump button
if (Key(JUMP_BUTTON))

//make sure we don't double jump when we reach our maximum jump
if (Player.PA != FALLING)
{

//set the player to jumping
Player.PA = JUMPING;

//If the Player is still starting his jump, decrease by 2
//else decrease by one. this will slow his jump.
if (Player.Gravity > 0)
Player.Gravity -= 2;
else
Player.Gravity --;

//skip the rest of the function to speed things up
return;
}

//if player releases Jump button while the player is jumping
if (!Key(JUMP_BUTTON) && Player.PA == JUMPING)
{

//he falls
Player.PA == FALLING;
}

//the player is falling
if (Player.PA == FALLING)
{

// if the player's gravity is negative, fall slowly
// otherwise fall fast

if (Player.Gravity < 0)
Player.Gravity ++;
else
Player.Gravity += 2;

//skip the rest of the function to speed things up
return;
}

//the player isn't jumping or falling
if (Player.PA != JUMPING || Player.PA != FALLING)

//reset the gravity
Player.Gravity = Default_Gravity;
}

void Apply_Gravity()
{

// if the gravity is positive, the player will fall.
// otherwise he will rise.
Player.x -= Player.Gravity;

// if the player has reached the highest point he can jump
if (Player.Gravity < Player.maxJumpHeight)
{
Player.PA = FALLING;
}

}

//this is called every frame
void UpdateFrame()
{

Controls();

ApplyGravity();

//this function will check the AI, logic, etc.
Logic();

//this function will draw the sprites, background, character, etc.
Render();

}

--------

what do you guys think?


P.s. Maraming salamat sa tulong nyo (Thank you very much for all your help!)


Edited: Forgoten UpdateFrame. Added UpdateFrame()

Edited by - s_cloudx on February 9, 2002 8:11:08 PM

Share this post


Link to post
Share on other sites