Skip to main content

News

Topic: (more) 2D platformer issues (Read 5640 times) previous topic - next topic

0 Members and 1 Guest are viewing this topic.
(more) 2D platformer issues
Hello everyone. I'm still trying to make a 2D platformer, and while I've made a fair bit of progress on it, I'm still running into issues with gravity and ground detection. When you jump, the character goes up, and falls back down, but when it reaches the ground, instead of stopping, it goes down into the ground for about half a second before the "burying" prevention kicks in and pushes it out of the ground. Height seems to have nothing to do with it, since if you walk off a ledge without jumping, it hits the ground as expected. The problem exists somewhere in scripts/SideScrollingEngine/base.js, in Character.update(), Character.gravityCheck(), Character.moveCheck(), or Character.onGround(), but I've been unable to find the exact cause. Any ideas?
You can download the game here

Re: (more) 2D platformer issues
Reply #1
When a character hits the ground is one of the trickiest parts of gravity.

Can you describe the process you use to check for obstructions? I'll have a look at the code, but (in my experience) it's hard to figure out these kinds problems without a lot of careful reasoning and even more trial and error.

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: (more) 2D platformer issues
Reply #2
You calculate fall speed going down and check a 'ground line'. Say it's in pixels. If you are falling down at 6.345 pixels (fairly fast), check if it crosses the ground line. So,

Code: (js) [Select]

if (player.y + 6.345 > ground) { /**/ }


if that is true, set player.y = ground (or otherwise his torso will be in the ground, and we wouldn't want that!). This is not too hard to do in a custom engine since for every platform all you need to do is keep track of it's 'ground' y-axis values. In Sphere's map engine we can do a simpler check, but we still need to know something about the ground line:

Code: (js) [Select]

if (IsPersonObstructed(name, player.x, player.y+6.345)) {
  player.y = ground_line;
}


Try it out with a constant value first (all tiles on platform are the same height).

The other solution is a tad more computational, but requires no knowledge about the ground line. The algorithm is similar to the top, but requires calling many obstruction checks:
Code: (js) [Select]

if (IsPersonObstructed(name, player.x, player.y+6.345)) {

  // we overshot the ground somewhere, backtrack until we are on top:
  for (var i = 1; i <= 6; i++) {  // 6 from Math.floor(6.345)
    var y = player.y + (6 - i); // 6 from Math.floor(6.345)
    if (!IsPersonObstructed(name, player.x, y)) player.y = y;
  }
}


I haven't tested the code, it's purely theory.
  • Last Edit: May 09, 2013, 08:26:05 pm by Radnen
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: (more) 2D platformer issues
Reply #3

When a character hits the ground is one of the trickiest parts of gravity.

Can you describe the process you use to check for obstructions? I'll have a look at the code, but (in my experience) it's hard to figure out these kinds problems without a lot of careful reasoning and even more trial and error.

So I've noticed :/
For obstructions, it just calls Sphere's IsPersonObstructed()


You calculate fall speed going down and check a 'ground line'. Say it's in pixels. If you are falling down at 6.345 pixels (fairly fast), check if it crosses the ground line. So,

Ah, so should I create a function then to, given X, find the Y position of the ground, as opposed to relying on Sphere's obstruction checking?

Code: (js) [Select]

if (IsPersonObstructed(name, player.x, player.y+6.345)) {
  player.y = ground_line;
}

Try it out with a constant value first (all tiles on platform are the same height).

That's a little bit like what it already does, though I was hoping I could have it so that platforms don't necesarily have to be completely flat, i.e. you could have a sloped or uneven platform. I guess I could use this method still, as long as it also checks where ground_line is at every frame.

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: (more) 2D platformer issues
Reply #4

Ah, so should I create a function then to, given X, find the Y position of the ground, as opposed to relying on Sphere's obstruction checking?


Well, if you use the other method, you can rely on Sphere's obstruction checking. It will even support sloped lines, etc.
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here