Hi all, I’ve just got a quick question relating to converting world coordinates to screen coordinates. I’ve got a code snippet below that’s written in javascript rather than java, but it should be fairly understandable. My issue is that I’m performing a 3D perspective projection, but this function allows for points that are behind the camera (off the screen) to still be rendered. I’m wondering whether there’s a check that I can perform to check whether the point in question is outside of the view. I know it’s not a very good way of doing this. I’m just playing around trying to get something made. If anyone knows how I perform this check I’d appreciate your help. Thanks
var xOffset = canvas.width >> 1;
var yOffset = canvas.height >> 1;
function world_to_screen(x, y, z) {
// Vert relative to camera position
var rel_x = x - xCam;
var rel_y = y - yCam;
var rel_z = z - zCam;
// Vert relative to camera orientation
var c = Math.cos(yRot * (Math.PI / 180.0));
var s = Math.sin(yRot * (Math.PI / 180.0));
var orient_x = c * rel_x + 0 * rel_y - s * rel_z;
var orient_y = 0 * rel_x + 1 * rel_y + 0 * rel_z;
var orient_z = s * rel_x + 0 * rel_y + c * rel_z;
var proj_point = persp_point(orient_x, orient_y, orient_z);
var screen_x = ((proj_point[0] * canvas.height) | 0) + xOffset;
var screen_y = ((proj_point[1] * canvas.height) | 0) + yOffset;
return [screen_x, screen_y];
}
function persp_point(x, y, z) {
return [x / z, y / z];
}