This is more of a mathematical problem than programming problem.
I have created a very basic 3D engine using Visual Basic .Net. It displays lines on the screen with an additional z axis. The engine works, however when part of the line goes below 0 it messes up and starts drawing the line inverted again.
This is how it calculates the points:
y = (point.y / z) + offset.y + camera.y
x = (point.x / z) + offset.x + camera.x
Can anyone work out a way to only draw part of the line when it intersects the z=0 axis?
(source code) http://www.mediafire.com/?ww77q26y开发者_如何学Cwj3a5ry
You want to draw everything where z>0 because everything below zero is behind the camera and where Z == zero, this will blow up because you can't divide by zero. So I would do this: ('scuse the c#, but this should be the same anyway)
y = z > 0 ? (point.y / z) + offset.y + camera.y : 10000;
x = z > 0 ? (point.x / z) + offset.x + camera.x : 10000;
Where the 10000 value is something large enough that will not appear on your screen.
If your z axis is pointing in the opposite direction, you'll want to do this:
y = z < 0 ? -1*(point.y / z) + offset.y + camera.y : 10000;
x = z < 0 ? -1*(point.x / z) + offset.x + camera.x : 10000;
精彩评论