Presentation is loading. Please wait.

Presentation is loading. Please wait.

Game Programming Algorithms and Techniques

Similar presentations


Presentation on theme: "Game Programming Algorithms and Techniques"— Presentation transcript:

1 Game Programming Algorithms and Techniques
Chapter 8 Cameras

2 Chapter 8 Objectives Types of Cameras
Learn about all the different types of cameras used in games, including fixed, first-person, and follow. Perspective Projection How FOV affects the visible scene Common aspect ratios Camera Implementations Basic and spring follow cameras Orbit camera First-person camera Spline camera Camera Support Algorithms Camera collision and picking

3 Fixed Camera Popular in older survival horror games such as Resident Evil Top-down view of a sample scene, and where fixed cameras might be placed

4 First-person camera in Quadrilateral Cowboy
Gives an immersive first-person perspective First-person camera in Quadrilateral Cowboy

5 Follow Camera Follows behind a target object, such as a car:
A follow camera behind a car

6 Field of View Angle that represents the amount of the world that can be seen. Binocular field of view of humans is approximately 120°. When determining the field of view for a game, we must take into account how close the player is to the screen.

7 Viewing Angle The viewing angle is the angle that is occupied by the screen: 50” HDTV with a 40° viewing angle (a), and PC monitor with a 90° viewing angle (b)

8 Field of View, Cont'd We want an in-game field of view that's greater than or equal to the view angle. A too-narrow field of view can cause motion sickness!

9 Field of View Comparison
Sample scene shown with a 65° field of view (a) and a 90° field of view (b) ©York/fotolia

10 16:9 has a wider horizontal FOV (a) and a narrower vertical FOV (b)
Aspect Ratio Ratio between width and height of screen. Two ways we can resolve differences between aspect: ratios: 16:9 has a wider horizontal FOV (a) and a narrower vertical FOV (b) ©York/fotolia

11 Basic follow camera tracking a car
Follows right behind the target Basic follow camera tracking a car

12 Basic Follow Camera, Cont'd
// tPos, tUp, tForward = Position, up, and forward vector of target // hDist = horizontal follow distance // vDist = vertical follow distance function BasicFollowCamera(Vector3 tPos, Vector3 tUp, Vector3 tForward, float hDist, float vDist) // Eye is offset from the target position Vector3 eye = tPos – tForward * hDist + tUp * vDist // Camera forward is FROM eye TO target Vector3 cameraForward = tPos - eye cameraForward.Normalize() // Cross products to calculate camera left, then camera up Vector3 cameraLeft = CrossProduct(tUp, cameraForward) cameraLeft.Normalize() Vector3 cameraUp = CrossProduct(cameraForward, cameraLeft) cameraUp.Normalize() // CreateLookAt parameters are eye, target, and up return CreateLookAt(eye, tPos, cameraUp) end

13 Basic Follow Camera Problems
It's very rigid. No real sense of turning. Difficult to tell whether object or world is moving.

14 A spring connects the camera’s ideal and actual positions.
Spring Follow Camera Set up a spring between the camera's ideal and actual positions: A spring connects the camera’s ideal and actual positions.

15 Spring Follow Camera: Update
// First calculate the ideal position Vector3 idealPosition = target.position – target.forward * hDist + target.up * vDist // Calculate the vector FROM ideal TO actual Vector3 displacement = actualPosition – idealPosition // Compute the acceleration of the spring, and then integrate Vector3 springAccel = (-springConstant * displacement) – (dampConstant * velocity) velocity += springAccel * deltaTime actualPosition += velocity * deltaTime

16 Orbit Camera Orbits around a particular object: Orbit camera

17 Orbit Camera, Cont'd Easiest system is to store the offset from the camera. If we rotate this offset, the camera will then orbit around the object. Typically allow yaw/pitch rotation.

18 Orbit Camera: Update // Updates based on the incremental yaw/pitch angles for this frame function Update(float yaw, float pitch) // Create a quaternion that rotates about the world up Quaternion quatYaw = CreateFromAxisAngle(Vector3(0,1,0), yaw) // Transform the camera offset and up by this quaternion offset = Transform(offset, quatYaw) up = Transform(up, quatYaw) // The forward is target.position - (target.position + offset) // Which is just -offset Vector3 forward = -offset forward.Normalize() Vector3 left = CrossProduct(up, forward) left.Normalize() // Create quaternion that rotates about camera left Quaternion quatPitch = CreateFromAxisAngle(left, pitch) // Transform camera offset and up by this quaternion offset = Transform(offset, quatPitch) up = Transform(up, quatPitch) // Now compute the matrix cameraMatrix = CreateLookAt(target.position + offset, target.position, up) end

19 First-person camera implementation
Orbit the target offset instead: First-person camera implementation

20 Catmull-Rom spline with the minimum four points
Simple type of spline where we interpolate between two control points: The path can have more than four control points, and it'll work so long as there's one point before and one after Catmull-Rom spline with the minimum four points

21 Catmull-Rom Spline Code
class CRSpline // Vector (dynamic array) of Vector3s Vector controlPoints // First parameter is the control point that corresponds to t=0 // Second parameter is the t value function Compute(int start, float t) // Check that start – 1, start, start + 1, and start + 2 // all exist ... Vector3 P1 = controlPoints[start – 1] Vector3 P2 = controlPoints[start] Vector3 P3 = controlPoints[start + 1] Vector3 P4 = controlPoints[start + 2] // Use Catmull-Rom equation to compute position Vector3 position = 0.5*((2*P1)+(-P0+P2)*t+ (2*P0-5*P1+4*P2-P3)*t*t+ (-P0+3*P1-3*P2+P3)*t*t*t) return position end

22 Spline Camera We can have a camera follow a spline path:
The camera position is at the current t value. The target point can be t + a small delta t. If the camera can't spin upside down, the up can always just be world up.

23 Camera Collision How to solve the problem where the camera is blocked by an opaque object? One solution is to move the camera up past the object: Camera blocked by an object (a), and camera moved up so it’s no longer blocked (b)

24 Picking Clicking on and selecting a 3D object:
Picking casts a ray from the near to far plane and selects an object that intersects with the line segment.

25 Picking, Cont'd To do this, we need to take a 2D point (cursor position) and convert it into a 3D world space line segment. This process is known as an unprojection. To perform an unprojection, we need a 4D point: (cursor.x, cursor.y, z, 1) Here, z = 0 means convert to a point on the near plane, and z = 1 means convert to a point on the far plane.

26 Unprojection Code function Unproject(Vector4 screenPoint, Matrix camera, Matrix projection) // Compute inverse of camera * projection Matrix unprojection = camera * projection unprojection.Invert() return Transform(screenPoint, unprojection) end


Download ppt "Game Programming Algorithms and Techniques"

Similar presentations


Ads by Google