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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:28:00+00:00 2026-05-17T01:28:00+00:00

Hey, I have a question regarding code structure, and was wondering what the best

  • 0

Hey, I have a question regarding code structure, and was wondering what the best practice is or if there was a specific design pattern I should be using.

I have an abstract base class BoundingVolume which can have subclasses such as BoundingSphere, BoundingBox, etc. A comparison has to be made between two BoundingVolumes to determine if they overlap, this comparison is made by a World class which keeps track of all objects in the simulation and is responsible for handling collisions between volumes.

My initial thought was that the World class would hold a collection of BoundingVolume objects, and call a virtual CollidesWith method defined on BoundingVolume which would accept another BoundingVolume to make the comparison with. The problem is the algorithm for determining if two BoundingVolumes overlap is different for each subclass, for example the algorithm for sphere collides with sphere is not the same as box collides with box, or box collides with sphere.

Using my initial plan each subclass would have to determine what the actual type of the BoundingVolume being passed to it was and make a switch to use the correct algorithm. Additionally each subclass would have to be extended every time a new subclass is added, effectively eliminating all benefit of having subclasses in the first place. Is there a better way to structure this?

I’m more looking for a solution to situations like this in general than an answer to this specific scenario.

Thanks for your 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-05-17T01:28:00+00:00Added an answer on May 17, 2026 at 1:28 am

    Is there a better way to structure this?

    One solution here is to use double dispatch, which is where the target function is determined by the concrete types of two objects. In languages like C#, the default function call mechanism is single dispatch, i.e. the target function is just determined by the concrete type of a single object.

    In languages like C# or C++, double dispatch is achievable through the visitor pattern. For example, in your case, this might look like the following:

    interface BoundingVolume
    {
        bool CollidesWith(BoundingVolume v);
    
        bool CollidesWith(BoundingBox b);
        bool CollidesWith(BoundingSphere s);
    }
    
    class BoundingBox : BoundingVolume
    {
        public bool CollidesWith(BoundingVolume v)
        {
            return v.CollidesWith(this);
        }
    
        public bool CollidesWith(BoundingBox b)
        {
            Console.WriteLine("box/box");
            return true;
        }
    
        public bool CollidesWith(BoundingSphere s)
        {
            Console.WriteLine("box/sphere");
            return true;
        }
    }
    
    class BoundingSphere : BoundingVolume
    {
        public bool CollidesWith(BoundingVolume v)
        {
            return v.CollidesWith(this);
        }
    
        public bool CollidesWith(BoundingBox b)
        {
            Console.WriteLine("sphere/box");
            return true;
        }
    
        public bool CollidesWith(BoundingSphere s)
        {
            Console.WriteLine("sphere/sphere");
            return true;
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            BoundingVolume v1 = new BoundingBox();
            BoundingVolume v2 = new BoundingSphere();
            Console.WriteLine(v1.CollidesWith(v2));
        }
    }
    

    You could imagine that the box/sphere and sphere/box implementations just call out to a common collision utility function for detecting collisions between a sphere and a box.

    Additionally each subclass would have to be extended every time a new subclass is added, effectively eliminating all benefit of having subclasses in the first place

    Depending on what they do, the subclasses could still give you benefit through other polymorphic behavior, e.g. a ContainsPoint virtual method would return whether or not the specific BoundingVolume contained a Point.

    Essentially, though, you can’t avoid the combinatorial explosion if you have the need to special-case each type of collision since something somewhere will need to say “if I have an X and a Y, how do I compute their intersection efficiently?” This could be a big switch statement, a table of methods, or the double dispatch via visitor pattern mentioned above.

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

Sidebar

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.