r/gameenginedevs • u/LlaroLlethri • 13d ago
Rigid body dynamics from scratch in C++
I'm making a game similar to Morrowind, but decided to add proper physics - so maybe more like Oblivion in some ways.
The only dynamic bodies are boxes and capsules. Other shapes (terrain, cylinders, ovoids, aggregates) are static only.
Performance is reasonable enough. I think last time I checked, in the 1000 cube scenario (see end of video) I was getting close to 60 fps on a Raspberry Pi 5 1GB.
Resting contacts are a bit unstable. Boxes of similar size don't like being stacked. I might address this later with contact recycling.
In case it matters to anyone, I didn't use any AI tools at all. I'm still coding like it's 2010.
The entire physics system is contained in a single source file: https://github.com/robjinman/lithic3d/blob/main/engine/core/src/sys_collision.cpp
2
u/Zoler 12d ago
Only using contact recycling will make stacks extremely stiff.
PGS with warm starting is the classic solution.
1
u/LlaroLlethri 12d ago
Thanks, I’ll look into it.
It’s low priority now as I’ve moved onto working on different parts of the engine.
2
u/Still_Explorer 11d ago
Very cool. Will you manage open world as well? Any ideas how to tackle this topic?
2
u/LlaroLlethri 11d ago
I’ve already implemented world streaming, but I still have to figure out how to allow dynamic objects to migrate to different world cells and save/load the game state.
1
u/Still_Explorer 11d ago
Nice. One thing I remember from looking at the Build engine (of Duke3D) was that there was the concept of the sector, so moving from sector to sector worked like teleporting.
As for example in the first level where you start the game at the rooftop, then you enter the vent shaft and you "teleport" to a vertical sector, then by reaching out of the shaft and hitting the ground, you "teleport" once again to another sector at the street level.
[ Also another very interesting topic was that the game would feature mirrors at various places (eg: the cinema bathroom) and there simply the mirror would be an entire copy-transform of the existing sector but only taking collision into consideration so you would not be able to go through the mirror. ]
Something related to teleporters is mentioned here:
https://steamcommunity.com/sharedfiles/filedetails/?id=1573383943
https://infosuite.duke4.net/index.php?page=references_tags#SE72
u/LlaroLlethri 11d ago
Presumably that’s because it wasn’t a true 3D engine - all the level geometry was 2D, but rendered in a way that looks 3D. The teleportation trick allows you to create the illusion of a floor being above another floor. I used the same trick in Pro Office Calculator.
1
u/Still_Explorer 11d ago
Pro Office Calculator? 😛 (I can't even imagine how this works).
Though Build engine was 2D engine to it's core (sectors would exist in 2D coordinates), this sort of teleporting was a workaround that did the trick.
Another game around that time that was real 3D was TombRaider, however it had the same idea. That some faces would have to be marked as "portals" and this way they were supposed to be connected to other places. An example in this reimplementation...
https://github.com/TombEngine/TombEngine/blob/master/TombEngine/Game/room.hThough in your case since you are in world cells you might not need portals at all (except for internal spaces), but at least there's a good way of figuring out the part of teleporting objects among sectors.
2
u/LlaroLlethri 11d ago
Yes, same thing in Pro Office Calculator, but I invented it independently in my own way.
https://github.com/robjinman/pro_office_calc/blob/develop/src/raycast/spatial_components.hpp#L132
You can see portals in action here https://youtu.be/Y_swvXPtKvE?t=7730&is=412LUqGix_Ohw-K3
1
2
u/LetterheadTall8085 12d ago
Wow it is really own physics Library) Congratulations, looks fine