Orbit in Papervision, the real way and the easy way
Filed under Lab Work, Programming | Tags:Actionscript 3,Flash,math,papervision3d
I have been working with papervision3D only a bit now, and really starting to get the concepts of the camera, 3D space, containers, prmitives, etc. The hardest part by all means has been knowing/learning when to manage the camera vs just the container object holding 3D objects. A hot tip (up for dispute) from one of our motion designers was to try not to move the camera and to rely on the container rotation. This is great, if the background is static and has no need to relay an orbiting effect. However, in my case, i needed the camera to swing around the object in question. Knowing that, my approach was to “fasten” the camera to a virutal boom. Essentially, this “boom” would be an empty DisplayObject3D that will spin. The camera would in turn respond to that rotation accordingly.
So, into Trigland we go. Like all Flash trig mind games, the only thing you will need is an angle and a radius. The rest was done thousands of years ago by the greeks using their good friends cosine and sine.
[NOTE: i have to give all the math credit to Tyler Egeto from a post at the DDBlog, here . Tyler, you don't know it, but thanks.]
//calling the method below from my render loop(enterFrame) private function setCamera() : void { var rdx = boom.rotationX *(Math.PI/180); //rotation in radians var rdy = boom.rotationY *(Math.PI/180); //rotation in radians var radius = boom.distanceTo(cam); /*----------------------------------------------------------------------> //Egeto's original math follows the mouse movement. A very cool effect: var theta = mouseX*0.02; var phi = mouseY*0.02; cam.x = radius * Math.cos(theta) * Math.sin(phi) cam.z = radius * Math.sin(theta) * Math.sin(phi) cam.y = radius * Math.cos(phi); //however i want to follow the "boom", so i need to change up a few things in the formula //(not the swap of sin and cos, as well as the cam.x swap with cam.y) ---------------------------------------------------------------------->*/ var theta = rdx var phi = rdy cam.y = radius * Math.sin(theta) * Math.cos(phi) cam.z = radius * Math.cos(theta) * Math.cos(phi) cam.x = radius * Math.sin(phi); } however, let it be known, the same guy who told me to resist moving the camera (and also knows very little about code), seemed to know enough to bring the orbit method into a useful purpose. cam.orbit(90,boom.rotationY,true,boom);
ha!. Well, not all for nothing. Practicing the trig is hugely necessary and will no doubt pay off another day.