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

Author Topic: [Solved] Scrolling a view is creating odd sprite shaking  (Read 3343 times)

0 Members and 1 Guest are viewing this topic.

XDest

  • Newbie
  • *
  • Posts: 8
    • View Profile
[Solved] Scrolling a view is creating odd sprite shaking
« on: January 29, 2011, 09:42:05 pm »
This is a problem that I've been trying to come back to for the last couple of weeks, and haven't figured it out. I'm trying to smoothly scroll the view when the player moves past a certain boundary in the view. The problem is, when the view and the sprite are both moving at once, it causes the sprite (yet not anything in the background in the same view) to "shake". It looks like it's teleporting slightly. When moving the sprite without moving the view, it does not shake, when moving the view and not the sprite, it does not shake. I am using SFML 2.0.

Code: [Select]
   // Fixed!
    // Set up boundaries based on the "Camera" View  
    double rightbound=Camera.GetCenter().x+(Camera.GetSize().x/3);
    double leftbound=Camera.GetCenter().x-(Camera.GetSize().x/3);
    double lowerbound=-Camera.GetCenter().y-(Camera.GetSize().y/3);
    double upperbound=-Camera.GetCenter().y+(Camera.GetSize().y/3);

    double camerax=x-oldX, cameray=y-oldY;

    if (!camerax)   camerax=15;
    if (!cameray)   cameray=15;
    if (camerax<0)  camerax=-camerax;
    if (cameray<0)  cameray=-cameray;
   
    // The x and y coords here are the ones my entity uses, which it passed directly to its sprite.
    if (y>upperbound)   Camera.Move(0,-cameray);
    if (y<lowerbound)   Camera.Move(0,cameray);
    if (x>rightbound)   Camera.Move(camerax,0);
    if (x<leftbound)    Camera.Move(-camerax,0);

    oldX=x; oldY=y;

XDest

  • Newbie
  • *
  • Posts: 8
    • View Profile
[Solved] Scrolling a view is creating odd sprite shaking
« Reply #1 on: February 21, 2011, 06:41:07 am »
Okay, this is as minimal as I could get a complete example which demonstrates my issue. I'm probably missing something obvious, or I have some logic error somewhere.

Code: [Select]
#include <SFML/Graphics.hpp>

int main(int argc, char **argv) {
    int x=0,xs=0,ys=0,speed=25;
    float left=0.0,right=0.0;

    sf::RenderWindow App(sf::VideoMode(1280, 720, 32), "Game");
    App.SetFramerateLimit(60);
    App.UseVerticalSync(true);
    sf::Image back;
    back.LoadFromFile("sky.png");
    sf::Sprite bg(back);
    bg.Resize(1280,720);
    sf::Image image;
    image.LoadFromFile("character.png");
    sf::Sprite sprite(image);
    sf::View BGCamera(sf::FloatRect(0,0,1280,720));
    sf::View Camera(sf::FloatRect(-200,-500,1280,720));

    while (App.IsOpened()) {
        sf::Event event;
        while (App.GetEvent(event)) {
            if (event.Type == sf::Event::Closed)
                App.Close();
        }

        const sf::Input& inputs = App.GetInput();
        if (inputs.IsKeyDown(sf::Key::Right))       xs=speed;
        else if (inputs.IsKeyDown(sf::Key::Left))   xs=-speed;
        else                                        xs=0;
        sprite.Move(xs,ys);
        if (xs) sprite.FlipX(xs<0);

        left=Camera.GetCenter().x-(Camera.GetSize().x/3);
        right=Camera.GetCenter().x+(Camera.GetSize().x/3);
        x=sprite.GetPosition().x;
        if (!xs)     xs=speed;
        if (xs<0)    xs=-xs;
        if (x<left)  Camera.Move(-xs,0);
        if (x>right) Camera.Move(xs,0);

        App.Clear();
        App.SetView(BGCamera);
        App.Draw(bg);
        App.SetView(Camera);
        App.Draw(sprite);
        App.Display();
    }
    return 0;
}


Fixed. The camera and the sprite must move at 100% the same speed. Marking this topic as solved, hopefully I can get this fixed in my game as well.

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
[Solved] Scrolling a view is creating odd sprite shaking
« Reply #2 on: February 21, 2011, 08:35:54 am »
Any particular reason why you're using both vsync and frame rate limitation at the same time?

XDest

  • Newbie
  • *
  • Posts: 8
    • View Profile
[Solved] Scrolling a view is creating odd sprite shaking
« Reply #3 on: February 21, 2011, 11:03:28 am »
Not really, but it is irrelevant to this situation. This problem does not change with just one of them on.

 

anything