Coordinate Utility class for Papervision 3D

This class factors out a lot of work that needs to be done to position 3D objects at variable rotations, world coordinates, and screen distances. Use the link below to get the utility class. Using this class requires papervision. If you don’t already have papervision, then you probably don’t need this class… Also, the sample isn’t pretty (graphically), and the source has some weird key controls. They are completely unintuitive but get the job done for testing :)

Coordinate Utility class with sample source
Coordinate Utility demo

Sample
Here is an API snippet to give you an idea of what it does. The following code will reposition a plane to the coordinates defined by the screenCoordinates object. It will also render the plane in the scale of the movieclip material source.

//gets the distance the plane should be so the movieclip renders at its actual scale.
var distance:Number = CoordinateUtil.getTrueScaleDistance(camera);
var screenCoordinates:Number2D = new Number2D(100, 100); //based on a centered registration point
CoordinateUtil.setObjectByScreenCoordinates(camera, plane, screenCoordinates, distance);

History
Some time ago I wrote an article titled Plane Scaling and Positioning with Papervision. I shared the solutions I had used to over come some of the challenges us RIA developers face when developing 3D interfaces. Some time later I was asked about how to solve these problems under different conditions.

Well, I know this is a tough thing for most flash developers to accomplish. I know your boss is expecting you to do it and to do it right. I know we need to be able to do this stuff just by the way the typical web shop work flow goes. The designer design something and gives you a psd. Great. Now how are we suppose to precisely implement all of these 3D transformations without iterations of trial and error? I think this is something our community needs.

Sorry it took so long for those who have asked about this. Working on this, and related projects, I have fallen into knowledge tangents about 3D math (and just plain math and physics in general) in the past couple of months. It has made me even more knowledge hungry. It’s this particular issue of making papervision practical for interfaces, my interest in particle physics, FP10, and the video game I’m working on using another open source rendering engine in C++ that has really been taking up every second of my life. What can I say, I really love this stuff. And, sadly, lately I’ve just had nothing better to do.

Some of the more advanced readers might be wondering “Umm… Why didn’t you use unproject?” Well, to be honest, because it just did not do as much as I needed it to do. It was also slightly inaccurate. I didn’t spend much time with it. It’s entirely possible that I may have just been using it improperly.

That’s it for now. Hope this helps some of you out there! Please post any bugs that you might come across. It is absolutely possible that I will update this article as papervision evolves or if I, or you, come up with better (more optimal) techniques for this class.

18 Responses to “Coordinate Utility class for Papervision 3D”


  • Thanks H, hope to see you soon @ MAX

  • Hey buddy! Yep, looking forward to it. Hope all is well with you.

  • Thanks..exactly what i was looking for. been doing trial an error for sometime now

  • I’m glad it was of use to you :)

  • yes.. it did! i am diving into the code. what i really want to do..is say dynamically put points on ur app. my application has points which are used for illustrations… of say a room. if i could preview the room and dynamically put the points/dots ie… to find out what those coordinates are…would be great! or even move the points around using drag and somehow see what these coordinates are. any pointers u might have for me?

  • Hi scriptonian,

    That is built in. Use displayObject3D.calculateScreenCoords(camera) to calculate displayObject3D.screen, which is a Number3D that has the (center origin) x y screen coordinates.

    You can use something like this to move stuff around with mouseX and mouseY as input:

    var newPosition:Number3D = CoordinateUtil.objectScreenToSceneCoordinates(
    camera
    , new Number2D(mouseX, mouseY)
    , Number3D.sub(displayObject3D.position, camera.position).modulo
    );

    Hope that helps.

  • Yes i noticed after i sent that post. wow…its such a great utility class. thanks a million

  • So can this be used to position planes relitive to the side of the screen – it would be awesome to keep the planes in the main scene but kkep them attached to the side of the screen usinge the usual stage width x height!

  • Yes, you can do that :) I think the demo I have up for this does just that.

  • i think the demo is a bit confusing in context! but i think i have it worked out now – thanks for an amazing class!

  • You’re very welcome!

  • thanks,it’s helpful for me.

  • I’ve been playing around with your class, and Im sorry if I was mistaken with papervision, but I cant seem to find any pre-existing functions to project a 3D coordinate onto a 2D plane.

    I’ve managed to reverse engineer your function to convert a 2D point onto the 3D scene. The only problem I am having with it is the calculations are completely off when there are any rotation values applied to the camera. I believe the error originates when it comes time to rotate the camera transform by a vector matrix.

    I could quite possibly be going wrong somewhere, because all I am doing is just reversing the steps you took to get the 3D coordinate from a 2D point.

    Is there any light you might be able to shed on this?

  • Hi James,

    I apologize for the late response to your question. I have been very busy at work, and outside of work. I have emailed you, though you may have already solved this issue :)

  • James,

    I am posting the answer to your question so that others will have the answer in case they have the same question.

    The math for projection in papervision 2.0 is a matter of finding the perspective of the camera object. Once this has been achieved, one can project a 3D object to the screen by doing some basic math on the coordinates of the 3D object in camera space.

    The following equation assumes the coordinates are relative to the coordinate space of the camera. This can be achieved by multiplying the objects world transform with the camera transform, but I believe there is a public var for 3D objects in papervision that has this transform already computed. The trick is to look out for optimizations. The public var camera transform may not be updated unless that object has been rendered. I believe this property is screen, but this may have changed or may change with the upcoming version of papervision.

    Assume in the following code snippet that cam is the camera object, object is the 3D object, and screen is the objects transform in camera space.

    var perspective = cam.focus*cam.zoom/screen.z;
    var x = object.x*perspective;
    var y = object.y*perspective;

    That’s it!

    The math for this is bit more involved when using a projection matrix.

  • wow, again i’ve come searching and found the answer in the same place :D thanks for this utility class :D it was again very helpful.

  • You are very welcome Jairus :) I’m glad it has been so helpful to you!

  • Hey just a word : It’s really awesome and so useful…

    Thanks a lot !

Leave a Reply