GameEntity - Fish-In-A-Suit/Conquest GitHub Wiki

This class represents a renderable model (mesh) along with it's rotation, position in the world and scale.

Class layout

Imports

  • import math.Vector3f;
  • import models.Mesh;

Private instance fields

  • Mesh mesh
  • Vector3f position
  • Vector3f rotation
  • float scale

Constructors

  • GameEntity(float[] vPositions, int[] indices, float[] colours)

Public methods

  • Vector3f getPosition()
  • void updatePosition(float x, float y, float z)
  • Vector3f getRotation()
  • void setRotation(float x, float y, float z)
  • void increaseRotation(float rotx, float roty; float rotz)
  • float getScale()
  • void setScale(float scale)
  • Mesh getMesh()

Explanation

The constructor GameEntity(float[] vPositions, int[] indices, float[] colours) first creates a new Mesh instance out of the given data (which is then stored inside the vao). Then, it sets the default position, rotation and scale. Default position is considered to be (0, 0, 0), scale = 1 and rotation (0, 0, 0).

getPosition() returns Vector3f position of the GameEntity instance. This method is used inside Renderer.render() to construct a translation matrix to move the GeEntity around the virtual world: Matrix4f translationMat = transformation.getTranslationMatrix(entity.getPosition());

updatePosition(float x, float y, float z) is used to add values to the current position denoted as Vector3f. Note that += has to be used to add, since if we would only set position every time, say to 0.5, results wouldn't be correct. This method is used by Game.updateLogic() so as to update the position corresoondingly if any of W, A, S or D are pressed:

public void updateLogic(Window window) {
	if (window.keys[GLFW_KEY_W]) {
          entities[0].updatePosition(0, 0, -1.0f);
	} else if (window.keys[GLFW_KEY_S]){
	    entities[0].updatePosition(0, 0, 1.0f);
	} else if (window.keys[GLFW_KEY_A]) {
            entities[0].updatePosition(-1.0f, 0, 0);
	} else if (window.keys[GLFW_KEY_D]) {
	    entities[0].updatePosition(1.0f, 0, 0);
	} else if (window.keys[GLFW_KEY_SPACE]) {
	    entities[0].updatePosition(0, 1.0f, 0);
	} else if (window.keys[GLFW_KEY_LEFT_SHIFT]) {
	    entities[0].updatePosition(0, -1.0f, 0);
        } else if (window.keys[GLFW_KEY_X]) {
	    entities[0].increaseRotation(0.2f, 0f, 0f);
}

The getRotation() method returns Vector3f rotation, which is used by Renderer.render() to construct a rotation matrix and later send it to the vertex shader: Matrix4f rotationMat = transformation.getRotationMatrix(entity.getRotation().x, entity.getRotation().y, entity.getRotation().z).

The increaseRotation() method is used to add to the current value of rotation denoted as Vector3f when key X, Y, Z or I is pressed:

else if (window.keys[GLFW_KEY_X]) {
	entities[0].increaseRotation(0.2f, 0f, 0f);
} else if (window.keys[GLFW_KEY_Y]) {
	entities[0].increaseRotation(0f, 0.2f, 0f);
} else if (window.keys[GLFW_KEY_Z]) {
	entities[0].increaseRotation(0f, 0f, 0.2f);
} else if (window.keys[GLFW_KEY_I]) {
	entities[0].increaseRotation(0.08f, 0.08f, 0.08f);
}

The getMesh() method is used to get a reference to the mesh instance of the specified GameEntity instance. This method is used inside Renderer.render() to get a reference to the Mesh instance of the GameEntity instance and call it's render method: entity.getMesh().render();