Game Development Reference
In-Depth Information
The first part of this code is to create a few convenience variables to hold the
current location of your spaceman as well as a couple of variables to control
the speed and distance in which the spaceman will move during a render
cycle.
Based on the touch count, you move him either forward the
moveDistance value or backward the moveDistance value. Feel free
to add some “Easter eggs” for your user to find, like that 11-touch gesture.
Moving the Camera
Now that you have your spaceman moving, you will need to move the camera with him to
keep him in the frame. Now you will create the missing method positionCamer-
aWithSpaceman , as in Listing 14-3 .
Listing 14-3 . positionCameraWithSpaceman( )
func positionCameraWithSpaceman() {
let spaceman = spaceManNode.presentationNode()
var spacemanPosition = spaceman.position
let cameraDamping:Float = 0.3
var targetPosition
= SCNVector3Make(spacemanPosition.x, 30.0,
spacemanPosition.z + 20.0)
var cameraPosition = cameraNode.position
var cameraXPos = cameraPosition.x * (1.0
- cameraDamping) + targetPosition.x * cameraDamping
var cameraYPos = cameraPosition.y * (1.0
- cameraDamping) + targetPosition.y * cameraDamping
var cameraZPos = cameraPosition.z * (1.0
- cameraDamping) + targetPosition.z * cameraDamping
cameraPosition = SCNVector3(x: cameraXPos, y:
cameraYPos, z: cameraZPos)
cameraNode.position = cameraPosition
spotLight.position = SCNVector3(x: spacemanPosition.x,
y: 90, z: spacemanPosition.z + 40.0)
spotLight.rotation = SCNVector4(x: 1, y: 0, z: 0, w:
Float(-M_PI/2.8))
Search WWH ::




Custom Search