C Board  

Go Back   C Board > General Programming Boards > Game Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-22-2009, 08:49 PM   #1
Registered User
 
Join Date: Apr 2008
Posts: 58
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?
arcaine01 is offline   Reply With Quote
Old 10-22-2009, 08:55 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 10-22-2009, 08:58 PM   #3
Registered User
 
Join Date: Apr 2008
Posts: 58
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?
arcaine01 is offline   Reply With Quote
Old 10-22-2009, 09:47 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 10-23-2009, 04:01 PM   #5
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,470
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);
}
__________________
If you aim at everything you will hit something but you won't know what it is.
Bubba is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
camera rotation matrix Vick jr Game Programming 5 05-26-2009 08:16 AM
LNK2001 ERROR!!! need help lifeafterdeath C++ Programming 7 05-27-2008 05:05 PM
Problems moving the camera Mavix Game Programming 8 01-30-2008 12:52 PM
RTS camera movement blurrymadness C++ Programming 0 04-22-2007 10:37 PM
Camera rotation/movement in 3D world tegwin Game Programming 11 01-24-2003 01:43 PM


All times are GMT -6. The time now is 11:25 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22