I have been banging my head against a wall with a simple Papervision3D problem: making a normal Flash 2D object (a DisplayObject, or Sprite) to appear at the same coordinates as a 3D object (a DisplayObject3D, or Plane, or whatever).
After spending the best part of a day scouring mailing lists, I found this method that solved the problem:
public function convertTo2DCoords(o:DisplayObject3D, camera:CameraObject3D, offsetX:Number = 0, offsetY:Number = 0):Point {
var view:Matrix3D = o.view;
var persp:Number = (camera.focus * camera.zoom) / (camera.focus + view.n34);
return new Point((view.n14 * persp) + offsetX, (view.n24 * persp) + offsetY);
}
You just need to add half the Stage.width to point.x, and half the Stage.height to point.y, and Bob’s your uncle …
(I was working with a 3D scene that filled the whole stage … might need some tweaking if this is not the case …)
I am posting this because, although it’s apparently a quite basic Papervision problem, it took ages to find it. We need better PV3D documentation!




Great find! Now, do you happen to know how to reverse it?
Starting a pv3d project where I’m going to have to be jumping back and forth between 3d and MCs, and thought I would ask before spending the better part of the day figuring out how to reverse it.
If not, I’ll share my results (when I figure it out). Thanks
Hi!
Just for info, if your viewport doesn’t fill the whole stage, it’s just a matter of adding the viewport.width/2 and the viewport.height/2 to the Point’s x and y coordinates respectively.
Cheers!
Why not use object.screen.x and object.screen.y?
Because it didn’t work – at least against the bleeding edge of Great White in SVN at my last checkout.
To make this work again open this file branches\GreatWhite\src\org\papervision3d\objects\DisplayObject3D.as
Then go to empty line 756 and put this extra line of code:
calculateScreenCoords( renderSessionData.camera );
right above this two lines:
var screenZs :Number = 0;
var children :Number = 0;
how would you write a function convertTo3DCoords then.
so you can input 2d coords and get them in relation to the 3d coords? I tried the .screen fix but doesnt seem to move the object around
Hi…
@Slaymantis
I’ve recently wrote a method to do the reverse ( 2d->3d coords ):
private function setScreenCoords( do3d:DisplayObject3D, camera :CameraObject3D, x:Number, y:Number ) : void {
var persp:Number = (camera.focus * camera.zoom) / (camera.focus + do3d.view.n34);
do3d.x = x / persp;
do3d.y = -y / persp;
}
Thx, u really help me. Pretty easy but i could manage to figure that out myself ^_^