SFML community forums

Help => Graphics => Topic started by: Paijan on November 06, 2014, 11:55:10 pm

Title: Pixel Perfect Collision Image Problem.
Post by: Paijan on November 06, 2014, 11:55:10 pm
Hi.
I'm writing a code, which performs pixel perfect collision check.
It creates sf::Rect which is rectangle of collision.
Then it transforms its coordinates to sprites' local (referring it to 0,0 points of both sprites).
Then I can use those coordinates to choose pixels in sf::Image entities which are created from the same texture as sprites (it has to be sf::Image because spirtes don't have getPixel method) and check their alpha channels.

And it works perfectly fine until i try to scale my sprites in main loop. Then coordinates in sprite don't match coordinates in image, because one of them is scaled.

How do I manage this? Should I somehow scale sprite back to it's original size at the beginning of function?

(click to show/hide)
Title: Re: Pixel Perfect Collision Image Problem.
Post by: Ixrec on November 07, 2014, 12:13:21 am
The short answer is you need to work with the transforms (positions, rotations, scales, etc) of the objects whose pixels/coordinates you're testing, rather than trying to "untransform" them.

Getting all the details right is a moderately difficult task and we're not going to walk you through all of it, but conveniently it's already been done on the wiki: https://github.com/SFML/SFML/wiki/Source:-Simple-Collision-Detection-for-SFML-2