I'm trying to implement 3d graphics for a small game I'm working on, and the camera rotation isn't working quite right. For the camera, I am using a spherical coordinate system, with alpha as the azimuthal angle (theta in the picture) and beta as 90 - the polar angle (90 - phi in the picture):
Vision in the xy plane or objects with relatively small z coordinate values seems to be in order. However, when looking into the z-axis, the objects become distorted. Here is an example of looking at a sphere of radius 100 at (0, 0, 500) from (0, 0, 0):
And here is an example of looking at the same sphere at (500, 0, 0) from (0, 0, 0):
I'm almost certain that the projection algorithm is fine and that it is the camera rotation that is screwy. Here is the code I am using for the camera rotation algorithm. The distance() method returns the distance from one coordinate to another. point is the (x, y, z) coordinate for the point I am trying to rotate. camera is the (x, y, z) coordinate for the location of the camera. alpha is the azimuth and beta is 90 - the polar angle. Ultimately, I want point to rotated so that it is positioned with respect to the camera system.
point.x -= camera.x;
point.y -= camera.y;
point.z -= camera.z;
double alpha = Math.atan2(point.y, point.x) - cameraAlpha;
double beta = Math.atan2(point.z, Math.sqrt(point.x * point.x + point.y * point.y)) - cameraBeta;
point = new Coordinate(point.distance(new Coordinate(0, 0, 0)) * Math.cos(alpha) * Math.cos(beta), point.distance(new Coordinate(0, 0, 0)) * Math.sin(alpha) * Math.cos(beta), point.distance(new Coordinate(0, 0, 0)) * Math.sin(beta));
The rest of the projection algorithm models how the lens in the eyes function. As the y and z are symmetric, I doubt it this part is the issue. 750 is the height and width of the applet I am drawing to.
if (point.x < 0){
return null;
}else{
return new Point(375 + (int) (point.y * 750 / point.x), 375 + (int) (point.z * 750 / point.x));
}
I have spent hours trying to solve this issue so far, to no avail. I would greatly appreciate anyone who could help me! Thank you to everyone who reads this.