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

Author Topic: Incorrect coordinates after SetCenter on Sprite (1.6)  (Read 1144 times)

0 Members and 1 Guest are viewing this topic.

amator

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Incorrect coordinates after SetCenter on Sprite (1.6)
« on: April 26, 2012, 06:16:01 pm »
Hi!

I have a problem with setting coordinates after SetCenter call in sf::Sprite.
The problem is that I can't see sprite although coordinates are corrected.
Before the SetCenter call is OK on the same code  :(

Code: [Select]
tempBox.mSprite.SetImage(mPlatformImage);
tempBox.mSprite.Resize(tempHeight.mEndPoint - tempHeight.mStartPoint, 32);
tempBox.mSprite.SetCenter(tempBox.mSprite.GetSize().x / 2, tempBox.mSprite.GetSize().y / 2);
tempBox.mSprite.SetPosition(tempHeight.mStartPoint - tempBox.mSprite.GetCenter().x, tempHeight.mHeight - tempBox.mSprite.GetCenter().y);

Size of mSprite after Resize():
- width: 1536
- height: 32

- mEndPoint: 1520
- mStartPoint: -16
- mHeight: 402

Window resolution: 640x480

Let me add that I have a sf::View, centered on the center of the window and all the time I do a 2px offset in X axis.

Where is the problem?
Please, help me.


EDIT:
I recreated the problem with simple code. You can compile it and check (uncomment this line and comment out again).
Code: [Select]
#include <SFML/Graphics.hpp>
 
 int main()
 {
     sf::RenderWindow *window = new sf::RenderWindow(sf::VideoMode(640, 480), "Problemik");
 
     sf::Image image;
sf::Sprite sprite;
     
if (!image.LoadFromFile("problem.png"))
         return 1;
     
sprite.SetImage(image);
sprite.Resize(1536, 32);
//sprite.SetCenter(sprite.GetSize().x / 2, sprite.GetSize().y / 2); // Here is the problem
sprite.SetPosition(450 - sprite.GetCenter().x, 300 - sprite.GetCenter().y);
 
     while (window->IsOpened())
     {
         sf::Event Event;
         
while (window->GetEvent(Event))
         {
             if (Event.Type == sf::Event::Closed)
                 window->Close();
         }
 
         window->Clear();
         window->Draw(sprite);
         window->Display();
     }
 
     return 0;
 }

I attached whatever as problem.png


[attachment deleted by admin]
« Last Edit: April 26, 2012, 08:30:17 pm by amator »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Incorrect coordinates after SetCenter on Sprite (1.6)
« Reply #1 on: April 27, 2012, 08:04:23 am »
The Center of the sprite is a local point, it doesn't take the position/rotation/scale in account. So you must set it according to the original size of the sprite.
Laurent Gomila - SFML developer