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

Author Topic: Sprite not in the expected position  (Read 5956 times)

0 Members and 1 Guest are viewing this topic.

Lauro

  • Newbie
  • *
  • Posts: 10
    • View Profile
Sprite not in the expected position
« on: January 06, 2020, 11:03:33 am »
I create the render window in a state (I use state manager) in this way:
renderWindow.create(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Xenon", sf::Style::Fullscreen);
 

where the two macros are 1280 and 800.

in another state(objects are shared among states well, I'm sure), when I create a object that is supposed to be in the center of the screen, it is shifted to the right.
Specifically I have a spaceship class that has 4 "different" spaceship possible. The difference, other than their stats (fire rate, acceleration and so on.. that I will not report since they are not influent), are of course the dimensions of the sprite, that I use to subtract it to the half of the screen dimensions (both in width and height) to move its top left corner and put the spaceship's center in the center of the window.
Three of the spaceships are centred well, one is shifted in the right (correct y-position, x-position shifted to the right side of the screen).

this is how I manage the positioning, I will cut the stats attribution since are irrelevant:
xPos=((float)WINDOW_WIDTH - sprite.getLocalBounds().width)/2;
    yPos=((float)WINDOW_HEIGHT - sprite.getLocalBounds().height)/2;
    std::cout << xPos << ", " << yPos << std::endl;
    sprite.setPosition(xPos, yPos);
    std::cout << "position is" << sprite.getPosition().x << ", " << sprite.getPosition().y << std::endl;
    sprite.setOrigin(sprite.getLocalBounds().width/2,sprite.getLocalBounds().height/2);
 
I use the same code for all the 4 spaceships, in facts the constructor use a switch case for the texture selection and appropriate stats attribution, than it has a shared part (out of the switch case) with this portion of code.

What's the problem? I added the first cout to see what value is assigned to xPos and yPos but it always print the value it should have, and it is the same printed to the second cout, every time i run the program, for all the spaceships.

EDIT: now I changed a minor thing in the program, the entities life is considered as boolean and not as int and now randomly 2 spaceship are in the correct position and 2 are shifted (once i experienced a shift on the left)...
« Last Edit: January 06, 2020, 12:20:42 pm by Lauro »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprite not in the expected position
« Reply #1 on: January 06, 2020, 12:52:43 pm »
If you set the origin of your sprites to their center, then their position should just be the center of the window (WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2). Your code would work only in the case of the default origin (ie. the top-left corner).

But the shifting should be consistent, if you see random results then you may have some other problem, probably elsewhere in your code.
Laurent Gomila - SFML developer

Lauro

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Sprite not in the expected position
« Reply #2 on: January 06, 2020, 04:46:15 pm »
Thanks Laurent, I did so:
sprite.setOrigin(sprite.getLocalBounds().width/2,sprite.getLocalBounds().height/2);
    xPos = (float)WINDOW_WIDTH/2;
    yPos = (float)WINDOW_HEIGHT/2;
    sprite.setPosition(xPos, yPos);
 

and this is not resolving anything but the fact that now 3 sprites are positioned well and 1 is still not positioned well  :o :-\

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Sprite not in the expected position
« Reply #3 on: January 06, 2020, 04:54:01 pm »
The following isn't the solution since there is clearly something happening that shouldn't be that you need to know about but...

If you set the sprite's position to the view's centre, does that place it in the correct place?:
sprite.setPosition(renderWindow.getView().getCenter());
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Lauro

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Sprite not in the expected position
« Reply #4 on: January 06, 2020, 06:16:39 pm »
The following isn't the solution since there is clearly something happening that shouldn't be that you need to know about but...

If you set the sprite's position to the view's centre, does that place it in the correct place?:
sprite.setPosition(renderWindow.getView().getCenter());

the fact is that the sprite I have to center doesn't know about the render window..

edit: I tried as you said and it works fine, but the code now is a bit messy, and i haven't touched anything since last edit report
edit2: Now, all ok, just the same problematic spaceship is shifted in the bottom, but has the correct axis
« Last Edit: January 06, 2020, 07:47:17 pm by Lauro »

Lauro

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Sprite not in the expected position
« Reply #5 on: January 07, 2020, 01:16:46 pm »
somebody has any idea?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprite not in the expected position
« Reply #6 on: January 07, 2020, 01:37:14 pm »
You should write a complete and minimal example that reproduces the problem, then post it here.
Laurent Gomila - SFML developer

 

anything