• Aucun résultat trouvé

Camera Collision

Dans le document Game Programming Algorithms and Techniques (Page 196-200)

ComputeMatrix() end

end

Camera Support Algorithms

The implementations covered in the preceding section should provide enough information to get a camera that works on a basic level. But basic operation is only a small part of implement-ing a successful camera. In order to implement a camera that is useful for the player, additional support algorithms should be considered.

Camera Collision

Camera collision strives to solve a very common problem with many types of cameras: when there is an opaque object between the camera and the target. The simplest (though not the best) way to fix this is by performing a ray cast from the target position to the camera position.

If the ray cast collides with an object, the camera can be moved to a position that’s in front of

ptg11524036 the object that’s blocking the camera, as shown in Figure 8.12 . Unfortunately, this behavior

can be jarring; a more robust solution is to make a physics object representing the camera, but that’s beyond the scope of this section.

(a) (b)

Figure 8.12 Camera blocked by an object (a), and camera moved up so it’s no longer blocked (b).

Another consideration is what should be done when the camera becomes too close to the target object. Recall that the near plane of the camera is a little bit in front of the eye, which means a close camera could cause part of the target object to disappear. A popular solution to this problem is to hide or alpha out the target if the camera gets too close.

The alpha solution sometimes is also applied to the general problem of the camera being blocked by an object. So rather than moving the camera forward, the game may just alpha out the object that’s blocking it. Several third-person action games utilize this method.

Picking

Picking is the capability to click or tap to select an object in the 3D world. Picking is com-monly used in RTS games, such as the tower defense game in Chapter 14 , “Sample Game:

Tower Defense for PC/Mac.” In that particular game, picking is implemented in order to enable selection of the various towers (and locations where towers can be placed) in the game world. Although picking is not strictly a camera algorithm, it does tie in with the camera and projection.

Recall that to transform a given point in world space into projection (or screen) space, it must be multiplied by the camera matrix followed by the projection matrix. However, the position of the mouse (or touch) is expressed as a 2D point in screen space. What we need to do is take that 2D point that’s in screen space and transform it back into world space, which is known as an unprojection . In order to perform an unprojection, we need a matrix that can do the opposite transformation. For a row-major representation, this means we need the inverse of the camera matrix multiplied by the projection matrix:

unprojection=

(

camera projection×

)

1

But a 2D point can’t be multiplied by a 4×4 matrix, so before the point can be multiplied by this matrix, it must be converted to homogenous coordinates. This requires z- and w-components

ptg11524036 CAMERA SUPPORT ALGORITHMS 177

to be added to the 2D point. The z-component is usually set to either 0 or 1, depending on whether the 2D point should be converted to a point on the near plane or the far plane, respec-tively. And since it’s a point, the w-component should always be 1. The following Unproject function takes a 4D vector, because it assumes that it already has its z- and w-components set appropriately:

function Unproject( Vector4 screenPoint , Matrix camera , Matrix projection ) // Compute inverse of camera * projection

Matrix unprojection = camera * projection unprojection .Invert()

return Transform( screenPoint , unprojection ) end

The Unproject function can then be used to calculate two points: the mouse position unpro-jected onto the near plane ( z=0 ) and the mouse position unprojected onto the far plane (z=1). These two points in world space can then be used as the start and end points of a ray cast that tests against all of the selectable objects in the world. Because there can potentially be multiple objects the ray cast intersects with, the game should select the closest one. Figure 8.13 shows the basic premise behind picking.

z = 0

z = 1

far clipping plane near clipping plane

p1

p0

1

Figure 8.13 Picking casts a ray from the near to far plane and selects an object that intersects with the line segment.

ptg11524036

Summary

The camera is a core component of any 3D game. You potentially might use lots of different types of cameras, and this chapter covered implementations of many of them. A first-person camera gives an immersive view from the eye of a character. A follow camera might be used to chase behind a car or a player. An orbiting camera rotates around a specific target, and a spline camera might be used for cutscenes. Finally, many games might need to implement picking in order to allow the player to click on and select objects.

Review Questions

1. What does field of view represent? What issues can arise out of a narrow field of view?

2. How can you set up a basic camera that follows a target at a set horizontal and vertical distance?

3. In what way does a spring follow camera improve upon a basic follow camera?

4. When implementing an orbit camera, how should the camera’s position be stored?

5. How does a first-person camera keep track of the target position?

6. What is a Catmull-Rom spline?

7. What is a spline camera useful for?

8. A follow camera is consistently being blocked by an object between the camera and the target. What are two different ways this can be solved?

9. In an unprojection, what do z-components of 0 and 1 correspond to?

10. How can unprojection be used to implement picking?

Additional References

Haigh-Hutchinson, Mark. Real-Time Cameras . Burlington: Morgan Kaufmann, 2009 . This book provides a comprehensive look at many different types of game cameras, and was written by the creator of the excellent camera systems in Metroid Prime .

ptg11524036

C H A P T E R 9

Dans le document Game Programming Algorithms and Techniques (Page 196-200)