So I have a Sprite, which itself has a Texture, and which may be scaled/rotated/translated. I should probably also note that it is centered, i.e.
Sprite.setOrigin(Sprite.getLocalBounds().width / 2, Sprite.getLocalBounds().height / 2);
I also have a given Point (sf::Vector2f) in global space.
If the point is within the Sprite's local bounds, I want to figure out the color of the exact pixel of the source Texture the point corresponds to.
Obviously I somehow need to convert from global to local space, but I'm not sure how to do this. My previous attempt was:
//I also tried without subtracting the position,
//since I assume that the sprite's translation is also included in its transform
Point.x -= Sprite.getPosition().x;
Point.y -= Sprite.getPosition().y;
//I also tried getTransform(), which unsurprisingly didn't work either
Point = Sprite.getInverseTransform().transformPoint(Point);
sf::Color PointColor = Sprite.getTexture()->copyToImage().getPixel(Point.x, Point.y);
What is the correct way of doing this?
Your code is almost ok: you must offset the point by the (left, top) coordinates of the sprite's texture rect. Because the local point (0, 0) of the sprite maps to the top-left corner of the texture rect in the texture, not to point (0, 0).
Point = Sprite.getInverseTransform().transformPoint(Point);
Point.x += Sprite.getTextureRect().left;
Point.y += Sprite.getTextureRect().top;
sf::Color PointColor = Sprite.getTexture()->copyToImage().getPixel(Point.x, Point.y);
Note that the copyToImage() function will be damn slow, you should consider doing it once and storing the resulting image if you have to run this code often.