Game Items

Learn how to display objects on the screen.

Now that we have a black screen, it is time for us to add in objects (GameItems).

Creating a Game Item

Let's make a basic Game Item. To start, let's go to the MainScene and construct a new GameItem in the loadGraphics() method.

GameItem gameItem = new GameItem();

That constructs an empty game item named gameItem. If you were to run the game you would see nothing for two reasons:

  1. The game item has not been added to the scene.

  2. The game item has no mesh.

Let's deal with issue #1 first. When you construct a game item it just sits in memory. In order for us to get that game item to appear in a scene we must add it. The AbstractGameScene includes an add() utility method. The add() method allows you to add more than just GameItems, which we will cover in the future.

GameItem gameItem = new GameItem();
add(gameItem);

Once again if you run the code you see nothing. That is due to reason #2.

Meshes

A mesh represents a 3D geometric figure. In order for us to see a 3D object we need to use a Mesh. GameItems can be made from one or more meshes. (GameItems can also be empty as seen above.) Meshes can be generated via code or loaded through 3D files (such as .obj files). We will learn about the latter at a later point in time.

Let's have the gameItem be a cube. Start off by defining a new mesh variable above our GameItem declaration.

The mesh constructor takes several parameters.

Mesh cubeMesh = new Mesh(positions, textureCoords, normals, indices);

Positions (Vertices)

The positions value is a float array which contains all of the vertices for the Mesh. The length of the array is the number of vertices * 3.

Texture Coordinates (UVs)

Texture coordinates (also commonly called uvs) are the texture coordinates for each vertex. Texture coordinates range from 0-1 and are 2D vectors. (0, 0) is the top left while (1, 1) is the bottom right. The length of the array should be the number of coordinate * 2. (Note: The order of the vectors matter.)

Normal Coordinates

The normal coordinates represent the orthogonal vector for each vertex. Once again it needs to be in the same order as the position vectors.

Indices

There is often more than one of a single position vector. To save memory indices are used. Indices represent the true order of vectors. It is an int array with the numbers representing a certain position vertex.

Check out a more detailed explanation on what these values are.

A Cube Mesh

The Kakara Engine has predefined definitions for primitive objects, such as a cube. The data for a cube is stored in the CubeData class. Using the cube data class you can create your cubic mesh.

Mesh cubeMesh = new Mesh(CubeData.vertex, CubeData.texture, CubeData.normal, 
    CubeData.indices);

Materials

Now that we have our cube mesh, we need a way to texture it or give it color. To do that we assign meshes a material. We start off by constructing a material class.

Material cubeMaterial = new Material();

By default that creates a Material with a white color. We will learn about adding textures in the next chapter.

Now that we have our material created, we need to add it to the mesh we created.

cubeMesh.setMaterial(cubeMaterial);

Giving Game Items a Mesh

Now it is time to give our game item a mesh. To do that we just pass a mesh into the constructor.

GameItem gameItem = new GameItem(cubeMesh);

Transformations

If you run the code you will notice that you still don't see anything. That is because the Camera and the Cube are at the same location. We must move the cube farther back on the Z axis in order to see it. We do that by changing the position of the Transform component. For now don't worry about what components are, we will learn about them in the next chapter.

We can access the Transform component through a public field in the GameItem class. From there we can call the setPosition() method.

gameItem.transform.setPosition(0, 0, -10);

Now if you run the code we should see a gray square. In the next chapter we will learn about components and how to make our cube rotate.

Final Code

import org.kakara.engine.GameHandler;
import org.kakara.engine.engine.CubeData;
import org.kakara.engine.gameitems.GameItem;
import org.kakara.engine.gameitems.Material;
import org.kakara.engine.gameitems.mesh.Mesh;
import org.kakara.engine.scene.AbstractGameScene;

public class MainScene extends AbstractGameScene {
    public MainScene(GameHandler gameHandler) {
        super(gameHandler);
    }

    @Override
    public void work() {

    }

    @Override
    public void loadGraphics(GameHandler gameHandler) throws Exception {
        Mesh cubeMesh = new Mesh(CubeData.vertex, CubeData.texture, 
            CubeData.normal, CubeData.indices);
        Material cubeMaterial = new Material();
        cubeMesh.setMaterial(cubeMaterial);

        GameItem gameItem = new GameItem(cubeMesh);
        gameItem.transform.setPosition(0, 0, -10);
        
        add(gameItem);
    }

    @Override
    public void update(float v) {

    }
}

Last updated