Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Small jitter on slope tiles.  (Read 2983 times)

0 Members and 1 Guest are viewing this topic.

Kain5056

  • Newbie
  • *
  • Posts: 26
    • View Profile
Small jitter on slope tiles.
« on: September 23, 2013, 09:57:38 pm »
I'm trying to implement 45 degrees slopes in my platform game, and I came up with the following code:

Code: [Select]
//position.x, position.y, width and height are the sprite's coordinates and size.

void entity::slope_collision_detection( tile * Tile )
{
    if( position.x + width > Tile->x )
    {
        if( position.y + height > Tile->y )
        {
            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?

Thank you in advance.  :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
AW: Small jitter on slope tiles.
« Reply #1 on: September 24, 2013, 06:11:48 am »
I haven't tried to understand your code snippet, but from your description it seems to be a general collision issue, namely: Stability.
Even though your character stands still, your collision code is running like crazy, thus moving the player around. You probably want to introduce a way to set the character as "stable", thus being able to ignore the collision stuff, with the result that your character stands still. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Kain5056

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Small jitter on slope tiles.
« Reply #2 on: September 24, 2013, 08:55:49 am »
That's what I think, too. But I just don't know how to stabilize the sprite's position.

All the code does is calculating and setting the sprite's y position based on its x position on the slope.
This is the new code, with some redundant parts removed:

Code: [Select]
//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;
}

Tuffywub

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: Small jitter on slope tiles.
« Reply #3 on: September 24, 2013, 10:40:39 pm »
not sure exactly, but maybe your if statement try changing <= to <

currently, once the entity hits the tile, it is set to be = to the tiles height at the spot. Then, the next game loop, the <= means that the entity will still intersect with the tile, and will be reset to the same spot every time. This could cause jittering if you have gravity.

So either set the height of the entity to be a little above the tile (+ 0.001) or just change the if statement to use <.

Kain5056

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Small jitter on slope tiles.
« Reply #4 on: September 25, 2013, 12:25:28 am »
I have tried many variations of both with no luck. I subtracted and added varying values, changed the comparison at the if statement to everything I could think of, but the jittering persists.  :(

Kain5056

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Small jitter on slope tiles.
« Reply #5 on: September 25, 2013, 12:04:37 pm »
I almost figured it out. :)

Code: [Select]
//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. :)

 

anything