# Thread: Confusion on Camera controls

1. ## Confusion on Camera controls

So, I was working with 3D cameras and worlds, and I realized...I don't really understand what the matrices represent.

There is a ViewMatrix, a perspective matrix, an upmatrix, and a world matrix.

I thought that the ViewMatrix created the clipped world matrix, the perspective matrix scewed and transformed the world matrix to create an illusion of depth and perspective, and then a world matrix that contained the coordinates for everything...but that still leaves the up matrix, and I have NO idea what that would be.

I understand the math for creating perspective from matrices, but I don't know what the up matrix would be the equivalent to. What is this? How can I use this properly?

So far, I have created a camera like so...:

Camera.h:
Code:
```#include "camera.h"
static float aspect = 800.0f / 600.0f;
Camera::Camera(CameraType type)
{
_Type = type;
_Position = _Rotation = PointF(0.0f, 0.0f, 0.0f);

//D3DXVECTOR3 cameraPosition( 0.0f, 10.0f, 0.0f );
//D3DXVECTOR3 cameraTarget( 0.0f, 1.0f, 10.0f );
//D3DXVECTOR3 cameraUp( 0.0f, 1.0f, 0.0f );
//D3DXMatrixLookAtLH( &ViewMatrix, &cameraPosition, &cameraTarget, &cameraUp );
//GraphicDevice::GetService()->Device()->SetTransform( D3DTS_VIEW, &ViewMatrix );

////D3DXMatrixPerspectiveFovLH(&ProjectionMatrix, PI/4, 1.0f, 1.0f, 5000.0f);
////GraphicDevice::GetService()->Device()->SetTransform(D3DTS_PROJECTION, &ProjectionMatrix);

//   // Set the projection matrix
//D3DXMatrixPerspectiveFovLH( &ProjectionMatrix, D3DX_PI / 2, aspect, 1.0f, 1000.0f );
//GraphicDevice::GetService()->Device()->SetTransform( D3DTS_PROJECTION, &ProjectionMatrix );

}
Camera::~Camera(void)
{
}
void Camera::Update(void)
{
//D3DXVECTOR3 cameraPosition(_Position.X(), _Position.Y(), _Position.Z());
//D3DXVECTOR3 cameraTarget(_Position.X()+cos(_Rotation.Z()), _Position.Y(), _Position.Z()+sin(_Rotation.X()));
//D3DXVECTOR3 cameraUp(0.0f, 1.0f, 0.0f);

//   D3DXMatrixLookAtLH( &ViewMatrix, &cameraPosition, &cameraTarget, &cameraUp );
//
//   D3DXMatrixPerspectiveFovLH( &ProjectionMatrix, D3DX_PI / 2, aspect, 1.0f, 5000.0f );
//   HRESULT a = GraphicDevice::GetService()->Device()->SetTransform( D3DTS_PROJECTION, &ProjectionMatrix );
//int i = 0;
D3DXVECTOR3 Position(_Position.X(), _Position.Y(), _Position.Z()),
Target(_Position.X(), _Position.Y(), _Position.Z()),
UpVector(0.0f, 1.0f, 0.0f);
D3DXMatrixLookAtLH(&ViewMatrix, &Position, &Target, &UpVector);
D3DXMatrixPerspectiveFovLH(&ProjectionMatrix, D3DX_PI / 2, aspect, 1.0f, 5000.0f);
GraphicDevice::GetService()->Device()->SetTransform(D3DTS_PROJECTION, &ProjectionMatrix);
}
void Camera::Rotate(TransformType type, PointF rotation)
{
switch(type)
{
case TransformTypes::Absolute:
_Rotation = rotation;
break;
case TransformTypes::Relative:
_Rotation += rotation;
break;
}
}
void Camera::Move(TransformType type, PointF A)
{
switch(type)
{
case TransformTypes::Absolute:
_Position = A;
break;
case TransformTypes::Relative:
_Position.Z(_Position.Z() + cos(A.Z()) + sin(A.Z()));
_Position.X(_Position.Z() + cos(A.Z()) - sin(A.Z()));
break;
}
}```
Camera.cpp
Code:
```#include "camera.h"
static float aspect = 800.0f / 600.0f;
Camera::Camera(CameraType type)
{
_Type = type;
_Position = _Rotation = PointF(0.0f, 0.0f, 0.0f);

//D3DXVECTOR3 cameraPosition( 0.0f, 10.0f, 0.0f );
//D3DXVECTOR3 cameraTarget( 0.0f, 1.0f, 10.0f );
//D3DXVECTOR3 cameraUp( 0.0f, 1.0f, 0.0f );
//D3DXMatrixLookAtLH( &ViewMatrix, &cameraPosition, &cameraTarget, &cameraUp );
//GraphicDevice::GetService()->Device()->SetTransform( D3DTS_VIEW, &ViewMatrix );

////D3DXMatrixPerspectiveFovLH(&ProjectionMatrix, PI/4, 1.0f, 1.0f, 5000.0f);
////GraphicDevice::GetService()->Device()->SetTransform(D3DTS_PROJECTION, &ProjectionMatrix);

//   // Set the projection matrix
//D3DXMatrixPerspectiveFovLH( &ProjectionMatrix, D3DX_PI / 2, aspect, 1.0f, 1000.0f );
//GraphicDevice::GetService()->Device()->SetTransform( D3DTS_PROJECTION, &ProjectionMatrix );

}
Camera::~Camera(void)
{
}
void Camera::Update(void)
{
D3DXVECTOR3 cameraPosition(_Position.X(), _Position.Y(), _Position.Z());
D3DXVECTOR3 cameraTarget(_Position.X()+cos(_Rotation.Z()), _Position.Y(), _Position.Z()+sin(_Rotation.X()));
D3DXVECTOR3 cameraUp(0.0f, 1.0f, 0.0f);

D3DXMatrixLookAtLH( &ViewMatrix, &cameraPosition, &cameraTarget, &cameraUp );

D3DXMatrixPerspectiveFovLH( &ProjectionMatrix, D3DX_PI / 2, aspect, 1.0f, 5000.0f );
HRESULT a = GraphicDevice::GetService()->Device()->SetTransform( D3DTS_PROJECTION, &ProjectionMatrix );
//int i = 0;
//D3DXVECTOR3 Position(_Position.X(), _Position.Y(), _Position.Z()),
//	Target(_Position.X(), _Position.Y(), _Position.Z()),
//	UpVector(0.0f, 1.0f, 0.0f);
//D3DXMatrixLookAtLH(&ViewMatrix, &Position, &Target, &UpVector);
//D3DXMatrixPerspectiveFovLH(&ProjectionMatrix, D3DX_PI / 2, aspect, 1.0f, 5000.0f);
//GraphicDevice::GetService()->Device()->SetTransform(D3DTS_PROJECTION, &ProjectionMatrix);
}
void Camera::Rotate(TransformType type, PointF rotation)
{
switch(type)
{
case TransformTypes::Absolute:
_Rotation = rotation;
break;
case TransformTypes::Relative:
_Rotation += rotation;
break;
}
}
void Camera::Move(TransformType type, PointF A)
{
switch(type)
{
case TransformTypes::Absolute:
_Position = A;
break;
case TransformTypes::Relative:
_Position.Z(_Position.Z() + cos(A.Z()) + sin(A.Z()));
_Position.X(_Position.Z() + cos(A.Z()) - sin(A.Z()));
break;
}
}```

Then I try to move it like so:

Code:
```		if(KeyboardDevice::GetService()->Pressed(Keys::ArrowUp))
{
camera.Move(TransformTypes::Relative, PointF(0.0f, 0.0f, 0.1f));
}
else if(KeyboardDevice::GetService()->Pressed(Keys::ArrowDown))
{
camera.Move(TransformTypes::Relative, PointF(0.0f, 0.0f, -0.1f));
}```

So....Why doesn't this alter the perspective matrix, so that it appears that I am moving?
Does anyone know?

2. Up matrix? You mean up vector. That just says, well, which way is up. You've got a position for the camera, and the point that it's looking at (which sets most of your viewing things). But imagine holding a real camera, and turning it upside down. It's still in the same place, focused at the same point, but the image is upside down.

3. Oh lol...vector....makes much more sense haha

But...even so, Do you have any clue why my current code would not even adjust the viewmatrix when I call move?

4. I'm not entirely certain why you have two of the same function. Nor am I certain why you have sines and cosines in your relative move. Unless LookAt looks rather differently in DirectX than I'm expecting (very possible), your focus point moves with you. Maybe that's what you want, but depending on how you're drawing you very well may be having things follow you.

5. You do not need the sin() and cos(). And you are not calculating a correct view matrix via that code.

Code:
```void Camera::GetViewMatrix(D3DXMATRIXA16 *outMatrix)
{
if (m_viewChanged)
{
MakeOrtho();

float x = -D3DXVec3Dot(&m_Pos, &m_Right);
float y = -D3DXVec3Dot(&m_Pos, &m_Up);
float z = -D3DXVec3Dot(&m_Pos, &m_Look);

(*outMatrix)(0,0) = m_Right.x;
(*outMatrix)(1,0) = m_Right.y;
(*outMatrix)(2,0) = m_Right.z;
(*outMatrix)(3,0) = x;

(*outMatrix)(0,1) = m_Up.x;
(*outMatrix)(1,1) = m_Up.y;
(*outMatrix)(2,1) = m_Up.z;
(*outMatrix)(3,1) = y;

(*outMatrix)(0,2) = m_Look.x;
(*outMatrix)(1,2) = m_Look.y;
(*outMatrix)(2,2) = m_Look.z;
(*outMatrix)(3,2) = z;

(*outMatrix)(0,3) = 0.0f;
(*outMatrix)(1,3) = 0.0f;
(*outMatrix)(2,3) = 0.0f;
(*outMatrix)(3,3) = 1.0f;

m_matView = *outMatrix;
}
}

void Camera::MakeOrtho()
{
D3DXVec3Normalize(&m_Look,&m_Look);

D3DXVec3Cross(&m_Up,&m_Look,&m_Right);
D3DXVec3Normalize(&m_Up,&m_Up);

D3DXVec3Cross(&m_Right,&m_Up,&m_Look);
D3DXVec3Normalize(&m_Right,&m_Right);
}```