What's the best way to separate physics from graphics from game logic with Qt?
-
Hi! I think this may be a question that may be answered within any context, like C++ only. But since I've been using Qt for my college projects, including a videogame which I admit could have been a lot better, I think this may be a good place to start with.
That game I just mentioned, was pretty simple, spaceships moving around shooting each other using inmediate OpenGL mode and basic collision detection. No threads, texturing or even shaders.
Quickly, I found that I had lost the structure of my project since I didn't know where to write each function. For example, one thing that keeps annoying me is the fact that I can only draw OpenGL primitives within the glWidget (I'm almost sure this can be done differently, I may just need to read more), so if I have a subclass which needs to draw something in the OpenGL widget, it simply couldn't, forcing me to send its data to the glWidget in order to render it. This ended up by having a huge-overloaded glWidget, compaired to the other classes I had, to manage controls, game logic or colission detection.
I'm pretty sure there should be a better way to do this, Could you point me in the right direction please? I could even post the whole game code if needed, so you could see more clearly what kind of mess I'm talking about,
Thanks!
-
Hi,
the best way to separate physics from rendering, and also separate game logic from rendering is by using a component-based development approach.This means, your game entity is just a container of components each having one particular functionality. Should your entity interact with the physics system? Then add a physics component. Is it a sprite, a Rectangle, or whatever? Then add these visual components. Should the game logic be triggered every second? Then add a Timer component and add the logic there. Should the entity be controlled by an AI? Add the respective AI component.
We have used this concept in V-Play, which comes with the source code of the QML game API in the SDK. You can download it for free and have a look at it.
You also might find our tutorials and sample games useful to understand how component based systems work in practice.Cheers, Chris