Skip to main content
GameDev.net gamedev.net
🔒 Locked

directx camera help

Started by hpotter936 Dec 26, 2008 at 9:05 PM 2 replies 1k views
Original Post
hpotter936
hpotter936
hey, im trying a 3ps style camera with directx except that all i want it to do at the moment is when i move the mouse from side to side it will rotate around a model iv been serching around the internet for something to help me with this and have become completly confusses i can get the camera to start off in the right positon but once i try and rotate around the model i start getting rotations in weird ways this is the code im using to update the camera

void initCamera(void) {
	D3DXMATRIX tempMat;
	D3DXMATRIX LookAtMatrix;
	D3DXMATRIX ProjectionMatrix;
	D3DXVECTOR3 tempVec;

	D3DXMatrixLookAtLH(&LookAtMatrix,
		//i use modelY - 5 so that camera is positioned behind the model
		D3DXVec3TransformCoord(&tempVec, &D3DXVECTOR3(modelX, modelY - 5, 5.0f), D3DXMatrixRotationZ(&tempMat, xRoll/5000)), 
		&D3DXVECTOR3(modelX, modelY, 0.0f), 
		&D3DXVECTOR3(0.0f, 1.0f, 0.0f));

	d3ddev->SetTransform(D3DTS_VIEW, &LookAtMatrix);

	D3DXMatrixPerspectiveFovLH(&ProjectionMatrix, D3DXToRadian(45), (float)SCREEN_WIDTH/SCREEN_HEIGHT, 1.0f, 200.0f);

	d3ddev->SetTransform(D3DTS_PROJECTION, &ProjectionMatrix);
}
DraganO
DraganO
I think the problem is this line.

Quote:
D3DXVec3TransformCoord(&tempVec, &D3DXVECTOR3(modelX, modelY - 5, 5.0f), D3DXMatrixRotationZ(&tempMat, xRoll/5000)),


You are rotating the vector from (0 | 0 | 0) to (modelX | modelY - 5 | 5.0f) around the z-axis, but what you want is to rotate the vector from (modelX | modelY | 0.0f) to (modelX | modelY - 5 | 5.0f). So you should do something similar to this:

D3DXVECTOR3 tempVec = D3DXVECTOR3(modelX, modelY - 5, 5.0f) - D3DXVECTOR3(modelX, modelY, 0.0f);D3DXVec3TransformCoord(&tempVec, &tempVec, D3DXMatrixRotationZ(&tempMat, xRoll/5000));D3DXVECTOR3 CamPos = tempVec + D3DXVECTOR3(modelX, modelY, 0.0f);


Then pass CamPos to D3DXMatrixLookAtLH.

Also I guess you want to rotate around the y-axis. So try using D3DXMatrixRotationY instead of D3DXMatrixRotationZ.
hpotter936
hpotter936
hey thanks for your reply i tried out your code and it helped a lot its almost working the only problem now is that when i rotate the camera around to the front it doesn't stay parallel to the ground if you know what i mean so that when i get to the front of the model it is like the camera has been turned upside down. Any idea what might be going on here?
Thanks for the help
hpotter936
hpotter936
ok so iv tried a few other thing, adding in another rotation matrix and even as a last resort i tried rotating the lookat matrix at the same amount as the position matrix of the camera none of these seem to fix the problem im having here anyone got any suggestions for me thanks

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.