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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:43:36+00:00 2026-05-30T00:43:36+00:00

This is for a small game project with SDL on MinGW/Windows. I am working

  • 0

This is for a small game project with SDL on MinGW/Windows.

I am working on a physics engine, and my idea was to have a Physics::Object, which all physical objects should derive from and it registers itself with a global Physics::System class (it’s a monostate pattern) so that the user doesn’t need to track which objects are included in physics calculations and just needs to call a function like Physics::System::PerformTimestepCalculation(double dt).

This works fine, and I even implemented it using a single derived class Physics::Circle, which is a 2d circle. I was pretty happy with the predictive collision detection, even though I still need to optimise it.

Anyway, I ran into trouble when I started adding other primitives to include in the calculation, e.g. line. The Physics::System::PerformTimestepCalculation(double dt) became littered with calls to Object::GetID() or similar functions (may way to avoid dynamic_cast<>), but I feel dirty.

I did a bit of reading and realised that my the elements of my hierarchy are not substitutable (i.e. the collision between two circles is very different between the collision of two lines).

I like the way my Physics::Objects “self register” with the System class so they automatically get included in the calculations, and I don’t really want to lose this.

There must be some other sensible design paths. How can I better redesign things so non-substitutable objects do not get in the way?

Edit FYI:
In the end I have broken away entity and shape properties, similar to how it was described in the accepted answer, and similar to an entity-component-system model. It means I still have the yuk logic of “is this a circle or a line, and is that a line or a circle?”, but I’m no longer pretending that polymorphism helps me here. It also means I use some sort of factory and can have multiple calculation worlds happening at once!

  • 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-30T00:43:38+00:00Added an answer on May 30, 2026 at 12:43 am

    The most successful publically available physics engines are not very heavy on the ‘patterns’ or ‘object oriented design’.

    Here’s a rundown to back up my, admittedly bold, assertion:

    Chipmunk – written in C, enough said.

    Box2d – Written in C++, and there is some polymorphism here. there’s a hierarchy of shapes (base class b2Shape) with a few virtual function. That abstraction leaks like a sieve, though, and you’ll find lots of casts to leaf classes throughout the source code. There’s also a hierarchy of ‘contacts’, which proves more successful, although with a single virtual function it would be trivial to rewrite this without polymorphism (chipmunk uses a function pointer, I believe). b2Body is the class used to represent rigid bodies, and it is non-virtual.

    Bullet – Written in C++, used in a ton of games. Tons of features, tons of code (relative to the other two). There’s actually a base class that the rigid body and soft body representations extend, but only a small part of the code can make any use of it. Most of the base class’s virtual function relate to serialization (save/load of the engine state), of the two remaining virtual functions soft body fails to implement one with a TODO informing us that some hack needs to be cleaned up. Not exactly a ringing endorsement of polymorphism in physics engines.

    That’s a lot of words, and I haven’t even really started answering your question. All I want to hammer home is that polymorphism is not something that is applied effectively in existing physics engines. And that’s probably not because the authors didn’t “get” OO.

    So anyway, my advice: ditch polymorphism for your entity class. You’re not going to end up with 100 different types that you can’t possibly refactor at a later date, your physics engine’s shape data will be fairly homogeneous (convex polys, boxes, spheres, etc) and your entity data will likely be even more homogeneous (probably just rigid bodies to start with).

    Another mistake that I feel you’re making is only supporting one Physics::System. There is utility in being able to simulate bodies independently of eachother (for instance, for a two player game), and the easiest way to do this is to support multiple Physics::Systems.

    With that in mind, the cleanest ‘pattern’ to follow would be a factory pattern. When users want to create a rigid body, they need to tell the Physics::System (acting as a factory) to do it for them, so in your Physics::System:

    // returning a smart pointer would not be unreasonable, but I'm returning a raw pointer for simplicity:
    rigid_body_t* AddBody( body_params_t const& body_params );
    

    And in the client code:

    circle_params_t circle(1.f /*radius*/);
    body_params_t b( 1.f /*mass*/, &circle /*shape params*/, xform /*system transform*/ );
    rigid_body_t* body = physics_system.AddBody( b );
    

    Anyhoo, kind of a rant. Hope this is helpful. At the very least I want to point you towards box2d. It’s written in a pretty simple dialect of C++ and the patterns applied therein will be relevant to your engine, whether it’s 3D or 2D.

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

Sidebar

Related Questions

Hey all, i've been working on this small project in XNA the goal is
I have the mission to make a small game for a school project. Pictures
I'm implementing a small game and am having trouble getting the physics working properly.
Ok guys just a small game: I have some specifications for a project. At
I am working on a small Scala project. I have the following issue with
We have created a small game, it's working fine in devices, now we want
I started creating a small game engine for an independent project. When writing the
I am working on a small game and game engine in C++ using DirectX.
I'm working on a small game engine. One of the features of it is
I have had a small game (Written in C#) which I've built up over

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.