•      Sign In
• Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

# need help with transformation to matrix

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1Lucky808  Members   -  Reputation: 139

Like
0Likes
Like

Posted 04 March 2013 - 09:39 PM

Hi,

I need help figuring out how matrix works in this example

the custom defined identity matrix as follows:

aMatrix[0] = 1.0f; aMatrix[1] = 0.0f; aMatrix[2] = 0.0f;

aMatrix[3] = 0.0f; aMatrix[4] = 1.0f; aMatrix[5] = 0.0f;

aMatrix[6] = 0.0f; aMatrix[7] = 0.0f; aMatrix[8] = 1.0f;

then here is the translation transformation:

aMatrix[6] = aPoint.x * aMatrix[0] + aPoint.y * aMatrix[3] + aMatrix[6];

aMatrix[7] = aPoint.x * aMatrix[1] + aPoint.y * aMatrix[4] + aMatrix[7];

the last piece to transform the vertices:

aTransformedQuad->vertex1.geometryVertex.x = aQuad->vertex1.geometryVertex.x * aMatrix[0] + aQuad->vertex1.geometryVertex.y * aMatrix[3] + aMatrix[6];

aTransformedQuad->vertex1.geometryVertex.y = aQuad->vertex1.geometryVertex.x * aMatrix[1] + aQuad->vertex1.geometryVertex.y * aMatrix[4] + aMatrix[7];

aTransformedQuad->vertex2.geometryVertex.x = aQuad->vertex2.geometryVertex.x * aMatrix[0] + aQuad->vertex2.geometryVertex.y * aMatrix[3] + aMatrix[6];

aTransformedQuad->vertex2.geometryVertex.y = aQuad->vertex2.geometryVertex.x * aMatrix[1] + aQuad->vertex2.geometryVertex.y * aMatrix[4] + aMatrix[7];

aTransformedQuad->vertex3.geometryVertex.x = aQuad->vertex3.geometryVertex.x * aMatrix[0] + aQuad->vertex3.geometryVertex.y * aMatrix[3] + aMatrix[6];

aTransformedQuad->vertex3.geometryVertex.y = aQuad->vertex3.geometryVertex.x * aMatrix[1] + aQuad->vertex3.geometryVertex.y * aMatrix[4] + aMatrix[7];

aTransformedQuad->vertex4.geometryVertex.x = aQuad->vertex4.geometryVertex.x * aMatrix[0] + aQuad->vertex4.geometryVertex.y * aMatrix[3] + aMatrix[6];

aTransformedQuad->vertex4.geometryVertex.y = aQuad->vertex4.geometryVertex.x * aMatrix[1] + aQuad->vertex4.geometryVertex.y * aMatrix[4] + aMatrix[7];

Now, I understand so far they only apply to X-axis and Y-axis. Can someone please tell me how I can implement Z-axis into this code.

Thanks in advance for any input!

Ad:

### #2mhagain  Members   -  Reputation: 3814

Like
0Likes
Like

Posted 05 March 2013 - 11:05 AM

Use a 4x4 matrix; the top-left 3x3 portion is for rotation, and - depending on whether you favour row-major or column-major layout - the bottom row or rightmost column will contain translation.  Everything else pretty much extrapolates from your current 3x3 setup.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #3Lucky808  Members   -  Reputation: 139

Like
0Likes
Like

Posted 05 March 2013 - 07:33 PM

Thanks for the tips. I've recreated it in 4x4 matrix but it causes my app to crash every time I run it.

did I do it right? please take a look:

4X4 matrix:

aMatrix[0] =  1.0f; aMatrix[1] =  0.0f; aMatrix[2] =  0.0f;  aMatrix[3] =  0.0f;

aMatrix[4] =  0.0f; aMatrix[5] =  1.0f; aMatrix[6] =  0.0f;  aMatrix[7] =  0.0f;

aMatrix[8] =  0.0f; aMatrix[9] =  0.0f; aMatrix[10] = 1.0f;  aMatrix[11] = 0.0f;

aMatrix[12] = 0.0f; aMatrix[13] = 0.0f; aMatrix[14] = 0.0f;  aMatrix[15] = 1.0f;

Translation:

aMatrix[12] = aPoint.x * aMatrix[0] + aPoint.y * aMatrix[4] + aPoint.z * aMatrix[8] + aMatrix[12];

aMatrix[13] = aPoint.x * aMatrix[1] + aPoint.y * aMatrix[5] + aPoint.z * aMatrix[9] + aMatrix[13];

aMatrix[14] = aPoint.x * aMatrix[2] + aPoint.y * aMatrix[6] + aPoint.z * aMatrix[10] + aMatrix[14];

transform the vertices:

aTransformedQuad->vertex1.geometryVertex.x = aQuad->vertex1.geometryVertex.x * aMatrix[0] + aQuad->vertex1.geometryVertex.y * aMatrix[4] + aQuad->vertex1.geometryVertex.z * aMatrix[8] + aMatrix[12];

aTransformedQuad->vertex1.geometryVertex.y = aQuad->vertex1.geometryVertex.x * aMatrix[1] + aQuad->vertex1.geometryVertex.y * aMatrix[5] + aQuad->vertex1.geometryVertex.z * aMatrix[9] + aMatrix[13];

aTransformedQuad->vertex1.geometryVertex.z = aQuad->vertex1.geometryVertex.x * aMatrix[2] + aQuad->vertex1.geometryVertex.y * aMatrix[6] + aQuad->vertex1.geometryVertex.z * aMatrix[10]+ aMatrix[14];

aTransformedQuad->vertex2.geometryVertex.x = aQuad->vertex2.geometryVertex.x * aMatrix[0] + aQuad->vertex2.geometryVertex.y * aMatrix[4] + aQuad->vertex2.geometryVertex.z * aMatrix[8] + aMatrix[12];

aTransformedQuad->vertex2.geometryVertex.y = aQuad->vertex2.geometryVertex.x * aMatrix[1] + aQuad->vertex2.geometryVertex.y * aMatrix[5] + aQuad->vertex2.geometryVertex.z * aMatrix[9] + aMatrix[13];

aTransformedQuad->vertex2.geometryVertex.z = aQuad->vertex2.geometryVertex.x * aMatrix[2] + aQuad->vertex2.geometryVertex.y * aMatrix[6] + aQuad->vertex2.geometryVertex.z * aMatrix[10]+ aMatrix[14];

aTransformedQuad->vertex3.geometryVertex.x = aQuad->vertex3.geometryVertex.x * aMatrix[0] + aQuad->vertex3.geometryVertex.y * aMatrix[4] + aQuad->vertex3.geometryVertex.z * aMatrix[8] + aMatrix[12];

aTransformedQuad->vertex3.geometryVertex.y = aQuad->vertex3.geometryVertex.x * aMatrix[1] + aQuad->vertex3.geometryVertex.y * aMatrix[5] + aQuad->vertex3.geometryVertex.z * aMatrix[9] + aMatrix[13];

aTransformedQuad->vertex3.geometryVertex.z = aQuad->vertex3.geometryVertex.x * aMatrix[2] + aQuad->vertex3.geometryVertex.y * aMatrix[6] + aQuad->vertex3.geometryVertex.z * aMatrix[10]+ aMatrix[14];

aTransformedQuad->vertex4.geometryVertex.x = aQuad->vertex4.geometryVertex.x * aMatrix[0] + aQuad->vertex4.geometryVertex.y * aMatrix[4] + aQuad->vertex4.geometryVertex.z * aMatrix[8] + aMatrix[12];

aTransformedQuad->vertex4.geometryVertex.y = aQuad->vertex4.geometryVertex.x * aMatrix[1] + aQuad->vertex4.geometryVertex.y * aMatrix[5] + aQuad->vertex4.geometryVertex.z * aMatrix[9] + aMatrix[13];

aTransformedQuad->vertex4.geometryVertex.z = aQuad->vertex4.geometryVertex.x * aMatrix[2] + aQuad->vertex4.geometryVertex.y * aMatrix[6] + aQuad->vertex4.geometryVertex.z * aMatrix[10]+ aMatrix[14];

Thanks for any comments!

### #4mhagain  Members   -  Reputation: 3814

Like
0Likes
Like

Posted 06 March 2013 - 09:37 AM

Have you run this in a debugger?  I'd strongly suggest that you do so - it will tell you (1) where it crashed, and (2) what crashed it.  In this case I'd suspect that your definition of aMatrix is not a size-16 array, but that's just a guess - you should get used to using your debugger as a good habit.

Edited by mhagain, 06 March 2013 - 09:38 AM.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #5Lucky808  Members   -  Reputation: 139

Like
0Likes
Like

Posted 06 March 2013 - 08:06 PM

You were absolutely right! i forgot to update my aMatrix array to [16]. Thank-you!

Now that the app would run ok, but the z-buffer doesn't seem to work properly. Half of my player character's image got overlapped by other objects even when it has less z-values than the other objects. any idea why? (overlapping only happens when player is placed in the front. player hides himself properly when placed behind objects).

Thanks in advance!

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS