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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:16:37+00:00 2026-06-14T21:16:37+00:00

Okay, so I have an enemy class(With rotation, position, texture and so on). I

  • 0

Okay, so I have an enemy class(With rotation, position, texture and so on). I spawn a few enemies on the screen, they move around, but they overlap each other. So I tried to do a collision check between two enemies of the same class. But no matter what method I try, it isn’t quite working. The best thing I tried was:

foreach (Enemy enemy1 in enemies)
{
 enemy1Pos = new Vector2(enemy1.position.X, enemy1.position.Y)

 foreach (Enemy enemy2 in enemies)
 {
  enemy2Pos = new Vector2(enemy2.position.X, enemy2.position.Y)
  if (Vector2.Distance(enemy2Pos, enemy1Pos) < 200)
  {
  enemy1Pos += new Vector2((float)(enemy1.Speed * Math.Cos(enemy1.Rotation)), (float)(enemy1.Speed * Math.Sin(enemy1.Rotation)));
  }
 }
}

This is not to exact code, so it might have some mistakes in it. Anyway when i implemented this solution, the enemies were not overlapping so everything was fine on that part. But, they were always moving to the right side of the screen.

I’ve also looked up flocking etc, but I would like to know, how can I detect collision between 2 objects of the same class?

  • 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-14T21:16:39+00:00Added an answer on June 14, 2026 at 9:16 pm

    If enemy1 is equal to enemy2 always will collide. 🙂

    Avoid that….

    foreach (Enemy enemy2 in enemies)
    {
       if (enemy2 == enemy1) continue;
       enemy2Pos = new Vector2(enemy2.position.X, enemy2.position.Y)
       if (Vector2.Distance(enemy2Pos, enemy1Pos) < 200)
       {
          enemy1Pos += new Vector2((float)(enemy1.Speed * Math.Cos(enemy1.Rotation)), (float)(enemy1.Speed * Math.Sin(enemy1.Rotation)));
       }    
    }
    

    Foreach is not the best here… you can accomplish the collsion checking in a more efficient way with for:

     for (int i = 0; i<enemies.Count-1; i++)
     {
           var enemy1 = enemies[i];
           for (int j=i+1; j<enemies.Count; j++)
           {
               var enemy2 = enemies[j]
           }
    

    This way you avoid self collsion checkings and avoid duplicate checking too.

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

Sidebar

Related Questions

Okay I have a series of objects based on a base class which are
Okay I have updated my code a little, but I am still not exactly
Okay, I'm writing a game that has a vector of a pairent class (enemy)
Okay I have been working on a website but I keep on having an
Okay i have seen TouchXML, parseXML, NSXMLDocument, NSXMLParser but i am really confused with
Okay, I have an array in my Main Class that looks like this: objectArray:Array
Okay I have seen some very similar questions here but none seem to be
Okay I have a few things to ask about. I've included the jsfiddle version
Okay I have a div class content and whenever the browser resizes to 800px
Okay, so I have this class that basically acts as a manager of an

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.