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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:47:27+00:00 2026-05-18T01:47:27+00:00

I am building an XNA game with the following simplified structure: class Entity {

  • 0

I am building an XNA game with the following simplified structure:

class Entity
{
    Vector3 Speed;
    Matrix World;
}

class Mirror : Entity {}

class Lightbeam : Entity {}

class CollisionDetector
{
    /*
    ...
    */

    public override void Update(GameTime gameTime)
    {
        List<Entity> entities = entityManager.level.CurrentSection.Entities;

        for (int i = 0; i < entities.Count - 1; i++)
        {
            for (int j = i + 1; j < entities.Count; j++)
            {
                if(entities[i].boundingBox.Intersects(entities[j].boundingBox)) 
                {
                    collisionResponder.Collide(entities[i], entities[j]);
                }
            }
        }
        base.Update(gameTime);
    }
}

class CollisionResponder
{
    public void Collide(Entity e1, Entity e2)
    {
        Console.WriteLine("Normal entity collision response");
    }

    public void Collide(Mirror mirror, Lightbeam beam)
    {
        Collide(beam, mirror);
    }

    public void Collide(Lightbeam beam, Mirror mirror)
    {
        Console.WriteLine("Specific lightbeam with mirror collision response");
    }
}

What I am trying to achieve is that the Collide(Lightbeam beam, Mirror mirror) method is invoked when the collision detector detects a collision between a beam and mirror (so entities[i] is a Mirror and entities[j] is a Lightbeam and vice versa). However, since the entities list stores objects of the type Entity, the Collide(Entity e1, Entity e2) method is invoked instead.

What i tried to overcome this problem:

  • If a collision is detected, check which types of entities are colliding and invoke the corresponding method. This is quite an ugly solution, since the method should be changed each time a new collision type is added.
  • Use generics:
    Collide(Entity e1, Entity e2)
    where T : Lightbeam
    where U : Mirror

    However, this solution doesn’t make the compiler happy.

  • I found links to the Builder Pattern and Factory Pattern, but it doesn’t seem like that fixes my problem.

It seems to me that there’s a simple solution to this, but I couldn’t find any on the internet (using combinations of keywords like the following: generics, subclass, method, overloading, list, automatic casting).

  • 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-18T01:47:28+00:00Added an answer on May 18, 2026 at 1:47 am

    You can use the keyword dynamic.

    Here is an example:

    public class Program
    {
        static void Main(string[] args)
        {
            List<IEntity> entities = new List<IEntity>();
            entities.Add(new Mirror(1));
            entities.Add(new Mirror(2));
            entities.Add(new LightBeam(1));
            entities.Add(new LightBeam(2));
    
            //I also fixed your for-loops, you don't need to do entities.Count - 1
            for (int i = 0; i < entities.Count; i++)
            {
                for (int j = i + 1; j < entities.Count; j++)
                    Collide((dynamic)entities[i], (dynamic)entities[j]);
            }
    
            Console.ReadLine();
        }
    
        public static void Collide(Entity e0, Entity e1)
        {
            Console.WriteLine("Collision: IEntity {0}[{1}] and IEntity {2}[{3}].", e0.Name, e0.ID, e1.Name, e1.ID);
        }
    
        public static void Collide(LightBeam lb0, Mirror m0)
        {
            Collide(m0, lb0);
        }
        public static void Collide(Mirror m0, LightBeam lb0)
        {
            Console.WriteLine("Special Collision: Mirror {0}[{1}] and LightBeam {2}[{3}].", m0.Name, m0.ID, lb0.Name, lb0.ID);
        }
    }
    
    //Interfaces are our friends :)
    public interface IEntity
    {
        String Name { get; }
        Int32 ID { get; }
    }
    
    public abstract class Entity : IEntity
    {
        protected Entity(Int32 id = 0)
        {
            ID = id;
        }
    
        public Int32 ID { get; private set; }
        public abstract String Name { get; }
    }
    
    public class Mirror : Entity
    {
        public Mirror(Int32 id = 0)
            : base(id)
        {
        }
    
        public override String Name
        {
            get { return "Mirror"; }
        }
    }
    
    public class LightBeam : Entity
    {
        public LightBeam(Int32 id = 0)
            : base(id)
        {
        }
    
        public override String Name
        {
            get { return "LightBeam"; }
        }
    }
    

    OUTPUT:

    Collision: IEntity Mirror[1] and IEntity Mirror[2].
    Special Collission: Mirror Mirror[1] and LightBeam LightBeam[1].
    Special Collission: Mirror Mirror[1] and LightBeam LightBeam[2].
    Special Collission: Mirror Mirror[2] and LightBeam LightBeam[1].
    Special Collission: Mirror Mirror[2] and LightBeam LightBeam[2].
    Collision: IEntity LightBeam[1] and IEntity LightBeam[2].
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a game in XNA 3 and I have the levels stored in
I am planning on building a 2D game library for XNA, and one of
I'm building with XNA a class that encapsulate a custom geometry defined in a
I'm currently working on building a game which is on a planet, the way
Building a string for post request in the following way, var itemsToAdd = sl.SelProds.ToList();
I'm working on a simple 2D Real time strategy game using XNA. Right now
Building on How Do You Express Binary Literals in Python , I was thinking
Building a client-side swing application what should be notified on a bus (application-wide message
Building the same project (without any changes) produces binary different exe-files: some small regions
Building on what has been written in SO question Best Singleton Implementation In Java

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.