PV3D: Finding 2D screen coordinates of a 3D object

I’ve written about how to get the 3D coordinates of a 2D point on your screen, this time I needed to do it the other way round. Here’s a nice post about both of it, but it doesn’t exactly work with the current version of Papervision3D. This one, using the DisplayObject3D.screen property, is much better, though there’s some minor things to notice:

1. dobj3d.autoCalcScreenCoords has to be set to true – some pages in the web tend to ignore that and people’ll always get (0,0,0).

2. You can only get the 2D coordinates after the object rendred, because the calcultion of the 2D point seems to use the previous render. I tried instantiating a new DisplayObject3D for testing and trace the screen coordinates right after that, and it didn’t work. Bummer. The correct way to get it should be:

myObject.autoCalcScreenCoords = true;
trace(dae.screen.x + viewport.viewportWidth / 2 + viewport.x);
trace(dae.screen.y + viewport.viewportHeight / 2 + viewport.y);

Leave a Reply

Your email address will not be published. Required fields are marked *



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>