# Calculating points behind camera

This topic is 1800 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello guys.

I've been getting into crafting my own 3D-engine in Java (mostly for kicks, but also I like to have full control over everything I use).

It's been going great for the most part (haven't gotten far), and I can now draw wireframes almost perfectly.

The formula I use for calculating where to draw a point on the screen is:

zDifference = abs(z - camera.z);

screenX = camera.lens * (x - camera.x) / zDifference);
screenY = camera.lens * (y - camera.y) / zDifference);


This works just fine, except for one thing.

When the point goes back behind the camera, it completely stops working.

Ok, so obviously I shouldn't draw a point behind the camera, right? Problem solved? Well, almost...

Image a line, drawn between two points. Imagine walking past the first one, but the second one still being visible. When this happens, the line goes haywire, since the calculations for the first point stop working. So I can't just "not draw the point", since I need it to draw the complete line. I still need to calculate where on the screen (it will be off-screen, but still) this point should be drawn, so I can after that draw a line between the two.

You can check out and see the issue in-action here.

So, how should I go about fixing this? Is the formula completely stupid, or should I find a way to work around this issue?

Any response is greatly appreciated

Have a good one,

~Vrokulaf

Edited by Vrokulaf

##### Share on other sites

You can see how other APIs, such as OpenGL and Direct3D, handles this. What they do is basically inserting a clipping stage in the middle of transforming the object from model space to screen space to clip the object so that nothing extends outside the view volume (basically represented by the borders of your window and the near and far clip planes of your camera).

Instead of transforming your coordinates all the way to screen coordinates in one go, transform the coordinates to an intermediate clip space coordinate. This coordinate system is typically bounded by a unit cube or something similar, and you can then clip your line or polygon against this unit cube to ensure that only the visible parts remain. Then, take the final step to transform the clipped objects into screen space and rasterize it.