Archive
This is an archived tutorial about Flash. It's kept for history and may reference outdated tech.
TL;DR
Moving a shape and moving the camera turn out to be identical in Flash 3D. The camera is just an object with x, y, and z, and its position works opposite to real position: moving the camera 5 spaces toward a figure is like the figure moving 5 spaces toward you, so you offset everything by the camera.
- cameraView is a plain object with x, y, and z, plus values like focalLength, dir, and speed that relate to the camera.
- scaleRatio = focalLength/(focalLength+z) turns negative once z goes below -focalLength, which is something to watch for.
- The car example stores objects in an objectsInScene array and uses placeObjectIn3D to scale, position, and depth-sort each one.
- Each display function subtracts the camera's position from the object's position before placing it on screen.
This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.
Let's do a really quick example exemplifying this, contrasting shape movement and movement through the use of a camera. Here, we'll have a single figure, Joe, back in space along the z axis and then using figure movement and camera movement, contrast between the two.
[ offsetting with 'Joe' and the camera ]
There are two parts to this example. The first is with normal movement of the figure back and forth and the other is with the figure still and the camera moving back and forth. The figure movement is nothing new - the only difference there is the inclusion of a centered "theScene" movieclip to prevent the need for an offsetting origin object. The camera movement, however, is a bit different altogether. Here's how it's set up:
Steps to Create Animation
- We have the same setup here as the previous examples. Joe is our figure movieclip and he, like anything else in any 3D space will need his variables set. These are your normal x, y and z values so its pretty straight forward. Joe, however, is contained within the new theScene movieclip so he'll need to be referenced through that.
theScene.Joe.x = 0;
theScene.Joe.y = 0;
theScene.Joe.z = 500;
- Following Joe, the camera is set up. The camera is just an object much like a point object. It has its own x, y and z properties to determine its position only its position is the position of the view itself and not something in the view. Along with the x, y and z, the focalLength, dir and speed can also be defined here, each which relate to the camera.
cameraView = new Object();
cameraView.x = 0;
cameraView.y = 0;
cameraView.z = 500;
focalLength = 300;
dir = -1;
speed = 20;
- Finally, the definition of the onEnterFrame function. This will move the camera and position Joe in the screen based on that movement. What that movement really translates to is movement of Joe, or an offset for Joe. Now, the trick is that the camera position is opposite of real position. If, for example, the camera moves 5 spaces towards a figure in space or +5 spaces in the z axis, it would be analogous to the figure moving forward 5 spaces towards the camera view or -5 in z.
backAndForth = function(){
cameraView.z += speed*dir;
if (cameraView.z > 500){
cameraView.z = 500;
dir = -1;
}else if (cameraView.z < 0){
cameraView.z = 0;
dir = 1;
}
var scaleRatio = focalLength/(focalLength + this.z - cameraView.z);
this._x = (this.x - cameraView.x) * scaleRatio;
this._y = (this.y - cameraView.y) * scaleRatio;
this._xscale = this._yscale = 100 * scaleRatio;
};
theScene.Joe.onEnterFrame = backAndForth;
Conclusion? They are in fact identical. With this, you are able to have a camera, or your view, easily move in a 3D scene just by having it represent an offset for your other objects in your scene!
|
|
WARNING! Being Cautious of z Positioning Limits |
| You
should make an effort not to let
your z values go below 0, or at
least not too far below 0. Beyond
0, you have a cushion there of your
focal length value. After that,
think of what happens to the equation
that determines your scale ratio:
Once z is below -focalLength, your scaleRatio will be negative and actually start to go back up, at least in terms of absolute value. What you'll have then is flipped figures going back in the opposite direction. If, by chance, you have a situation where objects will need be positioned far back in z depth, or that's just the way they end up moving, you would then want to either stop calculations on those objects or just hide them all together so they don't pop in out of no where on the screen upside-down and moving backwards etc. |
|
Simple Car Racing With a Camera
Putting the camera to use in something practical isn't hard. It's just a matter of taking what was done previously and adding a concept to it. This implementation turns camera movement into the context of being a race car. This will show movement of the camera back and forth as well as side to side.
[ simple car racing with camera movement. arrow keys to move ]
Steps to Create Animation
- All imagery in this example aside from the ground will be dynamic and added at runtime rather than placed on the screen manually. This includes both the car images and the tires that define the track. Both the car movieclip and tire movieclip are created beforehand and given linker ID's in the library so they can be attached in code while the ground is just a square gradient box on the screen.

- Now we set up the scene movieclip for everything 3D (the cars and outlining tires) to exist in. Since everything in 3D is attached dynamically, this makes it easy to create the scene clip as an empty movieclip an just attach things within that. After making it, be sure to center it on the screen. Centered in this example is right at the top of the box representing the ground. The top of this box is also the horizon for the scene. Also added will be a depth variable to help in regulating depths for those attached clips since there are quite a few.
this.createEmptyMovieClip("theScene", 1);
theScene._x = 150;
theScene._y = 150;
theScene.depth = 1;
- In the first example, multiple clips were moved in 3D space and each were given their own onEnterFrame event for their movement. In the drawn box example, an array was created to contain all the points of the box that needed to be moved in the space. Here, again we are dealing with multiple clips, but instead of assigning them each their own separate onEnterFrame event, they can all be put into an array when created much like the points in the drawn box, thereby making it a little easier to work with. We'll call this array the objectsInScene array. Simply define that along with the rest of the setup variables.
objectsInScene = new Array();
- Next the camera is defined. This will act as the car being driven as it is from the car point of view you are seeing the scene. This will include the camera's position, velocity (as a car) and focal length.
cameraView = new Object();
cameraView.x = 0;
cameraView.y = -100;
cameraView.z = 0;
cameraView.velocity = 0;
focalLength = 300;
- Functions
will now have to be set up to display and control
how the cars and tires behave in the scene.
Both, however, will still follow the same concept
of offsetting by the camera's position and moving
and scaling based on those calculated values
as outlined in the following lines
thisx = this.x - cameraView.x;
this y = this.y - cameraView.y;
this z = this.z - cameraView.z;
this scaleRatio = focalLength/(focalLength + z);
this._x = x * scaleRatio;
this._y = y * scaleRatio;
this._xscale = this._yscale = 100 * scaleRatio;
this.swapDepths(Math.round(-z));
The second portion, starting with the scaleRatio, can be easily consolidated into a single function to avoid redundancy in the car and tire functions. Otherwise, each function will determine their x, y and z and react based on them. Cars, for example, move back in the z (increasing their z value) as to drive away from you. Also, when you pass them, they'll be repositioned ahead as to act as cars further down the track. Tires react similarly, reappearing in the distance to loop and be re-used again in the road definition.
placeObjectIn3D = function(obj, x, y, z){
var scaleRatio = focalLength/(focalLength + z);
obj._x = x * scaleRatio;
obj._y = y * scaleRatio;
obj._xscale = obj._yscale = 100 * scaleRatio;
obj.swapDepths(Math.round(-z));
};
// CARS
displayCar = function(){
var x = this.x - cameraView.x;
var y = this.y - cameraView.y;
var z = this.z - cameraView.z;
if (z < 4000){
if (z < 0){
this.z += 3000;
this.x = 200-Math.random()*400;
x = this.x - cameraView.x;
}
this.z += this.velocity;
z = this.z - cameraView.z;
}
placeObjectIn3D(this, x, y, z);
};
// TIRES
displayTire = function(){
var x = this.x - cameraView.x;
var y = this.y - cameraView.y;
var z = this.z - cameraView.z;
if (z < 0){
this.z += 3000;
z = this.z - cameraView.z;
}
placeObjectIn3D(this, x, y, z);
};
- With these methods defined, we can now physically add the cars and tires to the scene. For this, a for loop will be used to loop through and add the appropriate number of cars or tires needed. In this loop your basic x, y and z properties are assigned as well as a function called display which will be assigned to be either one of the displayCar or displayTire functions defined above depending on whether or not a car or tire is being attached. Its display function that will be called for the clip every frame for each object on the scene - kind of acting as each objects onEnterFrame. And they are of course accessed using the objectsInScene array so each attached clip will be added to that as well.
// CARS
for (i=0; i<3; i++){
attachedObj = theScene.attachMovie("car", "car"+i, theScene.depth++);
attachedObj.x = 200 - Math.random()*400;
attachedObj.y = 0;
attachedObj.z = 500+Math.random()*1000;
attachedObj.velocity = 20 + i*5;
attachedObj.display = displayCar;
objectsInScene.push(attachedObj);
}
// TIRES
for (i=0; i<20; i++){
attachedObj = theScene.attachMovie("tire", "tire"+i, theScene.depth++);
if (i < 10){
attachedObj.x = -250;
attachedObj.z = i*300;
}else{
attachedObj.x = 250;
attachedObj.z = 150 + (i-10)*300;
}
attachedObj.y = 0;
attachedObj.display = displayTire;
objectsInScene.push(attachedObj);
}
- Now its just a matter of setting up the function that will be used to run as the onEnterFrame. This will serve to do two things. One, it will move the camera based on pressing the arrow keys and secondly, it will loop through all the objects in the objectsInScene array and call each of their display methods which position them correctly on the screen.
drive = function(){
if (Key.isDown(Key.UP)) cameraView.velocity += 2;
else cameraView.velocity *= .95;
if (Key.isDown(Key.DOWN)) cameraView.velocity -= 2;
if (cameraView.velocity < 0) cameraView.velocity = 0;
else if (cameraView.velocity > 80) cameraView.velocity = 80;
cameraView.z += cameraView.velocity;
if (Key.isDown(Key.LEFT)) cameraView.x -= cameraView.velocity/5;
if (Key.isDown(Key.RIGHT)) cameraView.x += cameraView.velocity/5;
if (cameraView.x < -200) cameraView.x = -200;
else if (cameraView.x > 200) cameraView.x = 200;
for (var i = 0; i < objectsInScene.length; i++){
objectsInScene[i].display();
}
};
theScene.onEnterFrame = drive;
|
|
WARNING! Deleting From the objectsInScene Array |
| When
looping through the array of the
objects in the scene, you may come
across a situation where you will
need to remove an object from the
scene all together and therefore
from the objectsInScene array too.
The racing example loops and reuses
its objects so that wasn't necessary
in that example, but you can imagine
the need easily existing. Doing
this removal within the looping
process of the array when looping
through in an ascending order, however,
will cause the next object in the
array to be skipped since it will
shift into the position of the element
you've just removed. The looping,
though, will then proceed to the
next element in the array thinking
the current position, which would
then be what would have been the
next position, had already been
processed. To get around this problem, all you need to do is loop in an descending order starting with the last element in the array first. Then, if the current object needs removal from the array, when it is removed, the shifting will be from an element that had already been looped through. This is most easily done with a while loop in the following manner: var i = objectsInScene.length; Though using a for loop starting from the array length down to 0 will also suffice. |
|
Example the Last for Camera Movement and Space
We can take the car example a bit further and add in vertical movement along with the left and right. A simple addition of easing and you can get a nice effect of moving through space - here, being able to pull yourself through the distance ahead of you by clicking on a star.
[ pulling the camera through space; click on a star to go there ]
Before you go, here are a few things you can do:
-
Keep reading
Read the next article in the series, Trigonometry, or check out some related content:
-
Ask a question
Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.
That wraps up this tutorial. A huge thank you to all of you who buy kirupa's books, became a paid subscriber, watch the videos, and/or interact on the forums. Your support is what keeps writing like this online! 😇
This tutorial was written by senocular, also known as Trevor McCauley. He has been one of this community's most generous teachers since the early Flash days, and he is still around: find him on senocular.com and on the forums.
