How do game programmers define what 3D objects are?

579 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

It’s all just pretend. The computer never has any idea what a 3D object is. There are 3 main parts:

First the programmer just defines a group of data that will represent 3D objects. That data will usually at the very least have 3 numbers, which the programmer decides will represent XYZ position coordinates, and maybe some other numbers that will represent rotation, size, velocity, etc. There will also be a way to load a list of 3d coordinates (just more arbitrary numbers) from the 3D model file. To the computer it’s all just a list of numbers, so it’s loaded and stored just like any other data.

Next is the “editor.” This could be a full 3D level editor that outputs a file our game can read, or it could be just typing a text file by hand that says something like `[crate 0,3,4] [player 8,9,2]….`, specifying which objects to spawn at which coordinate and whatever other data we need. It really doesn’t matter, since we’re the programmer we get to define how our game reads the data, so we can make our game read whatever random data we want, how we want. Maybe we also include the file path to the 3D model in that level file, or maybe we program our game where if it sees `[crate X,Y,Z]` it just automatically looks for `crate.stl` in a specific folder. It’s all up to us.

This is still all just random data to the computer, nothing but a massive list of numbers stored in memory. In order to display it to the screen, we tell the computer to go through all of those points we loaded from the 3D file, calculate `point + object_position` and for every 3 points draw a triangle. In order to get the 3D effect, we do some math on those points to “project,” them into the screen, which is basically the same thing you do by hand when you draw a “3D cube,” on a piece of paper. At the end of the day, we’re just drawing 2D shapes on a monitor. We then write a “shader,” which is a small bit of code that tells the computer how to color each pixel of that triangle, which usually uses some 3D math to calculate lighting and such.

We tell the computer to do the previous step for every object, 60+ times a second. If we include a piece of code that says “every frame, take the position and add the object’s velocity to it,” then the object will be automatically drawn at the new position on the next frame since it’s recalculating every frame anyways. 

Sorry if this is an overload. 3D graphics is a LOT of information, but each step is relatively simple to understand, so if you’d like me to break down a specific step further just let me know (I’ve worked as both a 3D graphics dev and a 3rd grade teacher, so I should be able to answer any question you have).

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