Jump to content
  • Advertisement
Sign in to follow this  
Shadowwoelf

Confused on Pixel perfect detection (Riemers tutorial)

This topic is 3033 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was using Riemers tutorials and have been now trying to implement my own pixel perfect detection, but I have run into a problem in the rotation. If I rotate my image X amount of pixels the pixel perfect detection then fails, and I am not sure as to why.
//What the zombies get initialized to.
ZombieList.Add(new Sprite("Game/Zombie"));
                ZombieList[ZombieList.Count - 1].Position.Y = ZombieList[ZombieList.Count - 1].Height;
                ZombieList[ZombieList.Count - 1].Position.X = rnd.Next(0, 800);
                ZombieList[ZombieList.Count - 1].Origin.Y = ZombieList[ZombieList.Count - 1].Height;
                ZombieList[ZombieList.Count - 1].Velocity.Y = (rnd.Next(1, 10) / 100f);
                ZombieList[ZombieList.Count - 1].Rotation = 30;
                ZombieList[ZombieList.Count - 1].Scale = new Vector2(.5f);
                ZombieList[ZombieList.Count - 1].FadeInOut = true;


//I get a point then I use the inverse matrix of the image and 
//then find out where it hits in the texture
//If the .a >0 that means its not transparent and thus there is a hit.
public bool TexturesCollide(float mouseX, float mouseY)
        {
            bool returnValue = false;

            Matrix thisSprite = 
                Matrix.CreateTranslation(this.Origin.X,-this.Origin.Y,0)*
                Matrix.CreateRotationZ(MathHelper.ToRadians(this.Rotation))*
                Matrix.CreateScale(this.Scale.X, this.Scale.Y, 1) *
                Matrix.CreateTranslation(this.Position.X,this.Position.Y,0);
            Matrix invertSprite = Matrix.Invert(thisSprite);

            Vector2 pos1 = new Vector2(mouseX, mouseY);
            Vector2 pos2 = Vector2.Transform(pos1, invertSprite);

            if(pos2.X>0&&pos2.X<this.Texture.Width)
            {
                if(pos2.Y>0&&pos2.Y<this.Texture.Height)
                {
                    if (spriteColorArray[(int)pos2.X, (int)pos2.Y].A > 0)
                    {
                        returnValue = true;                        //This returns when the point is non transparent
                    }
                }
            }

            return returnValue;
        }

Any help would be appreciated! thanks.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!