3d wrapping, detecting view boundaries?

I wish to wrap 2d sprites, set in a 3d environment, but I’m having trouble figuring out the view boundaries for x and y. I move and wrap a number of sprites over the z, to make it appear as if you’re flying through them which is simple enough, but depending on their z value their wrapping bounds over the x and y axis should become larger the further they are away from the camera.

I’m not sure how to figure out the correct bounding value depending on the sprites z value relative to the camera. The camera is static in this case and points forward over the z. I move the sprites around it.

@Kirl, if you’re asking how many pixels “wide” the visible screen is at different distances, it’s quite simple (the code, that is -wrapping your head around the explanation may take a little longer).

https://coolcodea.wordpress.com/2014/12/31/190-figuring-out-the-borders-of-a-3d-screen/

Thank you ignaz! Excellent work on your webpage, very useful!

It looks like there’s a mistake in the example code right below the “Figuring out when objects fall off the edges” section. Looks like something went wrong with the if statement at the bottom of the code snippet. Missing “and” etc.

Thanks for the help!

@Kirl - I can’t see an error in the code

I copy pasted the snippet that looked wrong below (the if statement). Your explanation was fine however, it works and I’m in the process of wrapping my head around it. :slight_smile:

xMax = -z / projectionMatrix()[1] 
yMax = -z / projectionMatrix()[6]
if math.abs(x)>xMax or math.abs(y)>yMax then 
    --delete asteroid
end

Btw, this code only works if the camera is facing directly forward. If you want code that works when the camera is facing in any direction, see the post I just made for my World of Tanks project.