What is wrong with this camera rotation algorithm?

I am trying to implement 3D graphics for a small game that I am working on, and the rotation of the camera does not work quite right. For the camera, I use a spherical coordinate system with alpha in the form of an azimuthal angle (theta in the picture) and beta as 90 - polar angle (90 - phi in the picture):

enter image description here

Visibility in the xy plane or objects with relatively small z-coordinates are apparently in order. However, looking at the z axis, objects become distorted. Here is an example of considering a sphere of radius 100 at (0, 0, 500) from (0, 0, 0):

enter image description here

And here is an example of how to look at the same sphere in (500, 0, 0) from (0, 0, 0):

enter image description here

I am pretty sure that the projection algorithm is fine and that this is camera rotation. Here is the code I use for the camera rotation algorithm. The distance () method returns the distance from one coordinate to another. point - the coordinate (x, y, z) for the point I'm trying to rotate. camera - the coordinate (x, y, z) for the location of the camera. alpha is azimuth, and beta is 90 is the polar angle. Ultimately, I want the point to be rotated so that it is positioned relative 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 works in the eyes. Since y and z are symmetrical, I doubt this part is a problem. 750 is the height and width of the applet I'm drawing onto.

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 spent hours trying to solve this problem so far, but to no avail. I am very grateful to everyone who could help me! Thanks to everyone who reads this.

+4
1

Java Math , . - -89, , -pi/2.

, , . , . " ", , " ".

0

All Articles