Jump to content

  • Log In with Google      Sign In   
  • Create Account


[ C++ - SFML ] - Jittering camera!


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Kain5056   Members   -  Reputation: 432

Like
0Likes
Like

Posted 15 June 2013 - 09:13 AM

I'm trying to make a camera function in SFML, but there is something wrong with it and I cannot understand what.

 

Based on this awsomeness, I made a box for the player to be inside, and the camera to follow the box instead of the player. This way the player can make adjustments to their position without interference from the camera movement.

 

I also got the camera to lock to the bottom or the top of the level if the box gets to close to them, and the box to lock on to every platform the player jumps at (if the camera is not top or bottom locked).

 

The problem is, every time the player jumps on a platform without the camera being locked, the whole screen starts jittering up and down.

 

Here is the code. Please don't scream, I'm still learning.

class camera
{
public:

    static camera cameras;
    sf::RectangleShape cam;
    sf::RectangleShape box;
    sf::Vector2f camera_position;
    sf::Vector2f box_position;
    sf::Vector2f last_position;

    bool top_lock;
    bool bottom_lock;
    int speed;
    int destination;

    camera();
    void camera_move( entity * Entity , int total_width , int total_height );
};

camera camera::cameras;

camera::camera()
{
    cam.setSize( sf::Vector2f( startup::screen_control.SCREEN_WIDTH , startup::screen_control.SCREEN_HEIGHT ) );
    cam.setPosition( 0 , 0 );
    box.setSize( sf::Vector2f( 150.f , 300.f ) );
    top_lock = false;
    bottom_lock = false;
    speed = 0;
    destination = 0;
}

void camera::camera_move( entity * Entity , int total_width , int total_height )
{
    speed = Entity->top_speed.y;

    if( box_position.x > Entity->position.x ) box_position.x = Entity->position.x ;
    else if( box_position.x < Entity->position.x + Entity->width - box.getSize().x ) box_position.x = Entity->position.x + Entity->width - box.getSize().x;
    else box_position.x = last_position.x;

    if( !Entity->on_ground )
    {
        if( box_position.y > Entity->position.y ) box_position.y = Entity->position.y ;
        else if( box_position.y < Entity->position.y + Entity->height - box.getSize().y ) box_position.y = Entity->position.y + Entity->height - box.getSize().y;
        else box_position.y = last_position.y;
    }
    else
    {
        if( box_position.y > Entity->position.y + Entity->height - box.getSize().y ) destination = Entity->position.y + Entity->height - box.getSize().y;
        else if( box_position.y < Entity->position.y + Entity->height - box.getSize().y ) destination = Entity->position.y + Entity->height - box.getSize().y;
        else box_position.y = last_position.y;
    }

    if( destination != 0 )
    {
        if( box_position.y > destination - 0.f ) box_position.y -= speed * FPS::FPS_control.last_time;
        else if( box_position.y < destination - 0.f ) box_position.y += speed * FPS::FPS_control.last_time;
        else destination = 0;
    }
    else speed = 0;

    last_position.x = box_position.x;
    last_position.y = box_position.y;

    camera_position.x = ( box_position.x + box.getSize().x / 2 ) - startup::screen_control.SCREEN_WIDTH / 2;

    if( !top_lock && !bottom_lock )
    {
        if( camera_position.y < ( box_position.y + ( box.getSize().y / 2 ) ) - ( startup::screen_control.SCREEN_HEIGHT / 2 ) ) camera_position.y += speed * FPS::FPS_control.last_time;
        else if( camera_position.y > box_position.y + ( box.getSize().y / 2 ) - ( startup::screen_control.SCREEN_HEIGHT / 2 ) ) camera_position.y -= speed * FPS::FPS_control.last_time;
        else camera_position.y = box_position.y + ( box.getSize().y / 2 ) - ( startup::screen_control.SCREEN_HEIGHT / 2 );
    }

    if( camera_position.x < 0 ) camera_position.x = 0;
    if( camera_position.y < 0 ) camera_position.y = 0;
    if( camera_position.x > total_width - cam.getSize().x ) camera_position.x = total_width - cam.getSize().x;
    if( camera_position.y > total_height - cam.getSize().y ) camera_position.y = total_height - cam.getSize().y;

    if( camera_position.y < area::area_control.tile_size / 2 && Entity->position.y + Entity->height <= startup::screen_control.SCREEN_HEIGHT - area::area_control.tile_size ) top_lock = true;
    else top_lock = false;

    if( camera_position.y < area::area_control.total_height - startup::screen_control.SCREEN_HEIGHT + area::area_control.tile_size / 2 && Entity->position.y + Entity->height >= area::area_control.total_height - ( startup::screen_control.SCREEN_HEIGHT - ( startup::screen_control.SCREEN_HEIGHT / 3 ) ) ) bottom_lock = true;
    else if( Entity->on_ground ) bottom_lock = false;

    if( top_lock )
    {
        if( camera_position.y > 0 ) camera_position.y -= speed * FPS::FPS_control.last_time;
        else if( camera_position.y < 0 ) camera_position.y += speed * FPS::FPS_control.last_time;
        else camera_position.y = 0;
    }

    if( bottom_lock )
    {
        if( camera_position.y + startup::screen_control.SCREEN_HEIGHT < area::area_control.total_height ) camera_position.y += speed * FPS::FPS_control.last_time;
        else if( camera_position.y + startup::screen_control.SCREEN_HEIGHT > area::area_control.total_height ) camera_position.y -= speed * FPS::FPS_control.last_time;
        else camera_position.y = area::area_control.total_height - startup::screen_control.SCREEN_HEIGHT;
    }

    box.setPosition( box_position );
    cam.setPosition( camera_position );
}

The problem seems to be at this part:

    if( !Entity->on_ground )
    {
        if( box_position.y > Entity->position.y ) box_position.y = Entity->position.y ;
        else if( box_position.y < Entity->position.y + Entity->height - box.getSize().y ) box_position.y = Entity->position.y + Entity->height - box.getSize().y;
        else box_position.y = last_position.y;
    }
    else
    {
        if( box_position.y > Entity->position.y + Entity->height - box.getSize().y ) destination = Entity->position.y + Entity->height - box.getSize().y;
        else if( box_position.y < Entity->position.y + Entity->height - box.getSize().y ) destination = Entity->position.y + Entity->height - box.getSize().y;
        else box_position.y = last_position.y;
    }

    if( destination != 0 )
    {
        if( box_position.y > destination - 0.f ) box_position.y -= speed * FPS::FPS_control.last_time;
        else if( box_position.y < destination - 0.f ) box_position.y += speed * FPS::FPS_control.last_time;
        else destination = 0;
    }
    else speed = 0;

If someone more experienced than me could help, it would really be appreciated. :-)

Thanks in advance. :-)



Sponsor:

#2 AllEightUp   Moderators   -  Reputation: 4122

Like
1Likes
Like

Posted 15 June 2013 - 09:15 PM

I can't answer the problem other than by suggesting that it is likely an alternation of one of the various if/else/if statements causing the problem.  My suggestion is to add some logging into the code such that if it is alternating between some of the states here, it will become pretty obvious with lots of spammed messages.  Assuming windows, just use OutputDebugString in each of your cases, any other platform you can just use printf.  Even the most simplistic:

 

OutputDebugString( "Case 1" );

 

Repeated for what looks like 9 cases should give you feedback on what is causing the jitters.  My guess is that you need to use inclusive ranges which suggests some of the less than's should actually be less than or equals.  Or perhaps the other way around, depends on how that code works in practice.



#3 Kain5056   Members   -  Reputation: 432

Like
0Likes
Like

Posted 16 June 2013 - 12:01 AM

Thanks for your answer. :-) I tried many variations and values of inclusive ranges but nothing seems to work! The logging gave me data from all over the problematic part, but was not really helpful. Adding equals to the comparisons made no difference, either. :-(

 

The strange thing is that this configuration worked perfectly when I was still using SDL:

if( !Entity->on_ground )
    {
        if( box_position.y > Entity->position.y ) box_position.y = Entity->position.y ;
        else if( box_position.y < Entity->position.y + Entity->height - box.getSize().y ) box_position.y = Entity->position.y + Entity->height - box.getSize().y;
        else box_position.y = last_position.y;
    }
    else
    {
        if( box_position.y > Entity->position.y + Entity->height - box.getSize().y + 2 ) destination = Entity->position.y + Entity->height - box.getSize().y + 2;
        else if( box_position.y < Entity->position.y + Entity->height - box.getSize().y - 2 ) destination = Entity->position.y + Entity->height - box.getSize().y + 2;
        else box_position.y = last_position.y;
    }

...But now it does not work at all!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS