Binsk
Member
SOLVED: Oops... I should have been dividing by W rather than attempting to normalize the vector. Face-palm moment.
Hello, I am attempting to convert a point in 3D world-space into 2D screen-space on the window.
I thought that I had all the math right, but the results I get for the 2D point are completely wonky! Not even close to being correct.
Could someone take a look at this and tell me if I am missing something mathematically? I am going to have to post pseudo-code as my actual code uses a fair amount of custom structures.
That pseudo code follows my math exactly.
Am I missing something here? Any help is appreciated. Thank you.
EDIT:
As an example, I have my camera set up as such:
With those values, the above code gives me the 2D point of:
{88180.63, 29394.90}
EDIT 2:
Alright, so somewhat progress. With the finalMatrix I tried normalizing the position (so it would be between [-1, 1].
I then changed my point2D formula to this:
This is MUCH closer and the results are dead-on near the center of the view. However the close it gets to the edges of the view the more off the results become, and to a significant amount.
Help is still appreciated if someone could review the math.
Hello, I am attempting to convert a point in 3D world-space into 2D screen-space on the window.
I thought that I had all the math right, but the results I get for the 2D point are completely wonky! Not even close to being correct.
Could someone take a look at this and tell me if I am missing something mathematically? I am going to have to post pseudo-code as my actual code uses a fair amount of custom structures.
Code:
// Our 3D world point:
point3D = {0, 100, 0}
xport = 0;
yport = 0;
wport = 1280;
hport = 720;
// Retrieve GameMaker matrices from our already set projection:
viewMatrix = matrix_get(matrix_view);
projectionMatrix = matrix_get(matrix_projection);
viewProjectionMatrix = matrix_multiply(viewMatrix, projectionMatrix);
pointMatrix = {1, 0, 0, point3D.x,
0, 1, 0, point3D.y,
0, 0, 1, point3D.z,
0, 0, 0, 1}
// Multiply our 3D position with the final matrix:
finalMatrix = matrix_multiply(pointMatrix, viewProjectionMatrix);
// Adjust to account for the viewport in the window:
point2D = {finalMatrix[12] * wport + xport, finalMatrix[13] * hport + yport};
return point2D;
Am I missing something here? Any help is appreciated. Thank you.
EDIT:
As an example, I have my camera set up as such:
Code:
camXFrom = -128;
camYFrom = -128;
camZFrom = 64;
camXTo = 0;
camYto = 0;
camZto = 0;
camFOV = 60;
camZNear = 1;
camZFar = 1024;
camAspect = 1280 / 720;
{88180.63, 29394.90}
EDIT 2:
Alright, so somewhat progress. With the finalMatrix I tried normalizing the position (so it would be between [-1, 1].
I then changed my point2D formula to this:
Code:
point2D = {((finalMatrix[12] + 1) * 0.5) * wport + xport,
((1 - finalMatrix[13]) * 0.5) * hport + yport};
Help is still appreciated if someone could review the math.
Last edited: