SFML community forums
Help => Graphics => Topic started by: Mashandar on April 22, 2009, 05:23:13 pm
-
Hello,
I'm trying to implement a pixel based collision detection,
and what I planned to do was to iterate through the pixels of one
sprite (go from 0,0 to sprite.GetSize().x, sprite.GetSize().y respectively),
use TransformToGlobal to get a global co-ordinate, then use the
opposing sprite's TransformToLocal, to check whether that pixel position
in that opposing sprite's space.
unfortunately, after using TransformToLocal on the opposing sprite, I
get unusual results.
Example
Sprite 1 Position: 265.83, 599.309
Sprite 2 Position: 327.316, 596.533
(Using TransformToGlobal)
Sprite 1 Global Position of 0, 0: 233.83, 567.309
Sprite 2 Global Position of 0, 0: 295.316, 564.533
(Using TransformToLocal of sprite 1's global position of (0, 0) on sprite 2)
-1229.72, 55.5112 -> Unusual
Here's some code to help explain what I'm doing:
sf::Vector2f pos1, pos2;
sf::Color colour;
int i;
for(i=0;i<sprite1.GetSize().x;i++) {
for(j=0;j<sprite1.GetSize().y;j++) {
pos1 = sprite1.TransformToGlobal(sf::Vector2f(i, j));
pos2 = sprite2.TransformToLocal(pos1);
if (<pos2's value is not invalid. eg. >= (0,0) or < sprite2.GetSize()>) {
colour = sprite2.GetPixel(pos2);
if (colour.a > 0) { return true; }
}
}
}
return false;
etc.
now in the cases I have been testing, the rotation and scale and even
center points on each object are the same, and I'm rather stumped as
to why wierd values are coming out. (especially such large negative values, I had values going up to -6000+)
I really hope someone can help.
Regards,
Mashandar
P.S. I apologise for the large posting, I just wanted to make sure there's
plenty of information about my problem.
-
Hi
Maybe you can write a minimal and complete example that reproduces the problem, with simple values.
-
Interestingly, I wrote the small test, and everything seems to work.
I guess it must be something to do with my game code.
I apologise for the time I wasted.
Thanks.
-
The only thing I can see is that you're using GetSize(), which already takes the scale in account. To be 100% correct you should use the dimensions of GetSubRect(). But if your scale is 1 then it doesn't explain the weird results.
-
Actually, that may be it.
I was using a scale of 1 in my test case, but the sprites
in my game code varies in size. I'll give that a shot.
EDIT: nevermind, that didn't seem to work.
-
I was stewing on this problem for a while now, but I finally figured it out!
out of pure silliness, I forgot the check the alpha values of one of the sprites,
and was only checking the alpha value of the second sprite. silly me.