y = mx + b for sloped tiles

Started by
11 comments, last by Kain5056 10 years, 6 months ago

Oh, I figured it out a few days ago, sorry I forgot to post it. wacko.png


bool entity::slope_collision_detection( tile * Tile )
{
    float y1 , y2 , x1 , x2 , m , b , x;
    y1 = Tile->y;
    y2 = Tile->y + tile_size;
    x1 = Tile->x;
    x2 = Tile->x + tile_size;
    m = ( y2 - y1 ) / ( x2 - x1 );

    if( Tile->tile_type == slope_right )
    {
        if( last_position.x + width - collision_box_position.x - FPS::FPS_control.last_time >= Tile->x + tile_size ) return false;
        m *= -1;
    }

    if( Tile->tile_type == slope_left )
    {
        if( last_position.x + collision_box_position.x + 1 + FPS::FPS_control.last_time <= Tile->x ) return false;
    }

    if( last_position.y + height - collision_box_position.y - 1 + FPS::FPS_control.last_time >= Tile->y + tile_size ) return false;

    b = y1 - m * x1;
    x = position.x + collision_box_position.x * m;


    if( position.y + height >= m * x + b + 1 )
    {
        on_ground = true;
        position.y = m * x + b - height + 1 - FPS::FPS_control.last_time;
        return true;
    }
    return true;
}

The additions and subtractions of 1 and FPS::FPS_control.last_time ( = time step ) are added so the sprite does not jitter, and for the slopes to line up perfectly to each other, so the sprite does not jump when traversing multi-tiled slopes. smile.png

The code works perfectly for me. smile.png

Advertisement

Use fixed timestep to avoid problems later on if a frame gets by some reason rendered even a few milliseconds slower than before.

FPS::FPS_control.last_time is my delta time as seconds * 32. I also use it for the entities movement and collision detection, so I only include it here because the sprite jitters on the slope without it I'm not really sure what you mean above.

I'm also planning to add the options to enable Vertical Sync and FPS Lock at 60 FPS in the Options Menu. Does that help?

I don't really want to permanently lock the FPS, though, because I want to always test the performance with a build-n FPS counter.

This topic is closed to new replies.

Advertisement