bigxav at April 7th, 2012 10:31 — #1
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
alienizer at April 7th, 2012 11:06 — #2
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.
bigxav at April 7th, 2012 11:26 — #3
Once a new bullet was created how did ur loop know to start adressig and updating that object?
alienizer at April 7th, 2012 11:47 — #4
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.
bigxav at April 7th, 2012 12:54 — #5
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!!
alienizer at April 7th, 2012 13:30 — #6
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.
bigxav at April 7th, 2012 16:21 — #7
How would u store the names of the objects you need to update??? In an array then
alienizer at April 7th, 2012 17:17 — #8
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.
bigxav at April 7th, 2012 19:25 — #9
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
alienizer at April 7th, 2012 19:44 — #10
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.
bigxav at April 7th, 2012 19:53 — #11
... I wish i knew it were that simple when i started... Ill tell you how it goes!
vilem_otte at April 9th, 2012 20:30 — #12
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)
stainless at April 10th, 2012 04:36 — #13
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.
bigxav at April 13th, 2012 11:42 — #14
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.
stainless at April 14th, 2012 05:04 — #15
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.
Object * child;
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.
bigxav at April 17th, 2012 07:27 — #16
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?
stainless at April 17th, 2012 08:25 — #17
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.
bigxav at April 30th, 2012 11:39 — #18
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