
When building flash 3D application it is often necessary to implement complex routines to manage camera movements. Wether it’s a 3rd person follow cam, a first person FPS camera or a more customized movements most of the time it would be desirable for developers to have access to a simple camera that implements a “natural” movement.
While developing a small racing game internally as a pet project, I came across the need to develop such a camera, one that would allow us to easily tail a moving object in the scene while keeping the right orientation in relation to the subject. Though, that is simple to do by itself, what really makes this camera interesting is that it’s not a “perfect” follow. In order to give a good acceleration effect on turns and in “turbo boost” moments, I wanted a camera that would be a bit late in it’s follow and allow for smooth and more natural feel, something that you see in classic 3D racing games.
Hence the Spring Camera was born, a camera that works very simply by implementing spring physics between the camera and it’s target. Imagine the camera is linked by a spring to a target, when the target moves in 3D space the spring extends and then “pulls” the camera thus giving a nice natural feel to the movement.
“One camera to rule them all”
Here is a sample Away3D code to initialize a spring cam in the View. By default, the spring motion is activated once the target property of the camera is set:
Ovbiously, there are more options. 3 main parameters are used to control the behavior of the camera:
Mass - The “weight” of the camera, how difficult it is to pull it.
Stiffness - How hard the spring is to extend. A high value here is as if the spring was a stick (or fixed lenght)
Damping - Controls how the spring resists the spring “boingggg” effect. A higher value makes for a smoother ride, lower values and the more boing you’ll get.
The camera also implements 2 important offset:
Position offset - Where the camera should be ideally in relation to the target (in model space). For example, if we want the camera to try to be 100 units behind the target, we’ll use a value of Number3D(0,0,-100); Obviously if the target moves fast we won’t stay 100 units behind exactly which is how the effect is created.
Look offset - Where the camera should look at in relation to the target. By default, the camera look AT the target but you might want to have the camera attempt to look at a different spot. For example, Number3D(-100,0,0) would look 100 units to the left of the target. (all this in target model space).
And that’s pretty much all there is to it in terms of usage! The camera will automatically set it’s position in relation to the target based on the physics of the spring every frame without any additional code. Now all you need to do is move the target in 3d space and change it’s orientation…the camera will automatically follow.
All of these parameters, including target can be modified at runtime and will be updated real-time so you can do some pretty funky adjustments in your application code to create specific effect when needed.
This camera can be used to build a wide variety of cool cameras movements including but not limited to 3rd person camera, 1st person camera, chase cam, as well as all the standard camera parameters. You can even use it for cool transition effects!
Take a look at the Spring physics powered 3D camera demonstration.
Download the SpringCam class for Away3D.
Simply add the class in your Away3D source in the src/away3d/cameras folder.
Download the SpringCamera3D class for Papervision3D.
Simply add the class in your PV3D source in the src/org/papervision3d/cameras folder.
You can also grab the source code to the demonstration (away3d 2.2.1+ required)
Special thanks go to Kim Reeves for giving me a needed hand in whipping up the demo. Kudos!
UPDATE: John Lindquist created a short example using the original Focus example for pv3d with source available.
Hey if you like it or use it in a project, let us know with a comment and subscribe to our RSS feed!