Exercise 1: Facing Angle and Turn Direction for a New Sprite — Possible Solution ==================================================================== GIVEN ------------------------------ f = [0, 1] (facing "up") d = [-2, 2] (direction to the player) STEP 1: THE DOT PRODUCT AND ANGLE (STEP 1's TECHNIQUE) ------------------------------ f . d = (0)(-2) + (1)(2) = 0 + 2 = 2 |f| = sqrt(0^2 + 1^2) = 1 |d| = sqrt((-2)^2 + 2^2) = sqrt(4 + 4) = sqrt(8) ~= 2.828 cos(theta) = (f . d) / (|f| |d|) = 2 / (1 x 2.828) ~= 0.7071 theta = arccos(0.7071) = 45 degrees The player is 45 degrees off of directly ahead - well within a typical detection cone, similar in spirit to Step 1's own ~53.1 degree result. STEP 2: THE 2D CROSS PRODUCT AND TURN DIRECTION (STEP 2's TECHNIQUE) ------------------------------ f1 d2 - f2 d1 = (0)(2) - (1)(-2) = 0 + 2 = 2 The result is positive, meaning (per this chapter's own Step 2 convention) d is counter-clockwise from f - the player is to the sprite's LEFT. The AI should turn left. WHY THIS WORKS AS AN ANSWER ------------------------------ Both calculations reuse this chapter's own Step 1 and Step 2 techniques exactly - the dot product for the angle, the 2D cross product's sign for the turn direction - applied to the new f and d values rather than simply re-describing the original worked example.