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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:10:45+00:00 2026-06-02T21:10:45+00:00

Consider that I have a symmetrical relationship matrix, similar to this: Except that each

  • 0

Consider that I have a symmetrical relationship matrix, similar to this:

Except that each “outcome” is a small piece of code.

My scenario: I have a bunch of Entity objects that “collide” with eachother. Each entity has a CollisionType value (an enum). In the design, a relationship matrix exists which describes how the entities behave when different CollisionTypes meet each other.

I’m wondering: How would I represent the relationships, and also implement logic on top of it, in an clean and high-performance manner which is easy to add new CollisionTypes to? In my mind it looks something like a 2D Switch statement.

Example (poor) solution:

void CollideEntities( Entity e1, Entity e2 ) {
    CollisionType t1 = e1.GetCollisionType();
    CollisionType t2 = e2.GetCollisionType();

    // perform basic logic based on t1 & t2
    if ( (t1 == COL_SOLID && t2 == COL_SQUISHY) || (t1 == COL_SQUISHY && t2 == COL_SOLID) ) {
        // do stuff..
    } else if ( (t1 == COL_SOLID && t2 == COL_DAMAGE) || (t1 == COL_DAMAGE && t2 == COL_SOLID) ) {
        // do other stuff..
    } // and so on...

}

Many potential solutions are apparent to me, but none of them strike me as particularly clean or efficient or easy to add new types to…

  • 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-02T21:10:48+00:00Added an answer on June 2, 2026 at 9:10 pm

    Try this:

    #include <vector>
    #include <iostream>
    
    class Symmetric_matrix {
      private:
        size_t size1;
        // The next should be <bool> rather than <int>,
        // but the writer's compiler objects.
        std::vector<int> outcomes;
      public:
        size_t size() const { return size1; }
        int &operator()(const size_t i, const size_t j) {
            const size_t a = i <= j ? i : j;
            const size_t b = i <= j ? j : i;
            return outcomes[(b*(b-1))/2 + a];
        }
        Symmetric_matrix(const size_t size0)
          : size1(size0), outcomes((size()*(size()-1))/2, false) {}
    };
    
    // Here is a test driver.
    int main() {
        Symmetric_matrix sm(5);
        sm(0, 1) = true;
        sm(0, 3) = true;
        sm(1, 3) = true;
        sm(2, 3) = true;
        sm(3, 4) = true;
        std::cout << "buyer-approver      : " << sm(0, 2) << "\n";
        std::cout << "approver-buyer      : " << sm(2, 0) << "\n";
        std::cout << "approver-requisition: " << sm(2, 3) << "\n";
        std::cout << "requisition-approver: " << sm(3, 2) << "\n";
        return 0;
    }
    

    Your question is an interesting one. As you have observed, one need only store the upper or the lower triangle of the matrix, not both.

    But what’s the (b*(b-1))/2 about, you ask? Answer: it comes of the curious arithmetical fact that 0 + 1 + 2 + … + (b-1) == (b*(b-1))/2 (try it).

    Of course, my sample code could stand some improvement. For one thing, for some reason (advice is requested), my code fails when it uses a std::vector<bool>, so I have used a std::vector<int> as a workaround. For another, it does not include proper handling for the case i == j. What it does do however is to convey the essential technique. You can fill out details at your discretion.

    (Update: It has later occurred to my why the std::vector<bool> fails. It fails because std::vector<bool> is implemented as an array of bits, whereas a single bit cannot be an lvalue because it has no address of its own. With clever coding, by having the operator()() return a manipulator of some specially defined type, one could probably finesse the problem without altering main(), but it is probably easiest just to define and use a set() member function if the <bool> is what we want to use.)

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

Sidebar

Related Questions

Consider this scenario. I have my own website, that I use as my identifier,
Assume that we have multiple arrays of integers. You can consider each array as
Consider following scenario: I have RESTful URL /articles that returns list of articles user
I have what I would consider a small sized iPhone app that uses SQLite.
Consider that i have two entities with following relationship: Entity A <-->> Entity B
Consider that I have 100 instances of a squeaker class. Each squeaker object is
Consider that I have opened a file, E:\code\module1\souce\temp.c , in Source Insight. Then in
Consider that I have 1 resource and 2 urls (let's say new one and
I have a chat application(Socket Programming) , for that consider I have 2 separate
I have a configuration file that I consider to be my base configuration. I'd

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.