How to make a free camera

(Difference between revisions)
Jump to: navigation, search
(improving)
m
Line 1: Line 1:
 
This tutorial show how to make a free camera using C++.
 
This tutorial show how to make a free camera using C++.
  
You have to already have you MGame derived class. See [[How to add your own C++ code (Game Plugin tutorial)]].
+
You have to already have your MGame-derived class. See [[How to add your own C++ code (Game Plugin tutorial)]].
  
 
First of all we need a centerCursor function. We can take that directly from [[MScript.cpp]] (the lua script functions source code):
 
First of all we need a centerCursor function. We can take that directly from [[MScript.cpp]] (the lua script functions source code):

Revision as of 13:32, 22 August 2013

This tutorial show how to make a free camera using C++.

You have to already have your MGame-derived class. See How to add your own C++ code (Game Plugin tutorial).

First of all we need a centerCursor function. We can take that directly from MScript.cpp (the lua script functions source code):

void MyGame::centerCursor(void)
{
	MEngine * engine = MEngine::getInstance();
	MSystemContext * system = engine->getSystemContext();
	MInputContext * input = engine->getInputContext();

	unsigned int width = 0;
	unsigned int height = 0;
	system->getScreenSize(&width, &height);
	int x = width/2;
	int y = height/2;

	system->setCursorPosition(x, y);
	input->setAxis("MOUSE_X", (float)(x / (float)width));
	input->setAxis("MOUSE_Y", (float)(y / (float)height));
}

On our header file:

MOCamera * camera;
float camera_dx = 0.0f;
float camera_dy = 0.0f;
float mouseX, mouseY, mouseX_prev, mouseY_prev;

On onBeginScene:

	camera = scene->getCameraByName("Camera");

On update, before anything else, we could store the mouse position in the mouseX and mouseY variable. You could do that at the start of the function so that these variables can be used from other code you may write in the future without the need to retrieve the values again:

	MEngine * engine = MEngine::getInstance();
	MInputContext * input = engine->getInputContext();
	mouseX = input->getAxis("MOUSE_X");
	mouseY = input->getAxis("MOUSE_Y");

Still on update, in whichever part of the function you want, we put our actual camera movement code.

MGame::update() is the last thing that should be called, which is the "original" code of the update() function from the base class MGame. So we'll put our camera movement code before that line of code.

	/*	free camera	*/
	//rotate horizontal (X mouse)
	camera->addAxisAngleRotation(camera->getInverseRotatedVector(MVector3(0,0,-1)),camera_dx*100);
	//rotate vertical (Y mouse)
	camera->addAxisAngleRotation(MVector3(-1,0,0),camera_dy*100);
	//move
	if(input->isKeyPressed("W"))
		camera->setPosition(camera->getPosition() + camera->getRotatedVector(MVector3(0,0,-2)));
	if(input->isKeyPressed("S"))
		camera->setPosition(camera->getPosition() + camera->getRotatedVector(MVector3(0,0,2)));
	if(input->isKeyPressed("A"))
		camera->setPosition(camera->getPosition() + camera->getRotatedVector(MVector3(-1,0,0)));
	if(input->isKeyPressed("D"))
		camera->setPosition(camera->getPosition() + camera->getRotatedVector(MVector3(1,0,0)));
	//get mouse direction
	camera_dx = mouseX - mouseX_prev;
	camera_dy = mouseY - mouseY_prev;
	//center cursor
	centerCursor();
	mouseX_prev = input->getAxis("MOUSE_X");
	mouseY_prev = input->getAxis("MOUSE_Y");
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox