Articles Archive
Articles Search
Director Wiki
 

Director 8.5: Following Models with the Camera

June 21, 2001
by Gary Rosenzweig

In the last three columns, I've shown you how to create different types of primitives and move them around. This week, I'll conclude the series by showing you how to view the action from different perspectives by having different cameras.

A camera is the spot where your eye is relative to the rest of a 3D world. The default camera for a 3D member is 250 units in front of the center of the world. Lets start by creating a second camera that is 200 units above the center of the world, looking straight down.

Most of the behavior that we are about to create is the same as the one from last week. There will be an additional call in the beginSprite handler to a new handler we'll write called createCameras. The exitFrame handler will use the left and right arrow keys to rotate the box model, instead of moving side-to-side.

property pScene, pBox, pPlane

on beginSprite me

  pScene = member ("Scene")

  -- reset the world when done
  pScene.resetWorld ()

  -- create box and plane
  createBox (me)
  createPlane (me)

  -- create cameras 2 and 3
  createCameras (me)

end

on exitFrame me

  -- move
  if keyPressed (125) then pBox.translate (0, 0, 1)
  if keyPressed (126) then pBox.translate (0, 0, -1)

  -- rotate
  if keyPressed (123) then pBox.rotate (0, 1, 0)
  if keyPressed (124) then pBox.rotate (0, -1, 0)

end

The createBox and createPlane handlers are exactly the same as last week, so I won't show them again here. Instead, let's jump right to the createCameras handler. The handler will create a new camera called "Camera 2". It will set the position property of the transform property to 200 units above the center of the world. The transform property of any model is a complex object that defines where the object is and what direction it is facing. In addition to setting the position of the camera, we will also set the rotation so that it is pointing down.

on createCameras me

 -- create top-down camera
  camera = pScene.newCamera ("Camera 2")
  -- move to 200 above
  camera.transform.position = vector (0, 200, 0)
  -- point down
  camera.transform.rotation = vector (-90, 0, 0)

The other camera we will create will start off at the same position as the box, but 50 units behind it.

-- create follow-the-box camera
  camera = pScene.newCamera ("Camera 3")
  -- move to 50 behind box
  camera.transform.position = pBox.transform.position + vector (0, 0, 50)

Now, here's the tricky part. We want this camera to follow the box around. To do that, you need to make the camera a child of the box. This simply means that the box and camera are linked and the box is in control of them both. Once this is done, the camera will remain 50 units behind the box at all times, no matter where the box moves to or what direction it is facing.

-- lock camera relative to box
  pBox.addChild (camera)
end

The example movie has three buttons at the bottom. Pressing any one of these will call gotoCamera which sets the camera property of the sprite. A 3D sprite's camera property defines which camera viewpoint is shown.

on gotoCamera me, n

  sprite (me.spriteNum).camera = pScene.camera[n]

end

Here is the finished product. You'll need Shockwave 8.5 to see it in your browser. Try moving the box around with the arrow keys. Then, switch to camera 2 to see it from above. Finally, switch to camera 3 to get the view from behind the box.

The source Director 8.5 movie is available for download in Mac or Windows format.

Gary Rosenzweig is the author of six books on Macromedia Director and Flash, including his latest, Flash 5 ActionScript for Fun & Games. He also publishes an email newsletter for Flash and Director developers called the Developer Dispatch. You can subscribe to this newsletter at http://developerdispatch.com.

Gary Rosenzweig is the Chief Engineer, founder, and owner of CleverMedia, a game and multimedia development company in Denver, Colorado. He is the author of ten books on Macromedia Director and Flash, including his latest, Special Edition Using Director MX.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.