How do game programmers define what 3D objects are?

468 viewsOtherTechnology

For the sake of the question, imagine that I’m making a game engine from scratch by one of the popular programming languages and I would like to put an interactable crate in my game. How does the game know what a crate is? I’m thinking like they create a crate in one of the rendering software, but how do they translate that to a game? Do they do something like

`define “crate” crate.stl`

then refer to it only as “crate” to give it animation, physics and lighting properties by extra code, or does it work completely differently?

A follow up question, I want to give velocity to my crate when it’s propelled by the player. Let’s say I tell it moves on x axis by “5” when “interact” happens, how is x axis or velocity defined?

In: Technology

9 Answers

Anonymous 0 Comments

First off, as others have explained, 3D objects are primarily a collection of points, and how the points link together. The simplest object you can make, and the one that ultimately makes up all other objects is the triangle.

To make a rriangle, you need to define 3 points. Lets call them A,B and C. Now aside from the points, you also need to define the edges, which you can do in one go as a collection of points [A,B,C]. (Imagine drawing lines from A-B, B-C and C-D.

As an extension of thia, to make a square, you only need 4 points, but still need to make it out of triangles, of which you need 2. But triangles can share edges, so you can have a square using 4 points, ABCD, and two triangles, ABC and BCD.

All of 5his is just a collection of numbers in some agreed upon format. Just a way of storing structured data.

Now, this is loaded into memory, and game engines differ. But think of this 3D object that you load from a file as a template, a collection of points and triangles (vertices actually). You use it to create an instance of that object in the game engine.

In the game engine, and the programming part in general, we use object oriented programming to make things easy to deal with. In computer memory, they are all just blocks of memory allocated for each object, which the programming language manages for us, but in the programming language we may have a “class” that describes properties of the object.

We as programmers (someone down the line) need to create that class. An Objecr class might look like

“`
class Object
{
float X;
float Y;
float Z;
float ScaleX;
float ScaleY;
float ScaleZ;
float RotateX;
float RotateY;
float RotateZ;
VertexList Vertices;
}
“`

Basically contains the information needed on positioning something in a 3D space. The neat part about this is you can use the Object class to create multiple Objects in memory. We can give each object different names so we know what it is . Like crate1, crate2, MaxPayne, etc.

The VertexList can also point to just one copy of a crate, so you can have multiple crate objecrs that reuse a set of vertices.

So when a 3D model is loaded into memory, it has a bunch of points that are “fixed” relative to some point of origin. We call this “object space”

When the game engine draws stuff, it takes a copy of these vertices and applies transformations to it based on the object properties.

For example, a triangle might be defined in object space as ((0,0), (0,1), (1,1)) and it stays that way in the object. But when drawn to screen they get copied and adjusted based on where we want to draw it. So an crate with X,Y,Z set to 10,10,10 will appear at a different location than a crate at 20,20,20, but they still use the same crate vertices to draw from.

Then when the game engine needs to draw stuff it looks at this list of objects and has the information of

* what to draw (list of vertices)
* where and hiw to draw it (object)

These are sent as commands to the Graphics card (GPU) and updated each frame.

The rest of the game engine and the game itself is about interacting with these objects via their properties.

Need to check if a crate hit another object? You check its position, do some math, and update the crate or thw other object, or both.

After all the calculations are done (and they must be done in less than the time it takes for a frame, which is for our purposes, 1/60th of a second) all the objects on screen are rendered by the game engine all over again.

Of course this is a huge oversimplification and there are so many optimizations and things going on to make things faster and more efficient.

You are viewing 1 out of 9 answers, click here to view all answers.