How to structure my game engine

D7f93025118d5f0bc737b8bb9e806bf8
0
BigXav 101 Apr 07, 2012 at 14:31 opengl

I want to begin work on a game engine, i have the knowledge I need to make a minimal one i believe i know how to check collisions, import a 3d model, and update physics… I just dont know how to structure my game engine… For example i plan on making an object for newly imported models and idk if i should maje every object have its own update function and call that in the openGL update, but with this solution i dont know how to start calling an update of a new object once i add it in real time… Can anyone offer me some guidence or have source to point me to? Thankyou :)

17 Replies

Please log in or register to post a reply.

88dc730f0f71e55be39de0ad103bd9ff
0
Alienizer 109 Apr 07, 2012 at 15:06

I wrote a component-based 2D game once (asteroid kind). Each component has functions such as collision etc. I ran a 60FPS loop and adjusted speed according to that so that when you do it at 120FPS, it still run at the same speed. My loop is where I check for collisions, update trajectories of rocks, bullets etc. and read the user mouse/key commands. I don’t know if this is the best way for your game, but it worked for me.

D7f93025118d5f0bc737b8bb9e806bf8
0
BigXav 101 Apr 07, 2012 at 15:26

Once a new bullet was created how did ur loop know to start adressig and updating that object?

88dc730f0f71e55be39de0ad103bd9ff
0
Alienizer 109 Apr 07, 2012 at 15:47

I keep a list of object instances, and that list gets bigger or smaller. In my loop, I go through the list and update all object in it, including adding or deleting from that list, when I’m done, I pass the data to OpenGL and draw that frame.

D7f93025118d5f0bc737b8bb9e806bf8
0
BigXav 101 Apr 07, 2012 at 16:54

Ok thats how i wanted to do it i just figured there was a better way and didnt know how to keep the list In a .txt file xml? An array IDK! Cuz i thought writing to a file would slow it down a lot!!

88dc730f0f71e55be39de0ad103bd9ff
0
Alienizer 109 Apr 07, 2012 at 17:30

Not to a file no! In memory. But you can have maps in a file and load them on the go when you need them. But your caracters, bullets and stuff, should stay in memory.

I’m not an expert at games, so perhpas someone else may give you better advice. Regardless, whether you use a loop or events or both, they all do the same in the end.

D7f93025118d5f0bc737b8bb9e806bf8
0
BigXav 101 Apr 07, 2012 at 20:21

How would u store the names of the objects you need to update??? In an array then

88dc730f0f71e55be39de0ad103bd9ff
0
Alienizer 109 Apr 07, 2012 at 21:17

What you do is create a structure for each diffrent object, one Bullet stucture, one Green Monster structure and so on. Then you create an instance of those structures (objects) when you need them, and that’s the instance you store in the list array, as pointers. So if you have 10 bullets, you create 10 instances of the Bullet structure (each with diffrent name, location, direction etc), and save those pointers in the array. The structure contain all the info you want, such as name, location, direction, status, color, material and whatever. You decide what the struture is for each object. On each frame, you iterate through your list array and decide which are to be removed, added, changed etc. For example, a bullet will be updated on every frame to move forward in its direction, then on many frames later, it may hit something, so it gets deleted from the list (the object is freed and the pointer removed from the list), and the Green Monster is then updated to change color, or start falling etc., or even more object added for the blood splater etc. At the end of the list iteration, you iterate the list one more time, but this time, it’s only to update OpenGL as to what is to be displayed. Then on the next frame, you do that all over again.

If you set your frame rate to 60, make sure that any moving object such as a bullet, are moving at a constant rate no matter what frame rate you use. If your monter walks at 10 FPS, then you need to make it walk slower at 60FPS.

You can also use events to modify your list. Like mouse and keyboard events. Just lock your list first and unlock it when done, because you don’t want a frame to update it at the same time the mouse is.

D7f93025118d5f0bc737b8bb9e806bf8
0
BigXav 101 Apr 07, 2012 at 23:25

Now what if i was using c++ i would just use the same struct thing but make pointers to my objects with direction and such… Now my array with pointers would i just make that a global variable

88dc730f0f71e55be39de0ad103bd9ff
0
Alienizer 109 Apr 07, 2012 at 23:44

Exactly. When you create a new object, it is a pointer to a memory allocation that contain a copy of the structure. Yes, the list array should be global so any part of your software can access it.

D7f93025118d5f0bc737b8bb9e806bf8
0
BigXav 101 Apr 07, 2012 at 23:53

… I wish i knew it were that simple when i started… Ill tell you how it goes!

B20d81438814b6ba7da7ff8eb502d039
0
Vilem_Otte 117 Apr 10, 2012 at 00:30

It’s not that hard once you try it (and succeed). I’d just like to note that it doesn’t have to be global variable, but one can use singleton class to store all the stuff about game/game engine (for example).

There are many ways how to actually do it, all designed for specific cases -> you’ll most probably invent your own way how to structure the code. Anyway just a little advice - don’t stick to only single way to structure game engine, I’ve moved/changed huge amounts of stuff since beginning and it sitll isn’t perfect in my opinion, by well I’ll let it in pretty decent state for now (and work on other stuff - like games on it).

So don’t be afraid to not-doing stuff perfectly, do it just so much when it becomes decent in your opinion (e.g. when it’s simple to work with for you)

B5262118b588a5a420230bfbef4a2cdf
0
Stainless 151 Apr 10, 2012 at 08:36

Couple of hints I can think of.

Pass the frame time into all objects when you update them, don’t assume the frame rate will be exactly 60hz. It often isn’t. Also if you wanted to do slow motion effects you can do them easily by passing in a slower frame time to each object while still updating the screen at 60 hz

Have a base class that only has constructor, destructor, draw, and update methods in it and base all your objects on this. Even if you are working in c you can do this with a base struct. You can add methods to other objects as you need them, but having a base class is a must.

Have multiple lists of objects instead of a single global list. When you come to doing collision detection you will be really happy you did.

Other than that, experiment. Try stuff out and see what happens. If you find out that it’s not good enough later, you can change it. At least you have learned something and can see something on screen.

Good luck.

D7f93025118d5f0bc737b8bb9e806bf8
0
BigXav 101 Apr 13, 2012 at 15:42

Awesome now I feel like atleast I have a good grasp on how to code everything… I’m looking into making a blob that might be rough still working on displaying stuff on the screen in openGL… before I was using other frameworks now I want to use just openGL. How would I go about making joints between objects I don’t know how I would work that code in, or spring joints.

B5262118b588a5a420230bfbef4a2cdf
0
Stainless 151 Apr 14, 2012 at 09:04

You can attach objects to other things by making them a child of the object.

So when you create your class to hold an object, add a pointer to a second object.

In pseudo code it would be.

 class Object
{
private:
public:
       <usual stuff>
        Object * child;
        void Draw();
};

Object::Draw()
{
         pushmatrix
         translate
         rotate
         drawmesh
         if (child)
             child->Draw();
         popmatrix
}

Then the object will always be attached to the parent.

This link does not have to be a fixed link, you can rotate the child around the parent, move it relative to the parent, do what you like.

D7f93025118d5f0bc737b8bb9e806bf8
0
BigXav 101 Apr 17, 2012 at 11:27

Where would i handle collision detection? Would that take place in the base class holding all the pointers to the classes or would I pass one class to another?

B5262118b588a5a420230bfbef4a2cdf
0
Stainless 151 Apr 17, 2012 at 12:25

The way I handle it is that I have a method CalcBoundingVolume on all objects.

This method is called at a few points, when you load a mesh into an object, when you add a child mesh, and when I distort a mesh. (explosions et al)

This is recursive on the children as well.

The parent object has two bounding volumes, one that contains itself AND all the children, one that just contains itself.

When I need to do a collision detect, I check against the big one first, then check against the sub volumes.

Note I always use rays for collision detection, so I can handle the case when the ray strikes multiple objects (parent and one or more children). I calculate the position along the ray for each object and take the shortest length as the only collision.

D7f93025118d5f0bc737b8bb9e806bf8
0
BigXav 101 Apr 30, 2012 at 15:39

I have a question about using a scripting language inline with the game engine. How can I make a game engine in c++ and actually code the game in a scripting language such as python. Also what are the benefits of this?

finally I have another question about collisions what if I have one object with multiple triangles. I know how I would detect a collision between triangle and triangle but how would I check a full mesh to another mesh? break up the triangles and detect each alone