# Small jitter on slope tiles.

This topic is 1578 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to implement 45 degrees slopes in my platform game, and I came up with the following code:

//position.x, position.y, width and height are the sprite's coordinates and size.

void entity::slope_collision_detection( tile * Tile )
{
if( position.y >= Tile->y + ( tile_size - ( position.x + width - Tile->x ) ) - height )
{
position.y = Tile->y + ( tile_size - ( position.x + width - Tile->x ) ) - height;
}

if( position.y + height <= Tile->y ) position.y = Tile->y - height;
}


This code is for the left-pointing slope, btw.

It works great, but the player sprite jitters just a little bit when it's standing still on the slope.
It's not very noticeable, but it's really annoying.

I have tried many things to get rid of it, like changing the order of the collision, moving and displaying functions, or tinkering with the equations, but nothing seems to work.

Can anyone more experienced point me to what I am doing wrong here?

Edited by Kain5056

##### Share on other sites

Try using greater than, and less than for both comparisons, as opposed to greater-than/equals-to and less-than/equals-to.

##### Share on other sites

I tried it with no luck. I also tried adding and subtracting small values to both and either the if statement and the entity's y position varying from 0.0001 to 1. Did not work, either.

##### Share on other sites

maybe it's your physics calculations that make the jittering ? or do you use round() somewhere ? (if it's the case, you'd better use floor())

...or put all your coordinates in double, if your world is very large ? (float isn't that precise with huge numbers)

##### Share on other sites

I almost figured it out.

//position.x, position.y, width and height are the sprite's coordinates and size.
//float time_step = delta_time->asSeconds() * 32.f;

void entity::slope_collision_detection( tile * Tile )
{
if( position.y >= Tile->y + ( tile_size - ( position.x + width - Tile->x ) ) - height + 1 )
{
position.y = Tile->y + ( tile_size - ( position.x + width - Tile->x ) ) - height + 1 - time_step;
}
else if( position.y + height < Tile->y + 1 ) position.y = Tile->y - height + 1;
}


I had to add 1 and subtract the time_step variable because it's the exact distance that I use to check for collisions. It's a bit complicated how I do that, but it works great.

The sprite still jitters, but only when its y position is exactly inside (not above or below) the top pixel of the slope tile.

But I think I can figure this one out.

Thanks for the help.