Reputation: 3
I have a question related to projecting 3D xyz coordinates to a 2D representation in a window. So far, I have succesfully created a 3D world where the coordinates are happening correctly. I have also succesfully managed to project the xyz coordinates to a 2D plane in such a way, that the view is directly pointed towards the z dimension (which is the depth in this case, added to a normal xy plane on the window). This I've done simply by multiplying x and y values by ((viewpoint at z-axis) / (z value)).
Also, the coordinates represent a ball, which gets smaller as it goes "deeper" away from the view point, and bigger as it gets closer. This I've done simply by adjusting the size of the ball in relation to the size of the z value. All the x, y , and z values happen inside [300, 700] in the 3D world coordinates, meaning inside a cube.
Now, problems arise when I would want to rotate the projection of the cube. Suppose I find the right calculation to rotate the projection as pictured in the attached image, meaning ~40 degrees along the Y axis and ~45 degrees along the X axis. How will I then know how far the object is from the view point, in order to display it in the correct size?
Thank you so much already in advance!
Edit:
I have now decided to convert my classic xyz coordinates to spherical coordinates, which have distance from origin, angle between x and y axis, and angle between z and y axis.
Now, suppose I have my cube's opposite corners at (300,300,-300) and (600,600,-600) in a right-handed coordinate system. Suppose I take the origin (0,0,0) as the camera location for the projection. Succesfully convert these and all the other corner coordinates of the cube to spherical coordinates, in this case examples above become (519.62, 45, 125.26) and (1039.23, 45, 125.26). So can I now just discard the distance and convert the angles to some approriate relative pixels on the screen?
I tried it but it just doesn't seem exactly as it should be in my understanding. Any thoughts or tips would be appreciated.
The python code I used to convert xyz to spherical was the following, and it gave the distance from origin very nicely, if anyone is looking a way to do that:
import math
x=300
y=300
z=-300
dist = math.sqrt(x**2 + y**2 + z**2)
polar_angle = math.atan(y/x)
azimuthal_angle = math.acos(z/dist)
Upvotes: 0
Views: 44