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

Author Topic: I have been toying with sprites and textures and found an oddity  (Read 2415 times)

0 Members and 1 Guest are viewing this topic.

Spirro

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
I have been toying with sprites and textures and found an oddity
« on: November 14, 2015, 06:53:11 am »
I have been working on various things setting up for a small game program and came across something odd.  I am no an artist by any stretch so I found a placeholder graphic on opengameart to use as a ship, however I couldn't find a suitable size.  I thought scaling would be fine so I did so and plopped the sprite in the middle of the display and it looked off.  Thinking it had to do with the scaling I investigated a little.

The image is 150 by 120 pixels and the render area is 320 by 200.  I scaled the image by 0.5f for both width and height, placed it at what should be the center of the render area and printed out the screen render size, sprite global bound and local bounds in the console window.  What was odd to me is that the local bounds size of the sprite shows to be 150 width and 120 height, just as the original image, but the global shows as 60 width and 75 height.  Note that the values are inverted.  It should show as 75 width and 60 height.  I did make sure to recheck the code used and it seemed in the proper order.
Code: [Select]
    sf::FloatRect shipBounds;
    shipBounds = shipSprite.getLocalBounds();
    std::cout << "Ship local bounds size...X: " << shipBounds.width << " Y: " << shipBounds.height << std::endl;
    shipBounds = shipSprite.getGlobalBounds();
    std::cout << "Ship global bounds size...X: " << shipBounds.width << " Y: " << shipBounds.height << std::endl;

Note that shipSprite is defined as sf::Sprite higher up in the code.

Edit:  Sorry I forgot to put the version of SFML I used was compiled from the master branch on GIT.  2.3.x
« Last Edit: November 14, 2015, 07:11:30 am by Spirro »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: I have been toying with sprites and textures and found an oddity
« Reply #1 on: November 14, 2015, 01:52:43 pm »
Please post a complete and minimal example that shows the problem. The code you showed doesn't show the transformations you are using so we can't try to reproduce the problem. It might even be a good idea to include the texture you are using.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Spirro

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: I have been toying with sprites and textures and found an oddity
« Reply #2 on: November 14, 2015, 06:49:09 pm »
Please post a complete and minimal example that shows the problem. The code you showed doesn't show the transformations you are using so we can't try to reproduce the problem. It might even be a good idea to include the texture you are using.

Yes you're right.  At the time of posting I didn't think that was necessary, but after sleeping on it I figured it out.  I do rotate the sprite 90 degrees to change the direction it faces and that would be the problem with the inverted width and height, but I don't understand why it would be implemented that way.  To me, the new width and height would be the desired returns from getting the bounding box.

BlueCobold

  • Full Member
  • ***
  • Posts: 105
    • View Profile
Re: I have been toying with sprites and textures and found an oddity
« Reply #3 on: November 14, 2015, 07:21:52 pm »
Rotating a 150x120 sprite by 90° results in a 120x150 bounding box. That is totally logical.

Hapax

  • Hero Member
  • *****
  • Posts: 3357
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: I have been toying with sprites and textures and found an oddity
« Reply #4 on: November 14, 2015, 07:36:59 pm »
the new width and height would be the desired returns from getting the bounding box.
Global bounds gives the new width and height after all transformations. If you need the width and height after only 'some' of the transformations, you will need to apply those transformations manually.
Scale is easy: local bounds * scale
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Spirro

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: I have been toying with sprites and textures and found an oddity
« Reply #5 on: November 14, 2015, 07:44:28 pm »
Thank you Hapax.  As I posted this is kind of a trial period so I will just make adjustments to get things the way I expected them to be.