Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9052989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:18:56+00:00 2026-06-16T13:18:56+00:00

I am building a 2D game in XNA using C#, and I am using

  • 0

I am building a 2D game in XNA using C#, and I am using sprites that will track the player’s position and rotate accordingly in the spritebatch.Draw() method. I am now trying to implement per-pixel collision detection, and I believe that the rotation of the sprites is throwing it off. The collision checks are as follows.

private bool collision(Rectangle object1, Color[,] dataA, Rectangle object2, Color[,] dataB)
    {

        if (object1.Bottom < object2.Top)
            return perPixel(object1, dataA, object2, dataB);
       if (object1.Top > object2.Bottom)
           return perPixel(object1, dataA, object2, dataB);
       if (object1.Left > object2.Right)
           return perPixel(object1, dataA, object2, dataB);
       if (object1.Right < object2.Left)
           return perPixel(object1, dataA, object2, dataB);

       return true;
    }

    private bool perPixel(Rectangle object1, Color[,] dataA, Rectangle object2, Color[,] dataB)
    {
        //Bounds of collision
        int top = Math.Max(object1.Top, object2.Top);
        int bottom = Math.Min(object1.Bottom, object2.Bottom);
        int left = Math.Max(object1.Left, object2.Left);
        int right = Math.Min(object1.Right, object2.Right);

        //Check every pixel
        for (int y = top; y < bottom; y++)
        {
            for (int x = left; x < right; x++)
            {
                Color colourA = dataA[x, y];
                Color colourB = dataB[x, y];

                if (colourA.A != 0 && colourB.A != 0)
                {
                    return true;
                }
            }
        }
        return false;
    }

Only one of the sets of sprites being checked against will be rotated, if that will help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:18:57+00:00Added an answer on June 16, 2026 at 1:18 pm

    I faced a very similar problem , when i was doing collision detection on a rotated sprited.
    What i basically did was rotate the rectangle bounding the sprite by an angle theta , here theta was the angle passed in the rotation parameter of spriteBatch.Draw()

    A very naive implementation of what i am saying is.

    for each X in RectangleofSpriteToBeRotated{
       for each Y in RectangleofSpriteToBeRotated{
    
        Vector2 temp = Vector2.Transform(PointXY, Matrix.CreateRotationZ(Theta));
         //Save Point Temp in a new 2D Array 
        }
    }
    

    If you do/did not understand what i am saying then please comment , i will try to elaborate.

    Update:
    You should try doing this, I am assuming that the Second Sprite should be rotated, i haven’t run this code

    private bool collision(Rectangle object1, Color[,] dataA, Rectangle object2, Color[,] dataB)
        {
    
        var  RotatedP0=  new Vector2.Transform( new Vector2( object2.Top,object2.Left ),Matrix.CreateRotationZ(theta));
        var  RotatedP1=  new Vector2.Transform( new Vector2 (object2.Bottom,object2.Right),Matrix.CreateRotationZ(theta ) );    
    
    
            if (object1.Bottom < RotatedP0.Y)
                return perPixel(object1, dataA, object2, dataB);
           if (object1.Top > RotatedP1.Y)
               return perPixel(object1, dataA, object2, dataB);
           if (object1.Left > RotatedP1.X)
               return perPixel(object1, dataA, object2, dataB);
           if (object1.Right < RotatedP0.X)
               return perPixel(object1, dataA, object2, dataB);
    
           return true;
        }
    
        private bool perPixel(Rectangle object1, Color[,] dataA, Rectangle object2, Color[,] dataB)
        {
            //Bounds of collision
            int top = Math.Max(object1.Top, object2.Top);
            int bottom = Math.Min(object1.Bottom, object2.Bottom);
            int left = Math.Max(object1.Left, object2.Left);
            int right = Math.Min(object1.Right, object2.Right);
    
            //Check every pixel
            for (int y = top; y < bottom; y++)
            {
                for (int x = left; x < right; x++)
                {
    
                    Color colourA = dataA[x, y];
    
                    Vector2 v = Vector2.Transform(new Vector2(x,y), Matrix.CreateRotationZ(theta));
    
                    Color colourB = dataB[ (int) v.X, (int) v.Y];
    
                    if (colourA.A != 0 && colourB.A != 0)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building an iOS game that will have both singleplayer and multiplayer modes. I'm
I am currently building a game using C#/XNA on VS2012 and on my code
i'm building a wp7 application for a game using silverlight & XNA i have
I'm building a game using C# in XNA and I'm not sure which of
Suppose that I'm building game server using Erlang. It's very common circulation each user
I am building a Windows Phone game using C#/XNA. In the game, I need
I'm using XNA 4.0, and I've built a game which will be released for
I'm building a little Android game using libgdx. For now I have a copy
I'm creating a building game in JavaScript and PHP that involves a grid. Each
I'm building a game in as3 that has balls moving and bouncing off the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.