Rotation Matrix/World Matrix problems

26 replies to this topic

#1Dom_152

Member

• Members
• 67 posts

Posted 07 March 2007 - 05:43 PM

Hey all. I'm trying to get my world matrix for my engine working. I need to rotate the vertices on each axis then translate them. But when I try nothing is displayed. I think I must be creating a rotation matrix wrong. I thought I can create a rotation axis for X, Y and Z then multiply them together to make one rotation matrix. Is this correct or is there another way of doing this?

EDIT:

I have something that works as in it rotates:

rotMatrix.RotateX(m_rotation.GetX());
worldMatrix = rotMatrix;

rotMatrix.RotateY(m_rotation.GetY());
worldMatrix = worldMatrix.Multiply(rotMatrix);

rotMatrix.RotateZ(m_rotation.GetZ());
//Set the world matrix to that of our mesh
worldMatrix = worldMatrix.Multiply(rotMatrix);

worldMatrix = worldMatrix.Multiply(transMatrix);


But I think my projection or view matrices are screwed up because look at my "Cube":

#2Kenneth Gorking

Senior Member

• Members
• 939 posts

Posted 07 March 2007 - 06:57 PM

This looks more like a case of bad indices/triangles. Matrices won't affect single vertices, unless explicitly told too.
"Stupid bug! You go squish now!!" - Homer Simpson

#3Dom_152

Member

• Members
• 67 posts

Posted 07 March 2007 - 07:35 PM

I would've thought that the projection could screw up how they are shown on the screen seeing as it is "projecting" form World to Screen?

#4dave_

Senior Member

• Members
• 584 posts

Posted 07 March 2007 - 09:35 PM

Yes, but you'd more likely see something stretched/oriented wrong/scaled/translated, if it was a matrix problem, than broken up (vertex problem)

#5Rubicon

Member

• Members
• 90 posts

Posted 07 March 2007 - 10:21 PM

I'd back up the idea of screwed data.

Your cube is in the centre of the screen and I presume the rest of it looks like it's spinning around ? If so, it's your vertex data for sure.

Sidenote: I'd recommend picking up a matrix and vector class and using that instead of rolling your own. Getting all the overloads sorted out (if you use C++ or other oop) so you end up with Vertex.Pos*=Matrix for applying a matrix to a vertex for example makes you code seriously readable and is less bug-prone.

We actually wrote our own, but with hindsight I wish we'd started from someone elses!
Regards, Paul Johnson
www.rubicondev.com
My Free 3D Tower Defence Game

#6Dom_152

Member

• Members
• 67 posts

Posted 08 March 2007 - 07:41 AM

Thing is I copied the vertex/index data for this cube from the Irrlicht engine and I know that works. When you move the camera or rotate it that triangle thats sticking out in the pick seems to stay there and just gets elongated, stretched.

#7Rubicon

Member

• Members
• 90 posts

Posted 08 March 2007 - 09:21 AM

Which is a common example of projecting to infinity due to NAN errors in the vertex data. Might not be it, but you need to make sure as this *sounds* like a classic case
Regards, Paul Johnson
www.rubicondev.com
My Free 3D Tower Defence Game

#8Dom_152

Member

• Members
• 67 posts

Posted 08 March 2007 - 03:40 PM

I thought I might as well post my vertex data. I don't know if theres anything wrong here. The variable colour is 0xFFFFFFFF:


//Vertex data

vertices[0].X = 0.0f; vertices[0].Y = 0.0f; vertices[0].Z = 0.0f; vertices[0].NX = -1.0f; vertices[0].NY = -1.0f; vertices[0].NZ = -1.0f; vertices[0].diffuse = colour;

vertices[1].X = 1.0f; vertices[1].Y = 0.0f; vertices[1].Z = 0.0f; vertices[1].NX = 1.0f; vertices[1].NY = 1.0f; vertices[1].NZ = 1.0f; vertices[1].diffuse = colour;

vertices[2].X = 1.0f; vertices[2].Y = 1.0f; vertices[2].Z = 0.0f; vertices[2].NX = 1.0f; vertices[2].NY = 1.0f; vertices[2].NZ = -1.0f; vertices[2].diffuse = colour;

vertices[3].X = 0.0f; vertices[3].Y = 1.0f; vertices[3].Z = 0.0f; vertices[3].NX = -1.0f; vertices[3].NY = 1.0f; vertices[3].NZ = -1.0f; vertices[3].diffuse = colour;

vertices[4].X = 1.0f; vertices[4].Y = 0.0f; vertices[4].Z = 1.0f; vertices[4].NX = 1.0f; vertices[4].NY = -1.0f; vertices[4].NZ = 1.0f; vertices[4].diffuse = colour;

vertices[5].X = 1.0f; vertices[5].Y = 1.0f; vertices[5].Z = 1.0f; vertices[5].NX = 1.0f; vertices[5].NY = 1.0f; vertices[5].NZ = 1.0f; vertices[5].diffuse = colour;

vertices[6].X = 0.0f; vertices[6].Y = 1.0f; vertices[6].Z = 1.0f; vertices[6].NX = -1.0f; vertices[6].NY = 1.0f; vertices[6].NZ = 1.0f; vertices[6].diffuse = colour;

vertices[7].X = 0.0f; vertices[7].Y = 0.0f; vertices[7].Z = 1.0f; vertices[7].NX = -1.0f; vertices[7].NY = -1.0f; vertices[7].NZ = 1.0f; vertices[7].diffuse = colour;

vertices[8].X = 0.0f; vertices[8].Y = 1.0f; vertices[8].Z = 1.0f; vertices[8].NX = -1.0f; vertices[8].NY = 1.0f; vertices[8].NZ = 1.0f; vertices[8].diffuse = colour;

vertices[9].X = 0.0f; vertices[9].Y = 1.0f; vertices[9].Z = 0.0f; vertices[9].NX = -1.0f; vertices[9].NY = 1.0f; vertices[9].NZ = -1.0f; vertices[9].diffuse = colour;

vertices[10].X = 1.0f; vertices[10].Y = 0.0f; vertices[10].Z = 1.0f; vertices[10].NX = 1.0f; vertices[10].NY = -1.0f; vertices[10].NZ = 1.0f; vertices[10].diffuse = colour;

vertices[11].X = 1.0f; vertices[11].Y = 0.0f; vertices[11].Z = 0.0f; vertices[11].NX = 1.0f; vertices[11].NY = -1.0f; vertices[11].NZ = -1.0f; vertices[11].diffuse = colour;



#9Kenneth Gorking

Senior Member

• Members
• 939 posts

Posted 08 March 2007 - 05:58 PM

It's more likely your indices. The missing 'triangle' in your cube is because of bad ordering of your vertices, meaning that two triangles which should lie next to each other overlap instead.
"Stupid bug! You go squish now!!" - Homer Simpson

#10Dom_152

Member

• Members
• 67 posts

Posted 09 March 2007 - 08:33 PM

Index data:

//Indices
indices[0] = 0; indices[3] = 0; indices[6] = 1; indices[9] = 1; indices[12] = 4; indices[15] = 4; indices[18] = 7; indices[21] = 7; indices[24] = 9; indices[27] = 9;
indices[1] = 2; indices[4] = 3; indices[7] = 5; indices[10] = 2; indices[13] = 6; indices[16] = 5; indices[19] = 3; indices[22] = 6; indices[25] = 5; indices[28] = 8;
indices[2] = 1; indices[5] = 2; indices[8] = 4; indices[11] = 5; indices[14] = 7; indices[17] = 6; indices[20] = 0; indices[23] = 3; indices[26] = 2; indices[29] = 5;

indices[30] = 0; indices[33] = 0;
indices[31] = 11; indices[34] = 10;
indices[32] = 10; indices[35] = 7;


#11Kenneth Gorking

Senior Member

• Members
• 939 posts

Posted 10 March 2007 - 03:15 PM

It sure is. Now you need to re-arrange them so they are correct :)

Too make it easier on yourself, try and split up the cube to its individual faces, and fix one at a time.
"Stupid bug! You go squish now!!" - Homer Simpson

#12Dom_152

Member

• Members
• 67 posts

Posted 11 March 2007 - 10:10 AM

OK I tried that. I made a cube in a 3D Modeller, exported it and took the vertex and index data straight from that. But it still doesn't look right. Now it looks like a flattened L shape. :S

//Indices
indices[0] = 0; indices[3] = 3; indices[6] = 4; indices[9] = 7; indices[12] = 0; indices[15] = 5; indices[18] = 1; indices[21] = 7; indices[24] = 3; indices[27] = 6;
indices[1] = 2; indices[4] = 1; indices[7] = 5; indices[10] = 6; indices[13] = 1; indices[16] = 4; indices[19] = 3; indices[22] = 5; indices[25] = 2; indices[28] = 7;
indices[2] = 3; indices[5] = 0; indices[8] = 7; indices[11] = 4; indices[14] = 5; indices[17] = 0; indices[20] = 7; indices[23] = 1; indices[26] = 6; indices[29] = 3;

indices[30] = 2; indices[33] = 4;
indices[31] = 0; indices[34] = 6;
indices[32] = 4; indices[35] = 2;


//Vertex data
vertices[0].X = -0.5f; vertices[0].Y = 0.0f; vertices[0].Z = -0.5f; vertices[0].NX = 0.0f; vertices[0].NY = 1.0f; vertices[0].NZ = 0.0f; vertices[0].diffuse = colour;
vertices[1].X = 0.5f; vertices[1].Y = 0.0f; vertices[1].Z = -0.5f; vertices[1].NX = 0.0f; vertices[1].NY = -1.0f; vertices[1].NZ = -0.0f; vertices[1].diffuse = colour;
vertices[2].X = -0.5f; vertices[2].Y = 0.0f; vertices[2].Z = 0.5f; vertices[2].NX = 0.0f; vertices[2].NY = 0.0f; vertices[2].NZ = -1.0f; vertices[2].diffuse = colour;
vertices[3].X = 0.5f; vertices[3].Y = 0.0f; vertices[3].Z = 0.5f; vertices[3].NX = -0.0f; vertices[3].NY = 0.0f; vertices[3].NZ = 0.0f; vertices[3].diffuse = colour;
vertices[4].X = -0.5f; vertices[4].Y = -1.0f; vertices[4].Z = -0.5f; vertices[4].NX = 0.0f; vertices[4].NY = 0.0f; vertices[4].NZ = 1.0f; vertices[4].diffuse = colour;
vertices[5].X = 0.5f; vertices[5].Y = -1.0f; vertices[5].Z = -0.5f; vertices[5].NX = 1.0f; vertices[5].NY = 0.0f; vertices[5].NZ = 0.0f; vertices[5].diffuse = colour;
vertices[6].X = -0.5f; vertices[6].Y = -1.0f; vertices[6].Z = 0.5f; vertices[6].NX = 0.0f; vertices[6].NY = -0.0f; vertices[6].NZ = 1.0f; vertices[6].diffuse = colour;
vertices[7].X = 0.50f; vertices[7].Y = -1.0f; vertices[7].Z = 0.5f; vertices[7].NX = -1.0f; vertices[7].NY = 0.0f; vertices[7].NZ = 1.0f; vertices[7].diffuse = colour;


I'll post a screenshot as soon as I work out why the triangles flicker if I turn off lighting.

#13Dom_152

Member

• Members
• 67 posts

Posted 12 March 2007 - 09:09 PM

Right I made a video showing what happens exactly. The camera is pulling away slowly and the "cube" is rotating on it's Y-Axis:

Clicky

Any help at all?

#14Nils Pipenbrinck

Senior Member

• Members
• 597 posts

Posted 12 March 2007 - 09:56 PM

I can see exactly two triangles. And their indices aren't correct. I don't thinkt that you pass the wrong indices anymore. It must be something else, like the creation of the indes buffer maybe..

Here's what I see (from a triangle point of view):

My music: http://myspace.com/planetarchh <-- my music

My stuff: torus.untergrund.net <-- some diy electronic stuff and more.

#15Rubicon

Member

• Members
• 90 posts

Posted 13 March 2007 - 12:16 AM

Heh, I know that diagram so well! :surrender

So often do I see it, I've even named it:

"Cats Ears" :wallbash:
Regards, Paul Johnson
www.rubicondev.com
My Free 3D Tower Defence Game

#16Dom_152

Member

• Members
• 67 posts

Posted 13 March 2007 - 07:46 AM

I don't use index or vertex buffers directly. I use DrawIndexedPrimitiveUP(). Could it be that it's not interpretating my index data correctly? The index data I pass to it is an array of type unsigned long.

Here is my call to the D3D Function:

m_d3d9Device->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, rData->GetVertexCount(), rData->GetIndexCount() / 3,
(void *)indexData, D3DFMT_INDEX16, rData->GetVertexData(), sizeof(Vertex_Diffuse));

#17Rubicon

Member

• Members
• 90 posts

Posted 13 March 2007 - 09:46 AM

And yet you're specifically telling it they're U16 size with D3DFMT_INDEX16. There's a cracking explanation of how to call these functions in the docs...
Regards, Paul Johnson
www.rubicondev.com
My Free 3D Tower Defence Game

#18Nils Pipenbrinck

Senior Member

• Members
• 597 posts

Posted 13 March 2007 - 10:56 AM

problem solved. :-)
My music: http://myspace.com/planetarchh <-- my music

My stuff: torus.untergrund.net <-- some diy electronic stuff and more.

#19Dom_152

Member

• Members
• 67 posts

Posted 13 March 2007 - 04:16 PM

Well I changed D3DFMT_INDEX16 to D3DFMT_INDEX32, which is the only other option, before but it still doesn't make a cube. I get something weird like this:

CLICK

That's the object rotating slowly on it's X-Axis. The camera is also at -35 on the Z Axis and 20 on the Y looking at 0,0,0. I also notice that as I pull away from the object the parts that are off camera just keep stretching. I can never get the whole thing on the screen at once.

#20Kenneth Gorking

Senior Member

• Members
• 939 posts

Posted 13 March 2007 - 05:39 PM

Your indices are stored in an 'indices' array above, but in the draw-call you use indexData. Are you by any chance mem-copying the data from one array to another, and if so, are you sure their sizes are the same? i.e not copying from a 'byte*' array to an 'unsigned long*' array.
"Stupid bug! You go squish now!!" - Homer Simpson

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users