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 698713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:18:21+00:00 2026-05-14T03:18:21+00:00

I’m trying to create a good way to handle all possible collisions between two

  • 0

I’m trying to create a good way to handle all possible collisions between two objects. Typically one will be moving and hitting the other, and should then “bounce” away.

What I’ve done so far (I’m creating a typical game where you have a board and bounce a ball at bricks) is to check if the rectangles intersect and if they do, invert the Y-velocity.

This is a really ugly and temporary solution that won’t work in the long haul and since this is kind of processing is very common in games I’d really like to find a great way of doing this for future projects aswell. Any links or helpful info is appreciated.

Below is what my collision-handling function looks like right now.

protected void collision()
    {
        #region Boundaries
        if (bal.position.X + bal.velocity.X >= viewportRect.Width ||
            bal.position.X + bal.velocity.X <= 0)
        {
            bal.velocity.X *= -1;
        }
        if (bal.position.Y + bal.velocity.Y <= 0)
        {
            bal.velocity.Y *= -1;
        }
        #endregion
        bal.rect = new Rectangle((int)bal.position.X+(int)bal.velocity.X-bal.sprite.Width/2, (int)bal.position.Y-bal.sprite.Height/2+(int)bal.velocity.Y, bal.sprite.Width, bal.sprite.Height);
        player.rect = new Rectangle((int)player.position.X-player.sprite.Width/2, (int)player.position.Y-player.sprite.Height/2, player.sprite.Width, player.sprite.Height);

        if (bal.rect.Intersects(player.rect))
        {
            bal.position.Y = player.position.Y - player.sprite.Height / 2 - bal.sprite.Height / 2;
            if (player.position.X != player.prevPos.X)
            {
                bal.velocity.X -= (player.prevPos.X - player.position.X) / 2;
            }

            bal.velocity.Y *= -1;
        }
        foreach (Brick b in brickArray.list)
        {
            b.rect.X = Convert.ToInt32(b.position.X-b.sprite.Width/2);
            b.rect.Y = Convert.ToInt32(b.position.Y-b.sprite.Height/2);
            if (bal.rect.Intersects(b.rect))
            {
                b.recieveHit();
                bal.velocity.Y *= -1;
            }
        }
        brickArray.removeDead();
    }
  • 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-05-14T03:18:22+00:00Added an answer on May 14, 2026 at 3:18 am

    Here are a couple of suggestions that may help.

    First of all, all your objects such as Player, Bal (not sure what that is) and Brick have somewhat similar interface – they all have sprite (defining the size of the object), rect (bounding box of the object), position (current position), velocity (current velocity). This suggests that you could create a common base class to encapsulate this functionality.

    Also note that you’re always recalculating rect based on the current position and sprite. This suggests that rect should actually be a property that hides the recalculation (which is always the same!)

    So, you can start by defining a common base class like this (I’ll follow .NET standard coding style and use properties and CamelCase names):

    class GameObject {
      Point Position { get; set; } 
      Vector Velocity { get; set; }
      Sprite Sprite { get; set; }
      Rectangle Rect { 
        get { 
          // Ecnapsulated calculation of the bounding rectangle
          return new Rectangle
            ( (int)Position.X + (int)Velocity.X - Sprite.Width/2, 
              (int)Position.Y + (int)Velocity.Y - Sprite.Height/2, 
              Sprite.Width, Sprite.Height);   
        }
    }    
    

    If you now use this type as a base class for all your game objects, you should be able to write this:

    protected void Collision() {      
        #region Boundaries        
        if (bal.Position.X + bal.Velocity.X >= viewportRect.Width ||        
            bal.Position.X + bal.Velocity.X <= 0)        
            bal.Velocity.X *= -1;        
        if (bal.Position.Y + bal.Velocity.Y <= 0)        
            bal.Velocity.Y *= -1;        
        #endregion
    
        if (bal.Rect.Intersects(player.Rect)) {      
            bal.Position.Y = player.Position.Y - player.Sprite.Height/2 
                           - bal.Sprite.Height/2;      
            if (player.Position.X != player.PrevPos.X)      
                bal.Velocity.X -= (player.PrevPos.X - player.Position.X) / 2;      
            bal.Velocity.Y *= -1;      
        }      
        foreach (Brick b in brickArray.list) {      
            if (bal.Rect.Intersects(b.Rect)) {      
                b.RecieveHit();      
                bal.Velocity.Y *= -1;      
            }      
        }      
        brickArray.RemoveDead();      
    }      
    

    It also sounds like a good idea to split the function between two – one that checks bal and player and the other that checks the bricks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

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.