Game Development Reference
In-Depth Information
1: {
2: var targetx, targety, dx, dy;
3:
4: targetx = obj_flynn.x;
5: targety = obj_flynn.y;
6:
7: targetx += obj_flynn.facing*256;
8:
9: if( point_distance( x, y, targetx, targety ) < 1.0 )
10: {
11: x = targetx;
12: y = targety;
13: }
14: else
15: {
16: dx = targetx - x;
17: dy = targety - y;
18:
19: x += dx/16;
20: y += dy/8;
21: }
22: }
Line 2 sets up a number of temporary variables, including a target position for the
focus point. This is initially set to Flynn's position in lines 4 and 5, and then offset by
256 pixels in the direction Flynn is facing in line 7. Note that this position is a target
position, not the actual position. A target position is used to make the actual position
of the view slide gracefully toward a new focus point.
Line 9 then checks to see if the current position of the focus object is less than 1
pixel away from its target position. If it is, then it's close enough to snap straight into
the actual target position, so that's what lines 11 and 12 do.
If it's further than 1 pixel away, then lines 16 and 17 work out exactly how far away
the target position is in the x and y axes. Lines 19 and 20 then add fractions of these
differences onto the current focus position. This means that the further the focus is
from its target, the faster it will move, and it will slow down as it comes closer to its
target (and would actually never get there were it not for the check in line 9).
Now give the new camera a try. You should notice that using the focus object provides a
better view of the level in front of Flynn. It also slides into position as you change direction and
removes that nasty jump in the view's movement when Flynn reaches the top of a platform.
However, it could still do with taking Archie's movement into account too, so we'll add that too.
Search WWH ::




Custom Search