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

Author Topic: Pixel Perfect Collision Image Problem.  (Read 1790 times)

0 Members and 1 Guest are viewing this topic.

Paijan

  • Newbie
  • *
  • Posts: 4
    • View Profile
Pixel Perfect Collision Image Problem.
« 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)

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Pixel Perfect Collision Image Problem.
« Reply #1 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
« Last Edit: November 07, 2014, 12:18:50 am by Ixrec »