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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:59:15+00:00 2026-05-21T18:59:15+00:00

I have created a physics system that handles any collision object to any collision

  • 0

I have created a physics system that handles any collision object to any collision object like so:

namespace Collision
{
    template <typename T, typename U>
    inline void Check(T& t, U& u)
    {
        if(u.CheckCollision(t.GetCollider()))
        {
            u.HitBy(t);
            t.Hit(u);
        }
    }
}

and there are several other helper objects to make it easy to use, but the gist is that there are dynamic objects that need to be tested against static objects and other dynamic objects, but static objects don’t need to be checked.

What I would like is something like this:

void func()
{
    PhysicsWorld world;
    shared_ptr<CSphere> ballPhysics(new CSphere(0,0,ballSprite->Width()));
    BallCommand ballBehavior;
    CBounds bounds(0, 0, 640, 480);
    CBox obstacle(200, 150, 10, 10);

    Collision::Collidable<CBounds> boundC(bounds);
    Collision::Collidable<std::shared_ptr<CSphere>, BallCommand&> ballC(ballPhysics, ballBehavior);
    Collision::Collidable<CBox> obstC(obstacle);

    world.addStatic(boundC);
    world.addDynamic(ballC);
    world.addStatic(obstC);
    ...
    ...
    world.Update();
    ...
    ...
}

I’d love to deduce the containers through the add functions so using the system automatically updates the type lists. I think I get how to generate a typelist with a template function, but not how to then get it where I need it, or at what point in compilation it is complete.

If not that then some system using two typelists that then internally writes the update function to iterate through all the lists pairing them up against each other.

I’ve read some of the boost MPL book and read Andrei’s book several times. But, I seem to get caught up in the how it works stuff and don’t really translate that into how do I use it. I wish they had one more section on real world examples in the MPL book.

I’ve been able to get all of the pieces of a game engine to interact with rendering, physics, collisions (I separate detection from reaction), input, network, sound, etc. All in generic ways. Now I just need to hold all the things in a generic way. After all that generic work, it would be silly to require inheritance just so I can hold something in a container and I don’t want to hand code every collection possibility as that is one of the great benefits of generic programming.

I saw Jalf had indicated that s/he used MPL to do something similar, but did not go into details enough for me to figure it out. If anyone knows a practical use example or where I can get more info on using the MPL I’d be grateful.

Thanks again!

Update

boost MPL and boost Fusion both seem to do what I want, but there appears to be very little in the way of good real life examples of either libraries. The documentation for MPL is little more than this template does this and good luck understanding the implications of that. Fusion is a bit better with “Here’s an example but it’s just the tip of the iceberg!”

A typical boost MPL example is has_xxx. They use XXX and xxx in the example making it difficult to see the difference where XXX(The required text) and Test or CheckType or any more distinguishable user type could be used in place of xxx. Plus there is no mention that none of this is in a namespace. Now I know why Scott meyers compared this to the shower scene in Psycho.

It’s a shame really because what little I have gotten to compile and understand does really useful things, but is so hard to figure out I would never spend this much effort if I was on a shipping product.

If anyone knows real world examples or better references, explanations, or tutorial I would be grateful.

Update

Here’s more code:

template <typename T, typename V = VictimEffect, typename M = MenaceEffect>
class Collidable
{
    T m_Collider;
    V m_HitBy;
    M m_Hit;

public:
    Collidable(T collide, V victim, M menace) : m_Collider(collide), m_HitBy(victim),         m_Hit(menace) {;}
    Collidable(T collide) : m_Collider(collide) {;}
    Collidable(T collide, V victim) : m_Collider(collide), m_HitBy(victim) {;}

    T& GetCollider()
    {
        return m_Collider;
    }

    template <typename V>
    void HitBy(V& menace)
    {
        m_HitBy.HitBy(menace.GetCollider());
    }

    template <typename V>
    void Hit(V& victim)
    {
        m_Hit.Hit(victim.GetCollider());
    }

    template <typename V>
    bool CheckCollision(V& menace)
    {
        return m_Collider.CheckCollision(menace);
    }
};

Then to use it I do this

    Collidable<Boundary, BallCommand> boundC(boundary, ballBehavior);
    Collidable<CollisionBox> ballC(circle);

Then all I need is to call collide with all my active collidable objects against all my active and passive objects.

I’m not using std::function because the addition of function names makes the code clearer to me. But maybe that’s just legacy thinking.

  • 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-21T18:59:16+00:00Added an answer on May 21, 2026 at 6:59 pm

    This is not complete And I did not get everything I want, but it’s good enough for now. I’m entering the whole solution in case it helps others.

    #include <boost\mpl\vector.hpp>
    #include <boost\mpl\fold.hpp>
    #include <boost\mpl\for_each.hpp>
    #include <boost\mpl\inherit.hpp>
    #include <boost\mpl\inherit_linearly.hpp>
    #include <iostream>
    #include <vector>
    
    using namespace boost::mpl::placeholders;
    
    typedef boost::mpl::vector<short, long, char, int> member_types;
    
    template <typename T>
    struct wrap
    {
        std::vector<T> value;
    };
    
    typedef boost::mpl::inherit_linearly<member_types, boost::mpl::inherit<wrap<_2>, _1> >::type Generate;
    
    class print
    {
        Generate generated;
    
    public:
        template <typename T>
        void operator()(T)
        {
            std::cout << *static_cast<wrap<T>&>(generated).value.begin() << std::endl;
        }
    
        template <typename T>
        void Add(T const& t)
        {
            static_cast<wrap<T>&>(generated).value.push_back(t);
        }
    };
    
    void main()
    {
        print p;
    
        short s = 5;
        p.Add(s);
        long l = 555;
        p.Add(l);
        char c = 'c';
        p.Add(c);
        int i = 55;
        p.Add(i);
    
        boost::mpl::for_each<member_types>(p);
    }
    

    This isn’t the final object I need, but now I have all the pieces to make what I want.

    Update

    And finally I get this.

    template <typename TL>
    class print
    {
        template <typename T>
        struct wrap
        {
            std::vector<T> value;
        };
    
        typedef typename boost::mpl::inherit_linearly<TL, boost::mpl::inherit<wrap<_2>, _1> >::type Generate;
        Generate generated;
    
    public:
        void Print()
        {
            boost::mpl::for_each<TL>(*this);
        }
    
        template <typename T>
        void operator()(T)
        {
            std::cout << *static_cast<wrap<T>&>(generated).value.begin() << std::endl;
        }
    
        template <typename T>
        void Add(T const& t)
        {
            static_cast<wrap<T>&>(generated).value.push_back(t);
        }
    };
    

    Here TL is a boost::mpl container of what types should be held.

    I think that provides a good starting point for expanding, but covers much of the metaprogramming parts.

    I hope this helps others.

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

Sidebar

Related Questions

I have a C++ code that's a physics simulation tool. I would like to
I have created a template for Visual Studio 2008 and it currently shows up
I have created a PHP-script to update a web server that is live inside
I have created a UserControl that has a ListView in it. The ListView is
I have created a few small flash widgets that stream .mp3 audio from an
i have created a workflow activity that do give the item creater of a
I have created alot of stored procedures in my application that return the result
Have created a c++ implementation of the Hough transform for detecting lines in images.
I have created a custom dialog for Visual Studio Setup Project using the steps
I have created a C# class file by using a XSD-file as an input.

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.