Using Sandy 3D Flash Library
Part 3, The Camera
If you followed this series on using the Sandy API, you know that it uses a camera metaphor for the on screen presentation. The Camera3D object really performs like a camera, through which you observe the 3D world. Like with any other camera, you can move it around and point it in any direction, to see the world from different positions and angles. In this installment I want to introduce you to the handling of this fine instrument. I have written an interactive Flash application, that lets you control most of the the basic camera movements, and to see what gets in its scope.
Everything you see in a Sand 3D world goes through the camera. The camera has a position in the world, and can be moved around and pointed in any direction. It may also be animated, to give you a walk or a fly through in your 3D world. You can read the author's description of the camera here.
You introduce one or more cameras in the world by first creating an instance of
Camera3D and then calling the World3D.addCamera( cam:Camera3D ).
The camera looks at a screen, where the world draws itself, when we call the render() function.
// Create the world var world:World3D = World3D.getInstance() // Create a clipScreen as presentation surface for the World3D to draw on screen = new ClipScreen( this.createEmptyMovieClip('screen_mc', 1), 480, 400 );
After creating the world and a ClipScreen, we create the camera and associate it with the screen. the constructor also takes a focal distance as the first argument.
// Create a camera, so we can see our world. camera = new Camera3D( 700, screen ); // Position the camera at a negative distance along the z axis camera.setPosition(0,0,-500); // Attach the camera to the world world.addCamera( camera );
The camera is created in the origin by default, and it is looking in the positive z direction. To be able to see an object at the origin, we have to drag the camera back along the negative z axis. Here we place the camera at [0, 0, -500] in world coordinates.
Two coordinate systems
When we move the camera around, we have to deal with two different sets of coordinates, the global coordinate system and the camera's local system. When we first create a World3D and a camera, they are parallel. The x axes points to the right on the screen, the y axis points upwards and the z axis points away from us into the screen.
When we begin to move the camera around, and to rotate it, the coordinate systems are no longer parallel. As we always view the world through the camera, the cameras local axes always have the same orientation, the z axis pointing away from us, the x axis pointing to the right and the y axis upwards. If you think of the camera as your eye looking at the world, your line of sight is always the local z axis.
Enough talk already, I'm sure you got it ;)
Here is why I gave you so many words about the global and local coordinate systems. The camera in Sandy has a lot of flexibility. It can be translated relative to the global system using setPosition() and rotated around the global axes, using rotateX(), rotateY() and rotateZ().
But it can also be translated and rotated relative to its own local
using moveUpwards(), moveSideways() and moveForward(). And it may be rotated around its own coordinate axes, using pan(), tilt() and roll().
To get all this movements in perspective, I wrote an interactive Flash applet, where you can test some of the basic movements of the Sandy camera. It is a simple world, with coordinate axes and planes and a Pyramid at [0,0,0], and it has controls for basic movements like positioning, translating, rotating, panning, tilting, rolling and even zooming, by changing the focal or nodal distance.
Please take a few minutes to study the marvelous Camera3D and enjoy being the photographer.
Next on the agenda we'll study transformations that can be applied to groups of objects in a Sandy world.