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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:11:48+00:00 2026-05-20T12:11:48+00:00

I am currently working on an amateur physic engine for a school project but

  • 0

I am currently working on an amateur physic engine for a school project but I’m stuck because of a C++ issue. The setting is the following :

I have three different classes :

  • RigidBody (abstract)
  • Sphere (inherits from RigidBody)
  • CustomRigidBody (inherits from RigidBody and represents a polyhedra)

I need to check if a collision occurs between a pair of bodies with two eponymous methods. One is used to check for a contact with a Sphere whereas the other is used to check for a contact with a CustomRigidBody. There are several possible scenarii (Sphere/Sphere collision, Sphere/Custom collision, etc…) so these two methods are defined within all of these classes.

In RigidBody, they are abstract :

virtual bool isCollidingWith(Sphere* s_p) = 0;
virtual bool isCollidingWith(CustomRigidBody* rb_p) = 0;

But not in Sphere :

bool isCollidingWith(Sphere* s_p);
bool isCollidingWith(CustomRigidBody* rb_p);

Nor in CustomRigidBody :

bool isCollidingWith(Sphere* s_p);
bool isCollidingWith(CustomRigidBody* rb_p);

In my main program, I have a std::vector<RigidBody*> containing pointers to RigidBodies (the superclass) and I need to check for collisions between objects by pair by calling something like :

for(int i = 1; i < this->bodies_p.size(); ++i)
    for(int j = 0; j < i; ++j)
        if(this->bodies_p[i]->isCollidingWith(this->bodies_p[j]))
            std::cout << " COLLISION BETWEEN " << i << " AND " << j << std::endl;

I was under the impression that C++ would be OK with that but I get the following error message :

Engine.cc:35: error: no matching function for call to ‘RigidBody::isCollidingWith(RigidBody*&)’
RigidBody.h:53: note: candidates are: virtual bool RigidBody::isCollidingWith(Sphere*)
RigidBody.h:54: note:                 virtual bool        RigidBody::isCollidingWith(CustomRigidBody*)

My guess is that it has to do with the fact that the vector of bodies contains pointers to RigidBodies and they are not automatically casted to Sphere* or CustomRigidBody* but I don’t know how to fix the issue.

Thank you for your assistance 😉

  • 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-20T12:11:48+00:00Added an answer on May 20, 2026 at 12:11 pm

    This problem is solved by Double Dispatch. Essentially, you need to add another overload to RigidBody and its derived classes:

    bool isCollidingWith(RigidBody* rb_p) = 0;
    

    In the derived classes, e.g. Sphere, the implementation would look like:

    bool Sphere::isCollidingWith(RigidBody* rb_p)
    {
        return rb_p->isCollidingWith(this);
    }
    

    This works because the first time isCollidingWith is called (in your loop), the version of isCollidingWith(RigidBody*) from the correct derived class is called (via the virtual method). Then, in Sphere::isCollidingWith(RigidBody*), the correct derived class is used via the virtual method. However, this time, this is a Sphere*, so the overload that is called is the isCollidingWith(Sphere*) version.

    In other words:

    1. In your loop:

      this->bodies_p[i]->isCollidingWith(this->bodies_p[j])
      

      will call either Sphere::isCollidingWith(RigidBody*) or CustomRigidBody::isCollidingWith(RigidBody*), depending on the actual type of bodies_p[i]. Assuming it is a Sphere, then we get

    2. In Sphere::isCollidingWith(RigidBody* rb_p):

      return rb_p->isCollidingWith(this);
      

      which calls either Sphere::isCollidingWith(Sphere*) or CustomRigidBody::isCollidingWith(Sphere*), depending on the actual type of rb_p.

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

Sidebar

Related Questions

I am currently working on my own little project, but I have a little
Currently working on a project in MVC-3. Trying to put the following code in
Im currently working on a RTX/Janrain integration with bbPress, but im stuck with a
Currently working on a 3D media engine using C# and I have come across
I am currently working on an ASP.net c# project. I have an aspx page
I currently working on an issue tracker for my company to help them keep
I am currently working on a project and my goal is to locate text
I'm currently working on creating a new C# project that needs to interact with
I am currently working on a project with specific requirements. A brief overview of
I'm currently working on a C based application am a bit stuck on freeing

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.