Problems with DirectX Matrices

Started by
1 comment, last by chessgeek 15 years, 4 months ago
I am running an exercise after a one of the tutorials on directxtutorial.com. It's asking me to rotate and translate a triangle while having the camera follow it. I have it set up so that the camera follows the motion of the triangle, however it won't combine the rotation and the translation.

void render_frame(void)
{
	static float index = 0.0f; index += 0.05f;

	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

	d3ddev->BeginScene();

	d3ddev->SetFVF(CUSTOMFVF);

	D3DXMATRIX matTranslateY;
	D3DXMATRIX matRotateY;

	D3DXMatrixTranslation(&matTranslateY, NULL, index, NULL);
	D3DXMatrixRotationY(&matRotateY, index);

	d3ddev->SetTransform(D3DTS_WORLD, &matRotateY);
	d3ddev->SetTransform(D3DTS_WORLD, &matTranslateY);

	D3DXMATRIX matView;

	D3DXMatrixLookAtLH(&matView,
		&D3DXVECTOR3 (0.0f, 0.0f, 10.0f),    // the camera position
		&D3DXVECTOR3 (0.0f, index, 0.0f),   // the look-at position
		&D3DXVECTOR3 (0.0f, 1.0f, 0.0f));    // the up direction

	d3ddev->SetTransform(D3DTS_VIEW, &matView);

	D3DXMATRIX matProjection;

	D3DXMatrixPerspectiveFovLH(&matProjection,
		D3DXToRadian(45),    // the horizontal field of view
		(FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
		1.0f,    // the near view-plane
		100.0f);    // the far view-plane

	d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);

	// select which vertex format we are using
	d3ddev->SetFVF(CUSTOMFVF);

	// select the vertex buffer to display
	d3ddev->SetStreamSource(0, t_buffer, 0, sizeof(CUSTOMVERTEX));

	// copy the vertex buffer to the back buffer
	d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

	d3ddev->EndScene();

	d3ddev->Present(NULL, NULL, NULL, NULL);

	return;
}


Advertisement
You need to multiply matrices in order to concatenate the transformations they represent. With Direct3D the transformations will be applied in the left-to-right order that the matrices are multiplied. So rotation * translation will rotate the triangle first, then translate it (which is probably what you want).
Thanks that worked perfectly.

This topic is closed to new replies.

Advertisement