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

Author Topic: Getting sprite coords?  (Read 2040 times)

0 Members and 1 Guest are viewing this topic.

orgos

  • Newbie
  • *
  • Posts: 27
    • View Profile
Getting sprite coords?
« on: August 27, 2010, 03:21:41 pm »
Hi.

We want to make some "game engine" or framework using SFML, we are beginners programmers on c++ and we will need the entire forum help.

So here is our first request for help.

We make a new class called ege::Image (ege is our namespace for Easy Game Engine), the image inherit from sf::Sprite and the purpose is to make more simple the use of a simple Image. Image have many methods to make the developer life better. One of this method is SetAlign, where the Image can align to screen or to other Image, the align can be LEFT, CENTER, RIGHT, TOP, MIDDLE, BOTTOM, we implement it basically using the Position of Sprite and the Size of it, but that not make a full align when image rotate for example, looking in SFML source code we saw that the method Render of sprite calculate the coords to render the Sprite, there is any way that we can obtain it, direct from sf::Sprite to make a better calculation, we need it for others features to.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32418
    • View Profile
    • SFML's website
    • Email
Getting sprite coords?
« Reply #1 on: August 27, 2010, 03:25:19 pm »
I think what you need is the TransformToGlobal function. It converts a position defined in local coordinates, to global world coordinates.

For example, if you call sprite.TransformToGlobal(0, 0), then you'll get the screen position of the top-left corner of the sprite.
Laurent Gomila - SFML developer

orgos

  • Newbie
  • *
  • Posts: 27
    • View Profile
Getting sprite coords?
« Reply #2 on: August 27, 2010, 03:41:15 pm »
I don't think that works because, when I rotate for example 45 degrees the Sprite render surface with and height is not the same of the Sprite with and height.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32418
    • View Profile
    • SFML's website
    • Email
Getting sprite coords?
« Reply #3 on: August 27, 2010, 03:57:45 pm »
It depends what you call "width" and "height" after a 45 degrees rotation. It you talk about the dimensions along the global X and Y axis, then yes it depends on the sprite orientation. But you can still calculate it with the TransformToGlobal function and a few mathematics.

Here is how I would do it:
- transform the 4 corners of the sprite with TransformToGlobal
- extract the min and max X/Y of all points
- width = maxX - minX
- height = maxY - minY

In fact, here we compute what is usually called the "axis aligned bounding box" of the sprite.
Laurent Gomila - SFML developer

orgos

  • Newbie
  • *
  • Posts: 27
    • View Profile
Getting sprite coords?
« Reply #4 on: August 27, 2010, 04:34:29 pm »
Yes thats exactly what i want to do, but i don't know hoy to get the 4 corners of sprite after it is rotate for example.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32418
    • View Profile
    • SFML's website
    • Email
Getting sprite coords?
« Reply #5 on: August 27, 2010, 04:42:22 pm »
Code: [Select]
sf::Vector2f p0 = sprite.TransformToGlobal(0, 0);
sf::Vector2f p1 = sprite.TransformToGlobal(sprite.GetSize().x, 0);
sf::Vector2f p2 = sprite.TransformToGlobal(0, sprite.GetSize().y);
sf::Vector2f p3 = sprite.TransformToGlobal(sprite.GetSize().x, sprite.GetSize().y);
Laurent Gomila - SFML developer