I have a problem rotating an objects vertices with a matrix.
At the first few frames it looks okay, but after that it jumps around.
The final rotation is corect, but position is wrong.
D3DXVECTOR3& Transform(D3DXVECTOR3& V, D3DXMATRIX& M)
{
D3DXVECTOR3 Out;
float x = V.x * M._11 + V.y * M._21 + V.z * M._31 + M._41;
float y = V.x * M._12 + V.y * M._22 + V.z * M._32 + M._42;
float z = V.x * M._13 + V.y * M._23 + V.z * M._33 + M._43;
//float w = V.x * M._14 + V.y * M._24 + V.z * M._34 + M._44; <- don't need scaling for now
Out.x = x;
Out.y = y;
Out.z = z;
return Out;
}
//this is called each frame
void Update()
{
//same matrix function I use in Level Editor
D3DXMatrixRotationYawPitchRoll(&nodeRotation, -angle.y, angle.x, angle.z);
//make a new bbox in a temp location so we can keep the actual bbox
bboxMax = D3DXVECTOR3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
bboxMin = D3DXVECTOR3(FLT_MAX, FLT_MAX, FLT_MAX);
//temporarly make the actual bbox very big
sector->bboxMax = D3DXVECTOR3(FLT_MAX, FLT_MAX, FLT_MAX);
sector->bboxMin = D3DXVECTOR3(-FLT_MAX, -FLT_MAX, -FLT_MAX);
for (DWORD i = 0; i < sector->numVertices; i++)
{
//Move the object to origin and then transform it by the matrix and move it back to the position
sector->vertices[i] -= pos;
sector->vertices[i] = Transform(sector->vertices[i], nodeRotation);
sector->vertices[i] += pos;
//calculate the new bbox into the temp location
if (bboxMax.x < sector->vertices[i].x)
bboxMax.x = sector->vertices[i].x;
if (bboxMin.x > sector->vertices[i].x)
bboxMin.x = sector->vertices[i].x;
if (bboxMax.y < sector->vertices[i].y)
bboxMax.y = sector->vertices[i].y;
if (bboxMin.y > sector->vertices[i].y)
bboxMin.y = sector->vertices[i].y;
if (bboxMax.z < sector->vertices[i].z)
bboxMax.z = sector->vertices[i].z;
if (bboxMin.z > sector->vertices[i].z)
bboxMin.z = sector->vertices[i].z;
}
//set the actual bbox to be same as temp bbox
sector->bboxMax = bboxMax;
sector->bboxMin = bboxMin;
pos = (sector->bboxMax + sector->bboxMin) / 2.0f;
//Send state to update vertexbuffer
sector->mesh->Update();
}