• Create Account

# Tutorial for making a side-scrolling gravity character

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1P0jahn  Members   -  Reputation: 303

Like
0Likes
Like

Posted 29 December 2012 - 01:04 PM

I want to create a character for my game that is similar to Meatboy(or just Super Mario Bros if you are not familiar with Meatboy).

That is, a character that can jump, and fall after a while, a character that can do walljumps and wallslide, a character that will fall when "he" walk on a cliff(hollow space) and all that stuff. This seems like a very difficult challenge. I have been searching for a tutorial but I cant find anything.

Anyone have any ideas?

### #2Khatharr  Crossbones+   -  Reputation: 5299

Like
1Likes
Like

Posted 29 December 2012 - 01:13 PM

How familiar are you with Newtonian physics? (Specifically the nature of force and inertia.)

Apart from basic physics it's just a matter of good collision detection.

http://www.gamedev.net/page/resources/_/technical/game-programming/the-guide-to-implementing-2d-platformers-r2936

I found it to be a good article.

Edited by Khatharr, 29 December 2012 - 01:16 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #3P0jahn  Members   -  Reputation: 303

Like
0Likes
Like

Posted 28 January 2013 - 04:17 PM

Bumping this.

For the past week I have been trying to implements gravity into my game.

I have read a lot of tutorials, but none of them are really suitable for my game.

Its not the actual jumping algorithm I have trouble with, its the other features. Well, the jumping algorithm is messy too.

The features I want:

1. When pressing the jump button, do not jump to the maximum height immediately. Instead, you have to hold the jump button to jump higher(and respect a limit). This feature would allow short-jumps and give you more control over your character.

2. Activate gravity if you walked down from a cliff for example(i e, you are in the air without have jumped).

(Detecting whether or not I am in air is not a problem, but where to put the code is).

3. After you released your jump button, you can not jump again until you hit the ground.

None of these are not really hard to implement, but combining all these function is hard.

Like any other 2D game, the top-left corner of the screen have coordinates 0,0, and both increases when moving right/down.

I have created a bit of code but it needs to be rewritten for the tenth time.
I will show it tomorrow, but until then, I hope to get some ideas.

### #4MajorTom  Members   -  Reputation: 721

Like
0Likes
Like

Posted 28 January 2013 - 07:02 PM

Instead of looking for a tutorial that matches your exact requirements, you can gain a lot more by solving this yourself.

1. When pressing the jump button, do not jump to the maximum height immediately. Instead, you have to hold the jump button to jump higher(and respect a limit). This feature would allow short-jumps and give you more control over your character.

How do you think this could be achieved? If you know the maximum jump height and specify a jump speed, you can increment the characters height by the speed until they've reached the height limit - whilst the jump button is pressed. And when the jump button is released, the player falls. Once you've done this, you could improve the "feel" of the jump, by changing the characters jump height to increase by smaller and smaller amounts over time - rather than a constant speed. This will create a slightly floaty/hovery jump depending on the curve/falloff.

2. Activate gravity if you walked down from a cliff for example(i e, you are in the air without have jumped).

(Detecting whether or not I am in air is not a problem, but where to put the code is).

So you know the solution to the problem, but don't know where to put the code? You could place this code just after the input if you'd like.

(Though I'd suggest you place the physics code in the same area)

3. After you released your jump button, you can not jump again until you hit the ground.

You know when the jump button has been released, so you'll need to know when you've hit the ground. If you're using tiles/grid, check the tile below you is a floor/ground tile. If you're touching this tile, you've hit the ground. Reset the jump ability here.

None of these are not really hard to implement, but combining all these function is hard.

At this stage, solve the problems one at a time.

Additionally, I'd like to add that it took a long time for them to get the movement mechanics right, with lots of tweaking. You would probably benefit from making a simpler jump system first, and then improving that once it's complete.

I will show it tomorrow, but until then, I hope to get some ideas.

Looking forward to it

Saving the world, one semi-colon at a time.

### #5Nyssa  Members   -  Reputation: 426

Like
0Likes
Like

Posted 28 January 2013 - 07:04 PM


Vector2 position(0.0, 0.0);
Vector2 velocity(0.0, 0.0);
Vector2 force(0.0, 0.0);
float mass = 1.0;
float gravity = -9.8;
float damping = 0.001f;

while(true)
{
float dt = 0.016;//time elapse per frame in seconds

//Do game logic here

position = position + velocity * dt;

velocity *= 1.0 - (damping * dt);

force.y += mass * gravity;

velocity = velocity + (force / mass) * dt;
force = 0;
}


1. When pressing the jump button, do not jump to the maximum height immediately. Instead, you have to hold the jump button to jump higher(and respect a limit). This feature would allow short-jumps and give you more control over your character.

To the algorithm above, add some value to force.y once. If you hold down a key then continue to add a force over multiple frames.

2. Activate gravity if you walked down from a cliff for example(i e, you are in the air without have jumped)

The algorithm above with handle this automatically with gravity. Change the gravity value to increase the acceleration.

3. After you released your jump button, you can not jump again until you hit the ground.

For this you'll need to add collision detection and response, and may get tricky.

Edited by Nyssa, 28 January 2013 - 08:59 PM.

### #6Khatharr  Crossbones+   -  Reputation: 5299

Like
0Likes
Like

Posted 29 January 2013 - 08:44 AM

I wasn't joking about physics. Gravity and jumping force can very easily be handled as classical Newtonian forces.

If you understand the terms 'force', 'acceleration', 'velocity', 'terminal velocity', 'speed' and 'momentum' in the sense that they are used in physics then this should be very easy to implement.

If you don't know them in the scientific sense then spend a few hours getting to understand those concepts well, especially the relationship between force, acceleration and speed. Once you understand those concepts you can apply them directly to this kind of physics.

The only queer thing about jumping force is that the application of the force is dependent on whether the button is still pressed. When I did platformers I would have a 'jump_potential' variable that specified the amount of jumping force available. When the jump button was pressed then each frame I'd apply a set fraction of the jump_potential to the character as an upward force. (You can avoid asymptotic behavior in this case by simply using integer division.) If the button was released then the jump_potential would be emptied. If the character was standing on something (and the button was not pressed) then the jump_potential would be reset to its start value.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

### #7P0jahn  Members   -  Reputation: 303

Like
0Likes
Like

Posted 29 January 2013 - 01:49 PM

An update, it is going pretty well this time. I am using a modified version of Nyssas code.

I have a problem thought, with slopes.

When I run down from a cliff, the gravity work as expected, it pulls me down until I hit the ground.

But when I move down from a slope, I dont want to use the gravity/pushdown effect. Instead I just want to move the character down manually(increasing its y variable). Right now, when I move down from a slope, the character is not touching the slope, he just fly over it.

To solve this, I could just set the gravity to a very high value when I am standing on the slopes, but how do I detect that?
My map is a 2D char array, where 0 is solid and 1 is hollow.

### #8Nyssa  Members   -  Reputation: 426

Like
0Likes
Like

Posted 29 January 2013 - 06:20 PM

Since you're using a char array it shouldn't take too much to detect if a slope is coming:

int tilePositionX, tilePositionY;
char tileArray[mapSizeX][mapSizeY];

int nextXTile = tilePositionX+1;//+1 assuming you are moving forward
int yTileBelow = tilePositionY+1;

if ((nextXTile < mapSizeX) && (yTileBelow < mapSizeY))
{
//where 0 is solid and 1 is hollow
if (tileArray[nextXTile][yTileBelow]) == 1)
{
//the next tile is on a slope or there is a hole
}
}

Edited by Nyssa, 29 January 2013 - 06:21 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS