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

Author Topic: Strange behaviour in SFML 2.0  (Read 3198 times)

0 Members and 1 Guest are viewing this topic.

TheVirtualDragon

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Control + Alt + Delete!
Strange behaviour in SFML 2.0
« on: August 10, 2012, 02:40:47 pm »
OK, so I was creating a game which was going just fine, until I encountered a very strange problem.

My project uses Box2D and SFML, which has always given beginners loads of trouble, but I am not exactly a beginner. In my project, I have a component based architecture and then a player class which binds everything together. The problem is, that my sprite does not follow the position of it's body. It goes where it is supposed to be but then does not move. This might be a bit confusing, but everything will be clearer later on. This is my main code:

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

#include "Player.h"

using namespace std;

int main()
{
    sf::RenderWindow *window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "Quanta");
    sf::FloatRect viewRect(0, 0, 800/50, 600/50);
    sf::View gameView(viewRect);


    b2Vec2 gravity(0.0f, -10.0f);
    b2World *world = new b2World(gravity);

    Player mainPlayer(*window, *world);

    sf::RectangleShape testShape;
    testShape.setFillColor(sf::Color(0, 0, 0));
    testShape.setSize(sf::Vector2f(1.28, 1.28));

    while(window->isOpen())
    {
        sf::Event ev;
        while(window->pollEvent(ev))
        {
            if(ev.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                window->close();
        }

        window->clear(sf::Color(100, 175, 255));

        world->Step(1.0f/60.0f, 6, 2);

        window->setView(gameView);
        mainPlayer.UpdatePlayer(*window);
        //window->draw(testShape); //This is required, you'll see why later on.
        window->display();
    }

    delete window;
    delete world;

    return 0;
}

And this is the code from UpdatePlayer():

Code: [Select]
void Player::UpdatePlayer(sf::RenderWindow& window)
{
    b2Vec2 bodyPosition = _playerBody.body->GetPosition();
    sf::Vector2f spritePosition(bodyPosition.x, bodyPosition.y);
    sf::Vector2u spriteSize(1.28f, 1.28f);

    _renderPlayer.UpdateSprite(window, playerSprite, spritePosition, spriteSize); //Graphics component will update the sprite
}

And from UpdateSprite():

Code: [Select]
void GraphicsComponent::UpdateSprite(sf::RenderWindow& window, sf::Sprite &objectSprite, sf::Vector2f positions,
                                     sf::Vector2u objectSize)
{
    objectSprite.setPosition(positions.x - (objectSize.x / 2), -(positions.y + (objectSize.y /2)));
    window.draw(objectSprite);
}

Now, this used to work in SFML 1.6, but now, the sprite does not fall. It just stays at the position at which it's Box2D body was initialized. To see what was going on, I outputted the position of the Box2D body and it shows that the body is moving. However, the sprite is not following it's position.

BUT, if I uncomment the line which says "window->draw(testShape)" , then the sprite falls! This means, that the sprite updates it's position only when there is something else being drawn to the screen as well! I don't know if this is supposed to happen, but it didn't in SFML 1.6 so I am now very confused.

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Strange behaviour in SFML 2.0
« Reply #1 on: August 14, 2012, 08:21:43 am »
The sprite transformations are not applied until you draw the sprite. This is the normal behaviour in SFML 2.0
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Strange behaviour in SFML 2.0
« Reply #2 on: August 14, 2012, 08:51:19 am »
Your code looks ok. We'll need a complete and minimal code that reproduces the problem (without Box2D), so that we can test it and debug it easily.

Quote
The sprite transformations are not applied until you draw the sprite. This is the normal behaviour in SFML 2.0
No, they are applied immediately. Internally I use lazy assignments, but it should not make any difference for the end user.
Laurent Gomila - SFML developer

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Strange behaviour in SFML 2.0
« Reply #3 on: August 14, 2012, 12:14:43 pm »
Quote
The sprite transformations are not applied until you draw the sprite. This is the normal behaviour in SFML 2.0
No, they are applied immediately. Internally I use lazy assignments, but it should not make any difference for the end user.

Oops!  :-[ .. Sorry  :)
I must read documentation again  ;D
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Strange behaviour in SFML 2.0
« Reply #4 on: August 14, 2012, 12:20:41 pm »
Quote
I must read documentation again
You may have misunderstood the specific point of the documentation which states that transformations are not accumulated nor applied in the order of call, but rather applied once and in a predefined order (i.e. you cannot move(p)/rotate/move(-p) to rotate around a custom center).

But when you change a sprite's position, rotation or scale, it should be immediately visible either when you draw it, or when you request its bounding rectangle (...or whatever which depends on the sprite's transformations).
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Strange behaviour in SFML 2.0
« Reply #5 on: August 14, 2012, 10:55:08 pm »
The only mistake I could spot is:
  sf::Vector2u spriteSize(1.28f, 1.28f);
Unless that changed recently then your 1.28f is rounded down to 1(unsigned int) and then you perform int division 1/2 which is rounded down to 0 but that shouldn't affect the position.

Is your body and fixture of good size, maybe you're making too many world steps per second(like in this example, only thing capping your step rate is framerate)?
From Box2d user manual:
Quote
A variable time step produces variable results, which makes it
difficult to debug. So don't tie the time step to your frame rate (unless you really, really have to).
Are you absolutely sure that GraphicsComponent::UpdateSprite gets given good positions?
Maybe try capping this entire loop with sf::Clock so it runs in 60 fps constantly.
« Last Edit: August 14, 2012, 10:58:32 pm by FRex »
Back to C++ gamedev with SFML in May 2023

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Strange behaviour in SFML 2.0
« Reply #6 on: August 16, 2012, 01:10:30 pm »
I thought that all the transformations happened in Sprite::draw  :-\
Thanks Laurent
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

TheVirtualDragon

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Control + Alt + Delete!
Re: Strange behaviour in SFML 2.0
« Reply #7 on: August 16, 2012, 05:00:31 pm »
Thanks for all the replies, but it turns out that there was a problem in my graphics drivers. Everything works perfectly now and sorry to have bothered you. (Atleast I know to update my drivers before posting)

 

anything