Spherical forums

Sphere Development => Sphere Support => Script Support => Topic started by: Eggbertx on May 09, 2013, 10:58:19 am

Title: (more) 2D platformer issues
Post by: Eggbertx on May 09, 2013, 10:58:19 am
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 (https://dl.dropboxusercontent.com/u/10266670/MLP.zip)
Title: Re: (more) 2D platformer issues
Post by: Flying Jester on May 09, 2013, 07:15:22 pm
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.
Title: Re: (more) 2D platformer issues
Post by: Radnen on May 09, 2013, 08:16:09 pm
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.
Title: Re: (more) 2D platformer issues
Post by: Eggbertx on May 16, 2013, 02:56:38 pm

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.
Title: Re: (more) 2D platformer issues
Post by: Radnen on May 16, 2013, 07:26:24 pm

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.