This app started out as an experiment with animating a PerspectiveCamera in a WPF 3D scene. Basically, I wanted to achieve the effect of "flying" through a model. WPF's 3D animations are just too tempting to me though, and after achieving the camera animation I couldn't help but busy-up the app with rotating videos, a background Edgey track and some extra keyboard commands to have fun with. I haven't done anything useful in a WPF app yet :)
Anyway, here's the source code: MoveCamera.zip (8MB). The zip file is big because the media files are included.
And here's a screenshot:
The camera animation code is fairly easy to implement. The basic algorithm is to 1) calculate the next random camera position, 2) calculate the "look at direction" vector based on the new position and the desired look-at point (always near the origin of Cartesian space, 3) animate the position and look-at vector, and 4) handle the animation Completed event to repeat the process:
Point3D nextPosition = GetNextCameraPosition();
Vector3D nextLookDirection = GetNextLookDirection(nextPosition);
Point3D currentPosition = camera.Position;
Vector3D currentLookDirection = camera.LookDirection;
int duration = GetNextFlightDuration();
Vector3DAnimation lookAnimation = new Vector3DAnimation(
currentLookDirection, nextLookDirection,
TimeSpan.FromMilliseconds(duration));
Point3DAnimation positionAnimation =
new Point3DAnimation(currentPosition, nextPosition,
TimeSpan.FromMilliseconds(duration));
lookAnimation.Completed += new EventHandler(lookAnimation_Completed);
camera.BeginAnimation(
PerspectiveCamera.LookDirectionProperty, lookAnimation);
camera.BeginAnimation(
PerspectiveCamera.PositionProperty, positionAnimation);
tags: wpf 3d animation camera code programming