# world matrix problem

## Recommended Posts

Hello guys, im getting a pretty wierd problem.
When I try to move my model by changing its worldmatrix, it gets draw some frames at the 0 0 0 position, and some other frams as totally messed up something. Same for rotation, but scaling works fine.

[source lang="cpp"]
D3DXMatrixIdentity(&World);
D3DXVECTOR3 rotaxisX = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
D3DXVECTOR3 rotaxisY = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
D3DXVECTOR3 rotaxisZ = D3DXVECTOR3(0.0f, 0.0f, 1.0f);

D3DXMATRIX temprot1, temprot2, temprot3;
D3DXMatrixRotationAxis(&temprot1, &rotaxisX, 0);
D3DXMatrixRotationAxis(&temprot2, &rotaxisY, 0);
D3DXMatrixRotationAxis(&temprot3, &rotaxisZ, 0);

Rotation = temprot1 *temprot2 * temprot3;

D3DXMatrixTranslation(&Translation, 0.0f, 10.0f, 0.0f);
D3DXMatrixScaling(&Scale, 0.02f, 0.02f, 0.02f);
//Set objs world space using the transformations
World = Translation * Rotation * Scale;
[/source]

[source lang="cpp"]cbuffer cbPerObject
{
matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;
};

// Change the position vector to be 4 units for proper matrix calculations.
input.position.w = 1.0f;

// Calculate the position of the vertex against the world, view, and projection matrices.
output.position = mul(input.position, worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);
[/source]

I really cant see what is going wrong here. If all matrixchanges are 0, then its drawn fine.
Has it something to do with the math? The constantbuffer is set fine thought, light is set the same way and it works fine.

It looks like this (switching between these two frames randomly, ignore the terrain):
[url="http://imageshack.us/photo/my-images/3/32408839.png/"]http://imageshack.us...3/32408839.png/[/url]
[url="http://imageshack.us/photo/my-images/42/failjf.png/"]http://imageshack.us.../42/failjf.png/[/url] Edited by gnomgrol

##### Share on other sites
clb    2147
one thing I notice is that you do "World = Translate * Rotate * Scale", and in the shader "output.position = input.position * World". As a result, you first translate the point, then rotate it about the origin, and then scale it w.r.t the origin. It's more conventional to first scale, then rotate and finally translate, so try changing the code to "World = Scale * Rotate * Translate".

##### Share on other sites
Thank you, but that didnt do any good =(
The object now gets some times drawn in the right position, but the wierd flickering didnt change. Edited by gnomgrol

##### Share on other sites
clb    2147
When I encounter issues like this, my first go-to is PIX (and more recently, nVidia Paraller nSight). It's very helpful in disassembling the device state for evaluating that everything is as you'd expect.

##### Share on other sites
Well, I AM currently trying to figure out the problem with PIX myself, but I really cant see where the problem lies ..
I added two pictures to my first post.

Edit: Im through with analysing the buffers in PIX. I still cant see what the problem is, everything seems to be just fine.... Edited by gnomgrol

##### Share on other sites
clb    2147
Perhaps you're accidentally transposing some of the matrices, and you end up processing the translation elements of the matrix to affect the .w component of the output. This bug causes results that look a bit like the second image - vertices flying towards "infinity" in one direction.

##### Share on other sites
You are right. When I delete the line that transposes the worldmatrix, it keeps looking like pic2. Now I need to figure why the matrix only gets transposed in some frames, while in others not. Here is the drawingcode:

[CODE]
D3DXMatrixTranspose(&World, &World);
D3DXMatrixTranspose(&camView, &camView);
D3DXMatrixTranspose(&camProjection, &camProjection);
// Lock the constant buffer so it can be written to.
D3D11_MAPPED_SUBRESOURCE mappedResource;
cbPerObject* dataPtr;
dataPtr = (cbPerObject*)mappedResource.pData;
// Copy the matrices into the constant buffer.
dataPtr->worldMatrix = World;
dataPtr->viewMatrix = camView;
dataPtr->projectionMatrix = camProjection;
// Unlock the constant buffer.
d3d11DevCon->Unmap(matrixBuffer, 0);
d3d11DevCon->VSSetConstantBuffers(0, 1, &matrixBuffer);
[/CODE]

##### Share on other sites
I fixed it, but I dont know what caused it exactly. I have to call this code EVERY frame, not only once (I have no idea why)
[source lang="cpp"]D3DXMatrixIdentity(&World);
D3DXVECTOR3 rotaxisX = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
D3DXVECTOR3 rotaxisY = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
D3DXVECTOR3 rotaxisZ = D3DXVECTOR3(0.0f, 0.0f, 1.0f);

D3DXMATRIX temprot1, temprot2, temprot3;
D3DXMatrixRotationAxis(&temprot1, &rotaxisX, 0);
D3DXMatrixRotationAxis(&temprot2, &rotaxisY, 0);
D3DXMatrixRotationAxis(&temprot3, &rotaxisZ, 0);

Rotation = temprot1 *temprot2 * temprot3;

D3DXMatrixTranslation(&Translation, 0.0f, 10.0f, 0.0f);
D3DXMatrixScaling(&Scale, 0.02f, 0.02f, 0.02f);
//Set objs world space using the transformations
World = Scale * Rotation * Translation;[/source]

Now it works, but can you tell me why? Edited by gnomgrol