4
votes

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):

enter image description here

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):

enter image description here

And here is an example of looking at the same sphere at (500, 0, 0) from (0, 0, 0):

enter image description here

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.

1
Is there more code, like how are you building up a matrix for the camera?weston
I'm storing the x, y, and z coordinates for the camera as well as its spherical coordinates. This logic is done in the camera class. this.alpha refers to the azimuth angling of the camera. this.beta refers to 90 - the polar angling of the camera. alpha refers to the new angling of the azimuth after it has been transformed with respect to the camera, and the same for beta. Sorry I didn't make that clear! I edited the post to clarify. Also, should I post the entire code for the projection algorithm?user2605633
Well, you have shown how you calculate one point. How can that one point distort the view? I think you should post the projection algorithm then. Are you using OpenGL?weston
This is the method body for part of the projection algorithm (shifting the point with respect to camera). I'm using standard Java libraries and graphics to draw to screen.user2605633
It's usual to use a matrix. Libraries such as opengl help you create them for cameras with a method called 'look at'.weston

1 Answers

0
votes

The Java Math functions use radians, not degrees. In your first example, your debug output shows a camera beta of -89, but you actually want it to be -pi/2.

Better yet, avoid all the trigonometry completely by doing everything in terms of vectors. I find it very unlikely that a programming competition to make a game would rule out using a graphics library. "No external resources" probably means asset collection or a "make your own flappy bird" kit.