Jump to content
  • Advertisement
Sign in to follow this  
Kain5056

y = mx + b for sloped tiles

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Edited by Kain5056

Share this post


Link to post
Share on other sites
Advertisement

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

Share this post


Link to post
Share on other sites

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.

Edited by Kain5056

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!