• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Kain5056

[ C++ - SFML ] - Jittering camera!

2 posts in this topic

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. :-)

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0