Bright Hub
 
Matthew Casperson's Hubfolio

Away3D Programming Tutorials - Spring Camera

Article by Matthew Casperson (227,413 pts )
Published on Sep 20, 2009

Learn how to use a spring camera to create a 3rd person view in Away3D.

Introduction

In the last tutorial we saw how to use the hover camera to get a view of a static object. Here we will look at the spring camera, which is useful when you want to follow a moving object. It allows a 3rd person view of a target, much like the view you would see in World Of Warcraft.

Code Documentation

Player.as

In order to show off the spring camera we need a player mesh that can move around. For this we will make use of the MD2 mesh from this tutorial, terrain rendering from this tutorial, and the terrain following using the ElevationReader class from this tutorial. The Player class puts all these aspects together to create an avatar that can run around on some terrain.

First we load and texture the MD2 mesh. Note that we supply the scale to the MD2 parse, instead of using the scaleX, scaleY and scaleZ properties of the mesh that is returned. This is because scaling the mesh after it has been parsed modifies the transformation matrix, which is used by the spring camera. So if the mesh was scaled by 0.1, all the values passed to the spring camera would have to be 10 times larger to compensate. Having the mesh returned already scaled avoids these issues. [code]

We then supply the mesh to the base startupObject3DMeshObject function. [code]

The other functions, like setAnimation and keyDown/keyUp are only slight modifications of the same functions in the previous tutorials.

The initial animation is set to “stand”. [code]

We setup the ElevationReader. [code]

Now we create the spring camera. [code]

Unlike most Away3D classes, the spring camera does not read values from an init object passed into the constructor. This is why each property is set individually.

The positionOffset property defines the ideal position of the camera, relative to the object it is targeting. Here we set the camera to view the mesh from slightly above and to the right. [code]

The dampening property defines how “springy” the camera will be. [code]

The stiffness property defines how hard the camera is to move. The higher the value the more closely the camera will stick to the target. [code]

The target property defines which object the camera will follow. We set this to the MD2 mesh we loaded above. [code]

Finally the spring camera is set at the default camera by assigning it to the view.camera property. [code]

Final Words

The spring camera allows you to create a fluid 3rd person camera, which is ideal for games like RPGs and adventure games. Combined with terrain, terrain following and animated models you have a good basis for creating your own games.

Go back to Away3D Tutorials

Related Files

Images

Screenshot

Search More About:

Comments

May 24, 2010 4:47 AM
dcx
Camera Probleme
I try this sample with flex sdk 3.4 , spring camera don't move and the Mesh was changed too large!

thanks a lot! please have a try !
May 9, 2010 9:51 AM
Todor Jelev
moving
can you explain me why do you have two boolean variables for managing animation - moving and stillMoving?
Mar 16, 2010 1:03 PM
Vincent
Camera Problem
Manuel: +1
Feb 3, 2010 10:09 PM
Amy
imports for Flash IDE 9?
I'm trying to get the sample source to run in my Flash IDE 9 and I'm experiencing compatibility issues with import mx.core.Application and ArrayCollection. Do you have the AS3 code or the .fla file equivalent for this sample? Thanks!
Oct 21, 2009 8:27 AM
manuel
Camera Probleme
I try this sample with Flex , but spring camera don't move ?
Mesh and landscape was ok.
what i have forget ? why camera not same as your Demo SWF file.

thks a lot
 
Follow Matthew Casperson
Receive weekly updates from Matthew Casperson
 
Bright Hub - Science & Technology Articles, Buyer's Guides, How-To Tips and Software Reviews
About Bright Hub | Contact Us | Advertise with Us | Become a Writer | RSS | Site Map | Terms of Use | Privacy Policy | Copyright Policy
©2010 Bright Hub Inc. All rights reserved. Page copy protected against web site content infringement by Copyscape