Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Moving the Camera arund in DX8

This topic is 5986 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 have tried to adopt some code written by Jim Adams concerning the Camera (-> class cCamera (Programming Role Playing Games in DirectX)). Now I encountered a problem. I tried to display a quad using the camera class to zoom in and out and every time the camera enters the positive Region on the Z - Axis, the quad disappears. Here is the code:
//The camera:
//my programme uses this function to position the camera
bool CCamera:oint(float fXEye, float fYEye, float fZEye, float fXAt, float fYAt, float fZAt) {
  float fXRot, fYRot, fXDiff, fYDiff, fZDiff;

  // Calculate angles between points
  fXDiff = fXAt - fXEye;
  fYDiff = fYAt - fYEye;
  fZDiff = fZAt - fZEye;
  fXRot = (float)atan2(-fYDiff, sqrt(fXDiff * fXDiff + fZDiff * fZDiff));
  fYRot = (float)atan2(fXDiff, fZDiff);

  SetPosition(fXEye, fYEye, fZEye);
  SetRotation(fXRot, fYRot, 0.0f);

  return true;

bool CCamera::SetRotation(float fXRot, float fYRot, float fZRot) {
  D3DXMATRIX matXRotation, matYRotation, matZRotation;

  m_fXRot = fXRot;
  m_fYRot = fYRot;
  m_fZRot = fZRot;

  D3DXMatrixRotationX(&matXRotation, -m_fXRot);
  D3DXMatrixRotationY(&matYRotation, -m_fYRot);
  D3DXMatrixRotationZ(&matZRotation, -m_fZRot);

  m_RotationMatrix = matZRotation;
  D3DXMatrixMultiply(&m_RotationMatrix, &m_RotationMatrix, &matYRotation);
  D3DXMatrixMultiply(&m_RotationMatrix, &m_RotationMatrix, &matXRotation);

  return true;

bool CCamera::SetPosition(float fXPos, float fYPos, float fZPos) {
  m_fXPos = fXPos;
  m_fYPos = fYPos;
  m_fZPos = fZPos;

  D3DXMatrixTranslation(&m_TranslationMatrix, -m_fXPos, -m_fYPos, -m_fZPos);

  return true;

//And now the programme:
//The quad
    FLOAT x, y, z;
	DWORD color;
	float tu, tv;

CUSTOMVERTEX g_Vertices[] =
    {-1.0f, -1.0f, 0.0f, D3DCOLOR_RGBA(255, 0, 255, 255), 0.0f, 1.0f},
    {-1.0f, 1.0f, 0.0f, D3DCOLOR_RGBA(255, 0, 255, 255), 0.0f, 0.0f},
    {1.0f, -1.0f, 0.0f, D3DCOLOR_RGBA(255, 0, 255, 255), 1.0f, 1.0f},
	{1.0f, 1.0f, 0.0f, D3DCOLOR_RGBA(255, 0, 255, 255), 1.0f, 0.0f}
//Setting up the positions
//The programme uses the cWorldposition class, too
Graphics->SetPerspective(D3DX_PI / 4.0f, 1.0f, 1000.0f);
	Camera.Point(0.0f, 0.0f, -8.0f, 0.0f, 0.0f, 0.0f);
	Position.SetPosition(0.0f, 0.0f, 10.0f);
	Position.SetRotation(0.0f, 0.0f, 0.0f);

//And finally the SetPerspective method:
bool CGraphics3D::SetPerspective(float fFOV, float fNear, float fFar) {
	//Lokale Variablen
	D3DXMATRIX MatrixProjection;
	D3DVIEWPORT8 Viewport;

	hr = m_lpD3DDevice->GetViewport(&Viewport);
	if(FAILED(hr)) {
		return ReportError(NULL, &hr, "Fehler beim Auslesen des Viewports (CGraphics3D::SetPerspective)");
	float fAspect = ((float)Viewport.Width) / ((float)Viewport.Height);
	D3DXMatrixPerspectiveFovLH(&MatrixProjection, fFOV, fAspect, fNear, fFar);
	hr = m_lpD3DDevice->SetTransform(D3DTS_PROJECTION, &MatrixProjection);
	if(FAILED(hr)) {
		return ReportError(NULL, &hr, "Fehler beim Setzen der Projektions Matrix (CGraphics3D::SetPerspective)");
	return true;
Perhaps someone does spot the error. If I position the Camera on the positive part of the Z axis, the quad disappears. @Jim Adams: If it is kind of illegal to post this code I beg your pardon and would like you to delete this post ;-). Im Anfang war die Tat... Faust

Share this post

Link to post
Share on other sites
This is very simple. What happens is the camera is pointed towards the positive Z-Axis. When you set a positive value the camera goes beyond the quad (most likely) such that the camera is viewing in the direction opposite of the quad. Make sure that the camera is not:

1.) Inside the quad.
2.) Closer than the camera''s near clipping plane.
3.) Pointed away from the quad (most likely this happens).

"Programming is like sex make one mistake, and you have to support it forever."
Homepage: http://students.washington.edu/andrey

Share this post

Link to post
Share on other sites
Well, there is my problem.
The camera is pointing towards the quad and the quad ist located at (0.0f|0.0f|10.0f). If I position the camera at (0.0f|0.0f|1.0f) the quad disappears...

Im Anfang war die Tat...

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!