Third-Person Camera Using View And Projection Matrices

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
kabbotta
Posts: 4
Joined: Wed Apr 22, 2015 9:38 pm

Third-Person Camera Using View And Projection Matrices

Post by kabbotta »

I'm trying to implement the third-person camera described in a microsoft C# tutorial here: https://msdn.microsoft.com/en-us/librar ... 31%29.aspx

I think I'm close, but it still isn't quite working. The bulk of the code is in my Camera update method:

Code: Select all

 
rot_matrix.setRotationDegrees(target->getRotation());
rot_matrix.transformVect(offset);
 
view_matrix = rot_matrix.buildCameraLookAtMatrixRH(
  offset + target->getPosition(),
  target->getPosition(),
  vector3df(0, 1, 0));
 
proj_matrix = rot_matrix.buildProjectionMatrixPerspectiveFovRH(
  node->getFOV(),
  node->getAspectRatio(),
  node->getNearValue(), node->getFarValue());
 
node->setProjectionMatrix(proj_matrix);
node->setPosition(offset + target->getPosition());
node->setTarget(target->getPosition());
 
Right now it seems to follow the player, but it also seems to automatically begin zooming in, and it never stops. The target variable is the ISceneNode for my player entity that is being driven by input.

Any suggestions would be much appreciated. I'm only just beginning to the get the idea of the view and projection matrices.
kabbotta
Posts: 4
Joined: Wed Apr 22, 2015 9:38 pm

Re: Third-Person Camera Using View And Projection Matrices

Post by kabbotta »

Ok, I've realized a couple things I was doing wrong, but it still isn't working correctly. I was confused about how to create the view/projection matricies. This is what I'm doing now:

Code: Select all

 
rot_matrix.setRotationDegrees(target->getRotation());
rot_matrix.transformVect(offset);
 
view_matrix.buildCameraLookAtMatrixRH(
  offset + target->getPosition(),
  target->getPosition(),
  vector3df(0, 1, 0));
 
proj_matrix.buildProjectionMatrixPerspectiveFovRH(
  node->getFOV(),
  node->getAspectRatio(),
  node->getNearValue(), node->getFarValue());
 
node->setPosition(offset + target->getPosition());
node->setTarget(target->getPosition());
 
I found the setTransform method for the driver, so now I'm setting the matrices in my render system's udpate:

Code: Select all

 
driver->beginScene(true, true, SColor(255, 0, 0, 0));
 
driver->setTransform(
  ETS_VIEW, 
  GameManager::instance().cam->getViewMatrix());
driver->setTransform(
  ETS_PROJECTION, 
  GameManager::instance().cam->getProjectionMatrix());
 
smgr->drawAll();
 
driver->endScene();
 
The camera seems to be almost place correctly, but the movement is still a bit messed up.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Third-Person Camera Using View And Projection Matrices

Post by mongoose7 »

If you're having trouble with the movement, maybe you should be looking at the movement code? (You seem to have removed it from the samples.)
Post Reply