Having got this far, we finally have something really quite useable here- the Little Purple Dude is wandering happily about the landscape and, silhouetting himself against the sky and we are able to follow the whole thing from our camera. Great. This is the kind of interface you might use for an RTS or something like that. If you want to have more of a 3rd person point of view you really need to be able to centre it on the main character rather than having to chase around with mouse controls so the final version of this program uses everything we have already but moves the ViewingPlatform with the Little Purple Dude. To do this, we hand our behaviour the TransformGroup that results from a ViewingPlatform.getViewPlatformTransform() and allow it to move that around. There are two new methods, first up is used to initialize a couple of constants about the view position:
private void initCamera()camTrans and camVec are both Object level variables- I could create CamTrans in the method that moves the camera but as I have created this one already, there is no harm in adding it here. CamVec is used to describe the initial relationship between the Camera and the origin of the scene, which is taken to also be the location of our character. This method is called from the behaviour initialisation section.
On the same frame as our movement we then call another new method.
private void translateCamera(Transform3D ourTrans)I expect you can work most of this out for yourself by now, and you should also be able to spot that I am lamely creating a vector called “up”every time the method is called. This method is being called from the moveStep method after it has transformed the objects, being handed the ourTrans Transform3D. Currently it does not rotate to follow the character, instead staying above and in the –z direction giving an isometric point of view. It would probably be best to integrate it more closely with the controls and movement but that would make it a lot harder to show all the code in one place, so I have done it this way instead.
There are almost as many changes to be found in the FollowcamDude applet that accompanies this one than there are in the behaviour:
private ViewingPlatform ourView;It’s all simple stuff –we now have ourView as an object level variable and we do all the initialisation before we create the scenegraph. Otherwise the starting position that the behaviour records will be null and the whole thing will crash the moment you move. Secondly we have taken out the OrbitBehaviour. This is because it sets it’s starting point as 0,0,0 and if we move the mouse we get dropped straight back to the origin from our moving character, which rather spoils the point. A useful extension to think about would be centring the OrbitBehaviour on the Purple Dude.
And that is all it takes to create a walking terrain-following Purple Dude who is followed by his own camera. It may sound easy, but try creating a tutorial on it and you’ll see just how hard it is.
Previous: Up Hill and Down Dale | Back to the Index | Next: Where Now?