How to check for edge of ledge?

So I have ledges in my game like any other platformer. problem is when I am on the very edge of that ledge and am about to fall off, as depicted in the attached video, the character shakes uncontrollably. I was told to do :

if ( this.__hero.body.velocity.y > x ) { } where 'x' is a number > 0

but that has a slight delay before the fall animation starts playing.

How do I detect edges & put a stop to this bug once & for all?

Can someone please show me how they would do it?

Thank you in advance!

Can someone please help me? Getting tired of being ignored.

I don’t think detecting edges will solve the problem. You may have a race condition where changing the body is changing the collision state, which is changing the body and so on.

From the video it looks like the body is cycling between 3 states. 2 of those appear with the same animation frame, the “standing” one. In one state the body is farther to the left. That one is definitely overlapping the ledge.

Something is interrupting the “falling” state.

@samme : if ( this.__hero.body.velocity.y > 100 ) { } seems to work, but the problem is that makes it so the falling animation takes 1 second before it activates. How do I fix this?

So, as I think @samme mentioned, are your player’s body bounds (bounding box) changing size when the sprites’ animations change?

@seyuup : nope.

Ok, and let’s say that you don’t change to the falling animation when on the edge of the platform. Does the bug still occur?

Edit: I guess not since you had to use a condition to delay the animation.

@seyuup : correct.

I’m not sure what could possibly be the cause without having more details, but from rewatching the video you attached, does the sprite ever “leave” the bounds behind when your character runs left or right? I noticed from pausing at the beginning of the video that when you moved your player to the right, the bounds was “left behind”, then it adjusted to the player, but maybe that occurred because there was no collision under the player.

@seyuup : do not believe it does.

But the body offset is changing when the animations change?

@samme : I do not believe so. At least “I” did not set it to be that way.

hello?..

Getting real tired of being ignored here… :frowning:

There’s not enough information here to identify your problem for certain. My best guess is there is some kind of feedback loop going on among the animation state changes, the physics body, and the ledge collision.

The body’s position can change

  • whenever the sprite’s displayWidth or displayHeight changes, such as when changing texture frames
  • when you call body.setSize()
  • when you change the body’s position or offset
  • when you change the sprite’s position

So it could be any of those.

For a workaround, go back to

if ( this.__hero.body.velocity.y > 100 ) { … }

and find the smallest value that works.

Or always keep the body the same size when changing frames:

hero.body.setSize(WIDTH, HEIGHT) // constants