Kochol Game Engine  0.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Camera Movement

Introduction

This tutorial show how to listen to keyboard keys and move your camera. You have to read Hello Kochol Game Engine tutorial before this tutorial

The code for moving the camera

// This tutorial shows how to use keyboard inputs in Kochol Game Engine
// We recommend if you want test KGE change the codes in this tutorial
// You have to read tut01 before this tutorial we will delete old comments in this tutorial
#include "../../Include/kge.h"
#pragma comment(lib, "../../bin/debug/kge.lib")
// The classes for Input/Output are in io namespace
// The Keyboard class is used for getting keys status
// A constant for camera speed
const float fCamSpeed = 3.0f;
// In keys function we will find the pressed keys and move the camera in our world
void keys(float elaspedtime)
{
// We can find the pressed key with KeyDown function in Keyboard class
// If you want to know about a letter key is pressed you have to pass it as uppercase
if (key.KeyDown('W'))
// Move the camera forward
pCam->MoveBF(fCamSpeed * elaspedtime);
if (key.KeyDown('S'))
// Move the camera backward by passing a negative number to MoveBF function
pCam->MoveBF(-fCamSpeed * elaspedtime);
if (key.KeyDown('D'))
// Move the camera Right
pCam->MoveRL(fCamSpeed * elaspedtime);
if (key.KeyDown('A'))
// Move the camera left by passing a negative number to MoveRL function
pCam->MoveRL(-fCamSpeed * elaspedtime);
// Now we want to enable FPS camera by F1 key and disable it with escape key
// It is simple
pCam->AutoRotateByMouse(true);
pCam->AutoRotateByMouse(false);
}
int main()
{
dev.Init(params);
pRen = dev.GetRenderer();
pSnMgr = dev.GetSceneManager();
pCam = pSnMgr->AddCameraNode
(
kge::math::Vector(5, 5, 5),
);
for (int j =0; j < 25; j++)
{
for (int i = 0; i < 25; i++)
{
pMesh = pSnMgr->AddStaticMeshNode("../../media/models/hex.ms3d", true);
if (i % 2 == 0)
pMesh->SetPosition(kge::math::Vector(i * 1.5, 0.0, j * -1.74));
else
pMesh->SetPosition(kge::math::Vector(i * 1.5, 0.0, j * -1.74 + 0.87));
}
}
while (dev.Run())
{
// Store the elapsed time between frames
float fElaspedTime = t.GetTimeElapsed();
// Check for the keys
keys(fElaspedTime);
pRen->BeginRendering(true, true, false);
pSnMgr->RenderAll(fElaspedTime);
pRen->EndRendering();
}
}