Camera movement and rotation

Make the main player movable

If you chose to skip the last page, fair play just make sure you have this include in the file that was covered as we will need to make use of it's features.

#include <Unified-Engine/Objects/scriptObject.h>

Here we need to care about another concept called delta time, it's the time taken per frame which alows us to maintain the same speeds no matter what frame rate we have. This requires a new header from:

#include <Unified-Engine/Core/time.h>

Now we start of by defining an empty scriptable object for the camera:

class CameraControl : public UnifiedEngine::ScriptableObject{
public:
    UnifiedEngine::GameObject* OBJ;

    CameraControl(UnifiedEngine::GameObject* Par, UnifiedEngine::ObjectComponent* Camera)
        : UnifiedEngine::ScriptableObject(Par)
    {
        
    }

    int Update(){
        
    }
};

We don't care about the render function as this is meant to be a controller. We want to take on the camera as it is not our parent. Instead we want to use a intermediate gameobject. Why? well because we may want to in the use physics with the camera object.

Now that we have a basic definition we want to load it:

// Lock the mouse
UnifiedEngine::InputPointer->Mouse.Cursor.Locked();

UnifiedEngine::GameObject CamOBJ(UnifiedEngine::Mesh{}, nullptr);
CameraControl Controller(&CamOBJ, &Cam);

We also make use of the InputPointer to lock the cursor. Locked cursor means it becomes invisible when the application is opened and all the mouse positions are a distance from the screen center.

Lets dealt with the constructor:

OBJ = Par;

OBJ = Par;
OBJ->Name = "CamCam";

UnifiedEngine::instantiate(OBJ);

this->OBJ->Children.push_back(Camera);
Camera->Parent = OBJ;

A lot of this could be moved outside including the attaching the camera to the gameobject and instantiating the gameobject. But for the time being it neatens up our main function and so it will stay.

Next up is the update function, there is a lot of code here and so I will go over a explication after:

//Player Movement
if (UnifiedEngine::__GAME__GLOBAL__INSTANCE__->Input->Keyboard.KeyPressed(UnifiedEngine::Key_W))
    this->OBJ->transform.Position += 1.f*UnifiedEngine::__GAME__GLOBAL__INSTANCE__->GetMainCamera()->front * UnifiedEngine::Time.DeltaTime;
if (UnifiedEngine::__GAME__GLOBAL__INSTANCE__->Input->Keyboard.KeyPressed(UnifiedEngine::Key_A))
    this->OBJ->transform.Position -= 1.f*UnifiedEngine::__GAME__GLOBAL__INSTANCE__->GetMainCamera()->right * UnifiedEngine::Time.DeltaTime;
if (UnifiedEngine::__GAME__GLOBAL__INSTANCE__->Input->Keyboard.KeyPressed(UnifiedEngine::Key_S))
    this->OBJ->transform.Position -= 1.f*UnifiedEngine::__GAME__GLOBAL__INSTANCE__->GetMainCamera()->front * UnifiedEngine::Time.DeltaTime;
if (UnifiedEngine::__GAME__GLOBAL__INSTANCE__->Input->Keyboard.KeyPressed(UnifiedEngine::Key_D))
    this->OBJ->transform.Position += 1.f*UnifiedEngine::__GAME__GLOBAL__INSTANCE__->GetMainCamera()->right * UnifiedEngine::Time.DeltaTime;
if (UnifiedEngine::__GAME__GLOBAL__INSTANCE__->Input->Keyboard.KeyPressed(UnifiedEngine::Key_SPACE))
    this->OBJ->transform.Position += 1.f*UnifiedEngine::__GAME__GLOBAL__INSTANCE__->GetMainCamera()->up * UnifiedEngine::Time.DeltaTime;
if (UnifiedEngine::__GAME__GLOBAL__INSTANCE__->Input->Keyboard.KeyPressed(UnifiedEngine::Key_LEFT_SHIFT))
    this->OBJ->transform.Position -= 1.f*UnifiedEngine::__GAME__GLOBAL__INSTANCE__->GetMainCamera()->up * UnifiedEngine::Time.DeltaTime;

//Player Rotation
glm::vec2 mPos = UnifiedEngine::__GAME__GLOBAL__INSTANCE__->Input->Mouse.GetPosition();

//Update pitch yaw and roll
UnifiedEngine::__GAME__GLOBAL__INSTANCE__->GetMainCamera()->transform.Rotation.x += static_cast<GLfloat>(mPos.y) * 10.f * UnifiedEngine::Time.DeltaTime;
UnifiedEngine::__GAME__GLOBAL__INSTANCE__->GetMainCamera()->transform.Rotation.y += static_cast<GLfloat>(mPos.x) * 10.f * UnifiedEngine::Time.DeltaTime;

if (UnifiedEngine::__GAME__GLOBAL__INSTANCE__->GetMainCamera()->transform.Rotation.x > 89.f)
    UnifiedEngine::__GAME__GLOBAL__INSTANCE__->GetMainCamera()->transform.Rotation.x = 89.f;
else if (UnifiedEngine::__GAME__GLOBAL__INSTANCE__->GetMainCamera()->transform.Rotation.x < -89.f)
    UnifiedEngine::__GAME__GLOBAL__INSTANCE__->GetMainCamera()->transform.Rotation.x = -89.f;

if (UnifiedEngine::__GAME__GLOBAL__INSTANCE__->GetMainCamera()->transform.Rotation.y > 360.f || UnifiedEngine::__GAME__GLOBAL__INSTANCE__->GetMainCamera()->transform.Rotation.y < -360.f)
    UnifiedEngine::__GAME__GLOBAL__INSTANCE__->GetMainCamera()->transform.Rotation.y = 0.f;

//Resolution with scroll
UnifiedEngine::WindowConfig conf = UnifiedEngine::__GAME__GLOBAL__INSTANCE__->__windows.front()->Config();

int scroll = UnifiedEngine::__GAME__GLOBAL__INSTANCE__->Input->Mouse.Scroll();

if(scroll == 1){
    conf.res_x *= 2;
    conf.res_y *= 2;
}
else if(scroll == -1){
    conf.res_x /= 2;
    conf.res_y /= 2;
}

if (conf.res_x <= 80)
    conf.res_x = 80;
if (conf.res_y <= 45)
    conf.res_y = 45;

if (conf.res_x >= 1280)
    conf.res_x = 1280;
if (conf.res_y >= 720)
    conf.res_y = 720;

UnifiedEngine::__GAME__GLOBAL__INSTANCE__->__windows.front()->LoadWindowConfig(conf);

return 0;

Starting with player movements, you can see a list of if statements all covering different key presses. These are standard game controls for movements. Each line within the statements will modify the GameObjects transforms position which controls where it is in world space. They all contain the UnifiedEngine::Time.DeltaTime demonstrating it's necessity. Try removing it and seeing how it affects movements. We also make use of the games main camera and the world and facing directions (Note: cameras ViewUp != Up).

Next up is the camera rotation, which is a little bit more simple. Where we simply get the mouse move distance and then increment both X and Y camera rotations using those mouse movements.

At the end is a little example of the scroll wheel and updating the games window. There is no need to have a full understanding as this is not a component of the tutorial, I just thought it would be interesting to show you how it can be used, in the case of altering the resolution of the game.

Last updated