Calculating movement

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

Recommended Posts

So, i now have a camera that is able to: move forward / back / left / right / turnleft / turnright. My problem is this situation... Say the user turns the camera to the left or right then wants to move forward based on were the camera is pointing. He pushes the key to move forward, but instead of the camera moving forward based were the camera is pointing, it moves forward into the z-axis. This is a obviously bug / flaw / whatever you want to call it and i have some questions on how to fix this. Would the easiest way to do this is to *somehow* calculate were the camera is poiting at, then calculate that into A translation? heres my code on how im moving my camera
void D3D_CUSTOMMATRIX::SetView( void )
{
static float CamX = 0.0f;
static float CamY = 1.0f;
static float CamZ = 30.0f;
static float CamRotX = 0.0f;

if( KEY_DOWN('A') )
{
CamX += 0.5f;
}

if( KEY_DOWN('D') )
{
CamX -= 0.5f;
}

if( KEY_DOWN('W') )
{
CamZ-= 0.5f;
}

if( KEY_DOWN('S') )
{
CamZ += 0.5f;
}

if( KEY_DOWN(VK_LEFT) )
{
CamRotX += 1.0f;
}

if( KEY_DOWN(VK_RIGHT) )
{
CamRotX -= 1.0f;
}

D3DXMatrixLookAtLH( &View,
&D3DXVECTOR3(CamX, CamY, CamZ),
&D3DXVECTOR3(CamX + CamRotX, 0.0f, 0.0f),
&D3DXVECTOR3(0.0f, 1.0f, 0.0f) );

return;
}


Thanks, fitfool

Share on other sites
[Source]//here is a camera class set it up like thisCamera::Camera(){	_cameraType = AIRCRAFT;	_pos   = D3DXVECTOR3(0.0f, 0.0f, 0.0f);	_right = D3DXVECTOR3(1.0f, 0.0f, 0.0f);	_up    = D3DXVECTOR3(0.0f, 1.0f, 0.0f);	_look  = D3DXVECTOR3(0.0f, 0.0f, 1.0f);}Camera::Camera(CameraType cameraType){	_cameraType = cameraType;	_pos   = D3DXVECTOR3(0.0f, 0.0f, 0.0f);	_right = D3DXVECTOR3(1.0f, 0.0f, 0.0f);	_up    = D3DXVECTOR3(0.0f, 1.0f, 0.0f);	_look  = D3DXVECTOR3(0.0f, 0.0f, 1.0f);}Camera::~Camera(){}void Camera::getPosition(D3DXVECTOR3* pos){	*pos = _pos;}void Camera::setPosition(D3DXVECTOR3* pos){	_pos = *pos;}void Camera::getRight(D3DXVECTOR3* right){	*right = _right;}void Camera::getUp(D3DXVECTOR3* up){	*up = _up;}void Camera::getLook(D3DXVECTOR3* look){	*look = _look;}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::pitch(float angle){	D3DXMATRIX T;	D3DXMatrixRotationAxis(&T, &_right,	angle);	// 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 own 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 _up and _right around _look vector		D3DXVec3TransformCoord(&_right,&_right, &T);		D3DXVec3TransformCoord(&_up,&_up, &T);	}}void Camera::getViewMatrix(D3DXMATRIX* V){	// Keep camera's axes orthogonal to eachother	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::setCameraType(CameraType cameraType){	_cameraType = cameraType;}[/Source]

Share on other sites
Simple. Build a rotation matrix for your camera, then multiply the point {0, 0, z, 0} by it, where z is the speed that you want your camera to move forwards. Then add the resulting vector with your camera's position.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• Forum Statistics

• Total Topics
633680
• Total Posts
3013304
×