C++Game DevelopmentLanguage FeatureLearn C++

How To: Develop A Fun 3D Game Like Minecraft In C++

Minecraft is one of the most popular game in the world, it is a good game to improve creativity of kids. It has very basic graphics with very useful game mechanism. Developing a 3D gamemight be little bit hard for all developers, requires good math skills, higher programming logics, requires optimization techniques, understanding 3D digital environment, matrix operations, camera movements, texture effects, light effects etc.

C++ Builder is easy to build these kind of simple games. C++ is faster programming language that you can develop fully native and faster games. You can use OpenGL or Direct3D libraries or some other 3rd party 3D Engines. In C++ Builder you can directly create your own 3D objects, you can animate them on runtime. Viewport3D (TViewportd3D) component in C++ Builder FireMonkey projects is good to display many basic 3D Objects like Plane, Cube, Sphere, Cone, Plane, Ellipse3D etc. Please see this post about Working With 3D In Modern Windows C++ Development for creating these 3D objects. You can also easily load your 3D objects into Viewport3D by using Model3D (TModel3D).

To create a 3D object to be used in Viewport3D we need to use TMesh classes.  TMesh is a custom 3D shape that can be customized by drawing 3D shapes.  It is a class publishes a set of properties from its ancestor, TCustomMesh, in order to let you design new 3D shapes at design time from within the IDE, through the Object Inspector. Use the Data property to specify the points, normals and textures for each point, and the order in which the resulting triangles are drawn. The designed shape is filled with the material specified through MaterialSource property. If no material is specified, then the shape is filled with red color. Please read more about Learn To Quickly Create Specific 3D Objects In Modern C++ Applications For Windows

1. Create a new C++ Builder Console FMX application, save all project and unit files to a folder. And modify code lines as below;
2. Drag a Viewport3D from Tool Palette on to Form. We will use this to display our 3D map space. Add a Label, Cube, a Sphere, Dummy Object and Camera, 2 Color Materials, 2 Lights to our ViewPort3D by dragging from the Tools Palette. Let’s modify these from design and
– Arrange Label1 Position to top left, we will use this to print and see coordinates
– Cube1 will be our ground. Form Object Inspector, Change it’s Name to Ground1 and set its Width=32, Height=0.01; and Depth=32; and set HitTest =false; Set it’s Material1.
– Sphere1 will be a reference to see center of map, it will be at the 0,0,0 position. You can change its position to understand coordinates in 3D space.
– Dummy1 will be our character, we will move this like user moves in the map. So Drag Camera1 into Dummy1 in the Structure Panel above the Object Inspector Panel, Camera will be eye of our character.
– Add a grass Texture image to ColorMaterial1 and brick Texture image to ColorMaterial2.
– Set both Light Types to Point and, put Lights to upper left and right corners, one light could be gray


3. Let’s add some constants before the __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) line shown as below,

4. Now lets start adding width, height and depth constants about our 3D space environment in our game,

5. Let’s define our st_space structure and let’s create our space with given dimensions above.

6. Now we need to define our step size and grid size, assume 0.5 is like 0.5m,, so our bricks will have 0.5×0.5×0.5 m3 size,

7. Now let’s create our function to create cubes in given x, y, z dimension on runtime. At the end we will put this cube pointer address to space[x][y][z].cube If you are new to 3D objects in C++ Builder, please see this post about Working With 3D In Modern Windows C++ Development for creating these 3D objects.

8. Be sure that your created cube address is safely stored somewhere. Here we store it with space[x][y][z].cube=cube; line. So we can free them from the memory at the end. We must free all those cubes from 3D space at the end. We can write this procedure to free all cubes from their pointer addresses in our space, see below;

9. We finished adding constant, variables and some functions which will be used in Form creation. Now we can setup our ground, and some cubes about at the 0,0,0 point. So we can understand that corner is origin

10. We must free all cubes in map when we close the form, when we start a new game, before loading a new map may be too. Select Form1 and double click to OnClose event and add our function as in given example below,

11. In this step you can run and see your environment on run time. But our character (in real it is our camera in our viewport3D) is not moving. To move our character we will move the Dummy1 object which has camera. So when Dummy1 moves camera will also move and we will see that we are getting closer, far or rotating around. To do this, we will check key presses and our character (Dummy1) will move according to these cases. You can use Key or KeyChar, here we will use KeyChar parameter to check input characters. Select Form1 and double click to OnKeyDown event, add switch case format as below,

Finally we should add actions inside this procedure. As you see we will use lowercase a and d to rotate left and right and w and s to move forward and backwards as in many games. We will use space to build bricks, you can bind b or other keys if you want. To move our character we will use our step variable and according to this our character will move as in this formula,

So, when user presses ‘a’ forward key must update X position along map width and Z position along map depth.

and we can rotate our character, in real this is our alfa angle which is in Y diagonal RotationAngle ıf Dummy1. For example to rotate right we can write this,

Note that 2*PI in Radians is equal to 360 Degrees, and we need to convert Degrees to Radians to use in Sin() and Cos() functions, like in give example

By given information above, now let’s keep editing code above, and let’s add some actions to wasd keys as below,

12. At this step, you can run your application. Press F9 to run and try to move and rotate in the map, you must see sphere at the center and try to see 3 cubes around 0,0,0 position that we generate before.

13. Finally let’s add functionality to space key. first we should calculate x and y coords at the one step ahead as in frontx and fronty. And we should find exact integer numbers which shows x y s position in grid form. This will help to check or modify data at space[X][Y][Z]. Finaly if these X Y Z are in limits we will check if there is cube or not. if there is no cube then we will create a new cube at that point else we will delete this brick from the memory also from the map.

14. This great example has no 3rd party engine, you don’t need to write codes about OpenGL or DirectX etc. It is just native codes and it has some official 3D components. It has less than hundred lines in real, that makes C++ Builder great ! Full code should be like this,

Here is our test result.

After this step, you can enhance your codes. You can limit your character in your map, you can add jumping, climbing to bricks or other abilities. You can add starting, loading and saving map data by using file operations. You can add some more features into st_space structure, animations, effects, mechanics, sound features etc. Hard part might be Server-Client interactions and multi-player environment.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Related posts
C++Introduction to C++Learn C++

Learn To Use Break And Continue In Loops With C++

C++Introduction to C++Learn C++

Learn To Define And Use Arrays In C++

C++Learn C++Videos

Learn C++ With Back to Basics: Design Pattern by Mike Shah (CPPCon 2020) Video

C++Introduction to C++Learn C++

Discover Switch Statements in C++

en_USEnglish