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

  • Home
  • SEARCH
  • 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 7853741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:47:15+00:00 2026-06-02T19:47:15+00:00

For the past 12 hours I’ve been trying all different methods I can think

  • 0

For the past 12 hours I’ve been trying “all” different methods I can think of, in order to make two rectangles intersect/test collision correctly in XNA (4.0).

I have three classes (just example names):

  • Game1.cs
  • Player.cs
  • Man.cs

In Game1 I load the content like this:

// In the beginning of Game1:

    Player player;
    Man man;

// LoadContent:

    man = new Man(Content.Load<Texture2D>("ManImage"), new Rectangle(440, 310, 150, 98));
    player = new Player(Content.Load<Texture2D>("playerSheet"), new Vector2(40, 420), 50, 44);

However, I want to check for collision between man and player, but I don’t understand how to “join” those two classes to be able to do an if(intersects) in Game1 (if that’s the place to do it).

The player rectangle looks like this:

playerRect = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);

Have I just gone completely “code blind” here, or what? Anyone able to help me out with this, motivation went down after 12 hours of collision testing.

  • 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-02T19:47:16+00:00Added an answer on June 2, 2026 at 7:47 pm

    You could, for both of the classes, add a property

    public Rectangle PositionRectangle { get; private set; }
    

    then, in the relevant constructors, you’d need to initiate this property

    public Man(..., Rectangle rectangle)
    {
        PositionRectangle = rectangle;
    }
    
    public Player(..., Vector2 position, int width, int height)
    {
        PositionRectangle = new rectangle((int)position.X, (int)position.Y, width, height);
    }
    

    You don’t need to worry about what frame you’re displaying, since this is related to the source rectangle, not to the destination rectangle (which is what you need to test for collisions).

    Then you can test for these collisions easily

    if(man.PositionRectangle.Intersects(player.PositionRectangle))
    {
        //collision logic
    }
    

    Of course, you’d need to take care to update these rectangles any time you change the positions (or widths and heights if those are changeable)


    Slight edit (or a better version)

    If you would like to position Man and Player easily without having to worry about how their rectangles are updated, you could set it up like this:

    class Man
    {
        Texture2D _texture;
    
        public Vector2 Position { get; set; }
        public int Width { get; set; } 
        public int Height { get; set; }
    
        public Rectangle PositionRectangle
        {
            get
            {
                return new Rectangle((int)Position.X, (int)Position.Y, Width, Height);
            }
        }
    
        public Man(Texture2D texture)
        {
            this._texture = texture;
            this.Width = texture.Width;
            this.Height = texture.Height;
        }
    
        ... //other man related logic!
    }
    
    class Player
    {
        Texture2D _texture;
        int _frameCount;
        int _currentFrame;
        //other frame related logic...
    
        public Vector2 Position { get; set; }
        public int Width { get; set; } 
        public int Height { get; set; }
    
        public Rectangle PositionRectangle
        {
            get
            {
                return new Rectangle((int)Position.X, (int)Position.Y, Width, Height);
            }
        }
    
        public Rectangle SourceRectangle
        {
            get
            {
                return new Rectangle(Width * _currentFrame, 0, Width, Height);
            }
        }
    
        public Player(Texture2D texture, int frameWidth, int frameCount)
        {
            this._texture = texture;
            this.Width = frameWidth;
            this.Height = texture.Height;
            this._frameCount = frameCount;
            this._currentFrame = 0;
            //etc...
        }
    
        ... //other player related logic! such as updating _currentFrame
    
        public Draw(SpriteBatch spriteBatch)
        {
             spriteBatch.Draw(_texture, PositionRectangle, SourceRectangle, Color.White);
        }
    }
    

    Here I’ve given you example of how you could make use of property accessors to dynamically generate your relevant rectangles!

    You use PositionRectangle to determine where your player (or man) will be located on screen, and you use SourceRectangle to determine which part of the texture you’re gonna draw.

    Also, if your man and player widths and heights aren’t going to change then you should set their set accessors to private.

    When you need to change the locations, you just set their position property

    man.Position = new Vector2(100, 100); //i.e.
    

    and your collisions and draw logic will automatically use the new rectangles without any further intervention!

    Hope you like it!

    Edit2

    Relating to source rectangle being public, you only need that if you’re drawing these from outside (i.e. Game1 calls spriteBatch.Draw(player.Texture, player.PositionRectangle, player.SourceRectangle, Color.White);).

    If you’re drawing it like in the example (from the inside), you could just scrap the whole public Recangle SourceRectangle{...} thing and just use that rectangle directly in the draw:

        public Draw(SpriteBatch spriteBatch)
        {
             spriteBatch.Draw(_texture, PositionRectangle, new Rectangle(Width * _currentFrame, 0, Width, Height), Color.White);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to understand this algorithm for past two hours, but can't
I've been trying to figure this out for the past few hours and can't
I have been struggling with this for the past few hours. All I'm trying
I've been trying for the past 5 hours to make my java ME application
for past two hours i've been trying to pass responseText to the object i'm
this is something that's been bugging me for the past two hours. I'm trying
I've been trying to solve this for the past two hours and couldn't do
I've been for the past few hours trying to figure out why openCV doesn't
I've just wasted the past two hours of my life trying to create a
I've been problem solving this for the past few hours and frustratingly can't find

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.