• Advertisement
Sign in to follow this  

Why does my flexible camera not move?

This topic is 3877 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to create a flexible camera--when I press down the right mouse button and move the mouse,then the camera can also move in response.In fact after I run my code,a game window appears,and I press the button and drag the mouse,but the camera doesn't act even a little.There are no errors in the output window.Let me post my camera class here:
#include <d3dx9.h>
class Camera
{
public:
	enum CameraType{LANDOBJECT,AIRCRAFT};
	Camera(D3DXVECTOR3,D3DXVECTOR3,D3DXVECTOR3);
	Camera(CameraType cameraType);
	~Camera();

	void strafe(float units);//left/right
	void fly(float units);//up/down
	void walk(float units);//forward/backward
	void pitch(float angle);//rotate on right vector
	void yaw(float angle);//rotate on up vector
	void roll(float angle);//rotate on look vector

	void getViewMatrix(D3DXMATRIX* v);//update the view matrix
	void setCameraType(CameraType cameraType);
	//void getPosition(D3DXVECTOR3* pos);
        //void setPosition(D3DXVECTOR3* pos);
	//void getRight(D3DXVECTOR3* right);
        //void getUp(D3DXVECTOR3* up);
	//void getLook(D3DXVECTOR3* look);

private:
    CameraType _cameraType;
    D3DXVECTOR3 _right;
    D3DXVECTOR3 _up;
    D3DXVECTOR3 _look;
    D3DXVECTOR3 _pos;
};
#include "Camera.h"
Camera::Camera(D3DXVECTOR3 pos,D3DXVECTOR3 look,D3DXVECTOR3 up)
{
_pos = pos;
_look = look;
_up = up;
_right = D3DXVECTOR3(0.0,1.0,0.0);
_cameraType = LANDOBJECT;
};
Camera::~Camera()
{};
void Camera::getViewMatrix(D3DXMATRIX* v)
{//keep camera's axes orthogonal to each orther
	D3DXVec3Normalize(&_look,&_look);
	D3DXVec3Cross(&_up,&_look,&_right);
	D3DXVec3Normalize(&_up,&_up);
	D3DXVec3Cross(&_right,&_up,&_look);
         D3DXVec3Normalize(&_right,&_right);
//build the view matrix
	float x = -D3DXVec3Dot(&_right,&_pos);
	float y = -D3DXVec3Dot(&_up,&_pos);
	float z = -D3DXVec3Dot(&_look,&_pos);

	(*v)(0,0)=_right.x;(*v)(0,1)=_up.x;(*v)(0,2)=_look.x;(*v)(0,3)=0.0f;
        (*v)(1,0)=_right.y;(*v)(1,1)=_up.y;(*v)(1,2)=_look.y;(*v)(1,3)=0.0f;
        (*v)(2,0)=_right.z;(*v)(2,1)=_up.z;(*v)(2,2)=_look.z;(*v)(2,3)=0.0f;
	(*v)(3,0)=x;       (*v)(3,1)=y;    (*v)(3,2)=z;      (*v)(3,3)=1.0f;

}

void Camera::pitch(float angle)//angle,in radians,to rotate
{
D3DXMATRIX T;
D3DXMatrixRotationAxis(&T,&_right,angle);//rotate around axis
//rotate _up and _look around _right vector
D3DXVec3TransformCoord(&_up,&_up,&T);
D3DXVec3TransformCoord(&_look,&_look,&T);

}

void Camera::yaw(float angle)
{
D3DXMATRIX T;
//rotate around world y(0,1,0) always for land object
if(_cameraType==LANDOBJECT)
D3DXMatrixRotationY(&T,angle);
//rotate around up vector for aircraft
if(_cameraType==AIRCRAFT)
D3DXMatrixRotationAxis(&T,&_up,angle);
//rotate _right and _look around _up or y axis
D3DXVec3TransformCoord(&_right,&_right,&T);
D3DXVec3TransformCoord(&_look,&_look,&T);

}

void Camera::roll(float angle)
{//only roll for aircraft type
if(_cameraType==AIRCRAFT)
{
D3DXMATRIX T;
D3DXMatrixRotationAxis(&T,&_look,angle);
//rotate _right and _up around look vector
D3DXVec3TransformCoord(&_right,&_right,&T);
D3DXVec3TransformCoord(&_up,&_up,&T);

}
}

void Camera::walk(float units)
{//move only on xz plane for land object
if(_cameraType==LANDOBJECT)
_pos+=D3DXVECTOR3(_look.x,0.0f,_look.z)*units;
if(_cameraType==AIRCRAFT)
_pos+=_look*units;
}

void Camera::strafe(float units)
{//move only on xz plane for land object
if(_cameraType==LANDOBJECT)
_pos+=D3DXVECTOR3(_right.x,0.0f,_right.z)*units;
if(_cameraType==AIRCRAFT)
_pos+=_right*units;
}

void Camera::fly(float units)
{//move only on y-axis for land object
if(_cameraType==LANDOBJECT)
_pos.y+=units;
if(_cameraType==AIRCRAFT)
_pos+=_up*units;
}

void Camera::setCameraType(CameraType cameraType)
{
_cameraType = cameraType;
}


the way I get mouse position coordinate :
POINT    m_ptLastMouse; //globle variable, position of last mouse point
//The following function says the current mouse position will become the last when press the right button:
void OnBegin( int nX, int nY)
    {
    m_ptLastMouse.x =nX;
    m_ptLastMouse.y =nY;
    }
//in the window's message handler:
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
// Current mouse position
    int iMouseX = (short)LOWORD(lParam);
    int iMouseY = (short)HIWORD(lParam);
    switch( msg )
    {
        case WM_DESTROY:
            Cleanup();
            PostQuitMessage( 0 );
            return 0;
        
		case WM_RBUTTONDOWN:
		case WM_LBUTTONDOWN:
                SetCapture( hWnd );
                OnBegin( iMouseX, iMouseY);// 
                return TRUE;
...




the way I set the view matrix:
//globle variables
float Z=0.0f;
D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f);
float X=0.0f;
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
...
D3DXMatrixLookAtLH( &matView,&vEyePt, &vLookatPt , &vUpVec );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );




the way I update the view matrix:
//in the window's message handler:
 case WM_MOUSEMOVE:
                if( MK_RBUTTON&wParam )
                {
                   SetCapture(hWnd);
                //measure the change between the last and current position of the mouse and scale it down 
                   Z = (iMouseY-m_ptLastMouse.x)/10.0f;
                   X = (iMouseX-m_ptLastMouse.x)/10.0f;
                   g_pCamera->walk(Z); 
                   g_pCamera->yaw(X); 
                   //update the view matrix representing the camera's new position/orientation
                   g_pCamera->getViewMatrix(&matView);             
                   g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );

		         }
               return TRUE;




If there was anything you didn't understand,please tell me. You are welcome. Thank you advanced. [Edited by - showin on June 11, 2007 11:59:16 PM]

Share this post


Link to post
Share on other sites
Advertisement
Not sure where the problem is but you have at least one error - in MsgProc(), you set iMouseX and iMouseY to wparam and lparam EVERY time the MsgProc() is called, but these paremeters represent mouse coordinates only when a mouse message is handled.

One other thing I would try is to step through the program with the debugger and see what values get passed to walk() and yaw(). Also maybe try taking these functions out from the MsgProc() and into your render function and pass them some constant number to see what that gives you.

[Edited by - Gage64 on June 13, 2007 1:27:19 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement