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

Author Topic: Retrieving the correct position from a VertexArray  (Read 2885 times)

0 Members and 1 Guest are viewing this topic.

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Retrieving the correct position from a VertexArray
« on: December 01, 2019, 07:52:38 am »
Hello,

So I've been messing around with VertexArray because I'm trying to see the difference in time when laying down tiles.

// create a quad
sf::VertexArray quad(sf::Quads, 4);

// define it as a rectangle, located at (10, 10) and with size 100x100
quad[0].position = sf::Vector2f(10.f, 10.f);
quad[1].position = sf::Vector2f(110.f, 10.f);
quad[2].position = sf::Vector2f(110.f, 110.f);
quad[3].position = sf::Vector2f(10.f, 110.f);

// define its texture area to be a 25x50 rectangle starting at (0, 0)
quad[0].texCoords = sf::Vector2f(0.f, 0.f);
quad[1].texCoords = sf::Vector2f(25.f, 0.f);
quad[2].texCoords = sf::Vector2f(25.f, 50.f);
quad[3].texCoords = sf::Vector2f(0.f, 50.f);

How do you write a getPosition() function when doing it like this smilar to the sf::Sprite::getPosition(). I get you can make a class and derive from Drawable and Transform. How would this look if you didn't do that? Would returning quad[0].position be the same as if I loaded a Sprite and returned the position? And if I set the origin to the middle {quad[0] + quad[1] / 2, quad[2] + quad[4] / 2} it would be like this?

Thanks, I hope that makes sense.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Retrieving the correct position from a VertexArray
« Reply #1 on: December 02, 2019, 12:03:44 am »
The centre of that quad is not "quad[0] + quad[1] / 2, quad[2] + quad[4] / 2".
Instead, it would be: ((quad[2] - quad[0]) / 2) + quad[0] or just (quad[0] + quad[2]) / 2

The top corner of this quad is quad[0] so that would be the equivalent of where a transformable's (such as a sprite) position would be. Of course, with a transformable, that corner would always be quad[0] + just the transformable's position. (so you'd probably make quad[0] (0, 0) and set the transformable's position to (10, 10).

If you aren't using sf::Transformable, what do you mean by "setting the origin"?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: Retrieving the correct position from a VertexArray
« Reply #2 on: December 02, 2019, 04:08:28 am »
Appreciate the help,

The reason I thought (quad[0] + quad[1]) / 2, (quad[1] + quad[2]) / 2 was because I was thinking about it in terms of (left + width) / 2 and (top + height) / 2.

I'm at work right now, so I'll try it when I get home. Appreciate it.

Thanks for your help.

 

anything