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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:02:24+00:00 2026-05-29T08:02:24+00:00

I am making a physics engine in c# / XNA, I have three basic

  • 0

I am making a physics engine in c# / XNA, I have three basic objects…

Sphere
Cube
Plane

that are all derived from

GameObject

I store all my objects in a list of GameObjects and I would like to loop through this list and be able to call a CheckCollision function that will go to the correct function for each pair of objects

eg

go is a Sphere,

go2 is a Sphere

if(CheckCollision(go, go2))
{
  //do stuff
}

bool CheckCollision(Sphere one, Sphere two)
{
  //Check Sphere to Sphere
}

bool CheckCollision(Sphere sphere, Plane plane)
{
  //Check Sphere to Plane
}

and I would like it to just go to the correct function withour having to use if checks.

Thank you.

  • 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-29T08:02:26+00:00Added an answer on May 29, 2026 at 8:02 am

    You could use virtual dispatch:

    abstract class GameObject
    {
        abstract bool CheckCollision (GameObject other);
        abstract bool CheckCollision (Sphere other);
        abstract bool CheckCollision (Cube other);
        abstract bool CheckCollision (Plane other);
    }
    
    class Sphere : GameObject
    {
        override bool CheckCollision (GameObject other) { return other.CheckCollision(this); }
        override bool CheckCollision (Sphere other) { /* ... implementation ... */ }
        override bool CheckCollision (Cube other) { /* ... implementation ... */ }
        override bool CheckCollision (Plane other) { /* ... implementation ... */ }
    }
    
    class Cube : GameObject
    {
        override bool CheckCollision (GameObject other) { return other.CheckCollision(this); }
        override bool CheckCollision (Sphere other) { /* ... implementation ... */ }
        override bool CheckCollision (Cube other) { /* ... implementation ... */ }
        override bool CheckCollision (Plane other) { /* ... implementation ... */ }
    }
    
    class Plane : GameObject
    {
        override bool CheckCollision (GameObject other) { return other.CheckCollision(this); }
        override bool CheckCollision (Sphere other) { /* ... implementation ... */ }
        override bool CheckCollision (Cube other) { /* ... implementation ... */ }
        override bool CheckCollision (Plane other) { /* ... implementation ... */ }
    }
    

    EDIT

    Consider what happens when you have this:

    GameObject go1 = new Sphere();
    GameObject go2 = new Cube();
    bool collision = go1.CheckCollision(go2);
    
    • The call goes to the abstract GameObject.CheckCollision(GameObject) method on the sphere.
    • Virtual dispatch means that we go to Sphere.CheckCollision(GameObject)
    • Sphere.CheckCollision(GameObject) calls the abstract GameObject.CheckCollision(Sphere) method on the cube.
    • Virtual dispatch means that we go to Cube.CheckCollision(Sphere)!

    Therefore, no type-checking if statements are necessary.

    EDIT 2

    See Eric Lippert’s answer at https://stackoverflow.com/a/2367981/385844; the first option — the visitor pattern — is essentially the approach outlined above. Eric’s other answer at https://stackoverflow.com/a/9069976/385844 also discusses this issue.

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

Sidebar

Related Questions

Making a flash page that can cycle through these three images on mouseclick. For
Making this tutorial: http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html I have ListActivity-derived class and onCreateContextMenu, onContextItemSelected overrides. I think
I am making a toy physics engine which works with floating point numbers which
im making an application where i have to load my database from html file
I've been making a game which uses the Box2D physics engine, and I've come
I am working on making a physics engine (with the help of a book),
Does anyone know a very simple physics engine, or just a set of basic
I am making a 2d engine using Java based on entities. The physics and
I'm using Box2d for a Bike Physics Game, Box2d lets you have fixtures that
Making my first steps with NHibernate, I'm trying to have it creating my Tables

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.