Sign in to follow this  
Shadowwoelf

Confused on Pixel perfect detection (Riemers tutorial)

Recommended Posts

Shadowwoelf    136
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this