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

Author Topic: Wierd glitch after using FlipX()  (Read 4868 times)

0 Members and 3 Guests are viewing this topic.

Mashandar

  • Newbie
  • *
  • Posts: 10
    • View Profile
Wierd glitch after using FlipX()
« on: April 29, 2009, 05:46:44 pm »
Hello,
         I finally got my pixel-perfect collision detection working but It's acting
         funky after using FlipX(true) on a sprite. when I check GetPixel to see
         what's going on, it's reporting that pixels are there, when, visually
         nothing is seen, so the pixels shouldn't be there (the alpha values
         of these pixels is 255).

         I have uploaded the image to imagebin and a sample test
         code to pastebin which shows this.

        Code: http://pastebin.com/m53275387
        Image: http://imagebin.ca/view/5qzsUj5.html

         I'm rather baffled, so I'm hoping someone can help.

EDIT: in the given example, if compiled, you'll see the flipped sprites (top sprites) colliding permaturely (stopping before reaching the other sprite)
and the unflipped sprites (bottom sprites) colliding as they should.

SamuraiCrow

  • Newbie
  • *
  • Posts: 40
    • Yahoo Instant Messenger - samuraileumas
    • View Profile
Wierd glitch after using FlipX()
« Reply #1 on: May 04, 2009, 05:08:01 am »
Are you indexing inverse coordinates in the GetPixel reader?  If FlipX(True) works the way I think it works, it just sets a negative scale factor when blitting to the screen.  The image itself remains unaffected in memory.

--Edit--
After looking at your source code, you need to make the GetPixel check the coordinates backwards relative to the center position on one or both images depending on whether they are flipped or not.

Mashandar

  • Newbie
  • *
  • Posts: 10
    • View Profile
Wierd glitch after using FlipX()
« Reply #2 on: May 04, 2009, 09:33:46 am »
Oh, ok.

I'll try that when I get the chance.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Wierd glitch after using FlipX()
« Reply #3 on: May 04, 2009, 09:43:09 am »
Sprite::GetPixel already takes FlipX/FlipY flags in account.
Laurent Gomila - SFML developer

Mashandar

  • Newbie
  • *
  • Posts: 10
    • View Profile
Wierd glitch after using FlipX()
« Reply #4 on: May 06, 2009, 01:24:04 pm »
Well, I'm still stumped. I tried some different things, like getting pixels from the source image itself while taking the flipping into account (I kinda knew it wouldn't work but thought it couldn't hurt to give it a go.) with no luck still.

I would really like to get this working, so if anyone has any suggestions, please let me know.

nitram_cero

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Wierd glitch after using FlipX()
« Reply #5 on: May 07, 2009, 06:15:52 pm »
Mashandar: Your code (from pastebin) seems ok.


Quote from: "Laurent"
Sprite::GetPixel already takes FlipX/FlipY flags in account.


The function has a bug.

Code: [Select]
////////////////////////////////////////////////////////////
/// Get the color of a given pixel in the sprite
/// (point is in local coordinates)
////////////////////////////////////////////////////////////
Color Sprite::GetPixel(unsigned int X, unsigned int Y) const
{
    if (myImage)
    {
        unsigned int ImageX = mySubRect.Left + X;
        unsigned int ImageY = mySubRect.Top  + Y;

        if (myIsFlippedX) ImageX = mySubRect.GetWidth()  - ImageX;
        if (myIsFlippedY) ImageY = mySubRect.GetHeight() - ImageY;

        return myImage->GetPixel(ImageX, ImageY) * GetColor();
    }
    else
    {
        return GetColor();
    }
}


Quote
       if (myIsFlippedX) ImageX = mySubRect.GetWidth()  - ImageX;
        if (myIsFlippedY) ImageY = mySubRect.GetHeight() - ImageY;


should be
Quote
       if (myIsFlippedX) ImageX = mySubRect.GetWidth()  - ImageX - 1;
        if (myIsFlippedY) ImageY = mySubRect.GetHeight() - ImageY - 1;





You could wait for this to be fixed or do a getpixel on the image like this:

Code: [Select]
if(sprite.IsFlippedX()) pxX=image.GetWidth()-pxX-1;
if(sprite.IsFlippedY()) pxY=image.GetHeight()-pxY-1;

Good luck!
-Martín

Mashandar

  • Newbie
  • *
  • Posts: 10
    • View Profile
Wierd glitch after using FlipX()
« Reply #6 on: May 07, 2009, 07:12:45 pm »
Aha!

Thanks, that worked perfectly!
I recompiled SFML on my machine with that for now, hopefully it'll be fixed in future SFML releases too, as that bug had me confused for a week. (I assumed it was my code's fault....as it tends to be most of the time)

nitram_cero

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Wierd glitch after using FlipX()
« Reply #7 on: May 07, 2009, 07:24:57 pm »
I usually think that too, but your code was ok and I had the SFML source opened so I checked on that just in case.


One recommendation: Before the per-pixel collisions, you could check if the sprites' rects intersect. This will save you a lot of CPU.
Code: [Select]
if(!s1.Intersects(s2))
   return false;


This is not valid for rotation thou, but Sprite::GetPixel doesn't work with rotation/scaling anyway.

Good luck!
-Martín

Mashandar

  • Newbie
  • *
  • Posts: 10
    • View Profile
Wierd glitch after using FlipX()
« Reply #8 on: May 08, 2009, 06:13:10 am »
I already check a rect intersection in my game, I don't have it in my sample code to keep things simple.

as for the GetPixel not handling rotation and scaling, isn't that what
TransformToLocal is for? I've been using sprites of many different
scales and (after this fix), have been running/colliding perfectly.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Wierd glitch after using FlipX()
« Reply #9 on: May 08, 2009, 11:58:09 am »
Quote
The function has a bug.

It's fixed, thanks for your help :)
Laurent Gomila - SFML developer

Mashandar

  • Newbie
  • *
  • Posts: 10
    • View Profile
Wierd glitch after using FlipX()
« Reply #10 on: May 08, 2009, 03:36:20 pm »
Hooray!  :D

nitram_cero

  • Full Member
  • ***
  • Posts: 166
    • View Profile
Re: Wierd glitch after using FlipX()
« Reply #11 on: May 08, 2009, 04:38:24 pm »
Quote from: "Mashandar"

as for the GetPixel not handling rotation and scaling, isn't that what
TransformToLocal is for? I've been using sprites of many different
scales and (after this fix), have been running/colliding perfectly.


You're absolutely right!
Thanks for the clarification.

-Martín