Exercise 2: Rotating Around the Wrong Pivot for an In-Place Spin — Possible Solution ==================================================================== COMPUTING THE INCORRECT RESULT ------------------------------ The character is at P=(10,0) and needs to rotate 180 degrees. If the code applies the rotation matrix directly to P without first translating relative to the character's own position (that is, incorrectly using the world origin as the pivot instead of the character's own position), the computation is simply R(180) applied directly to P: R(180) = [[cos(180), -sin(180)], [sin(180), cos(180)]] = [[-1, 0], [0, -1]] (cos(180)=-1, sin(180)=0) P' = R(180) * (10,0) = (-1*10 + 0*0, 0*10 + -1*0) = (-10, 0) (computed in floating point, the y-component comes out as an extremely tiny nonzero value, approximately 1.22*10^-15, rather than exactly 0 - the same kind of floating-point approximation of pi verified back in Chapter 2, since sin(180 degrees) isn't stored as exactly zero.) WHAT THE PLAYER WOULD ACTUALLY OBSERVE ------------------------------ Instead of the character staying in place at (10,0) and simply turning to face the opposite direction (the correct, intended behavior for "spin in place"), the character would instantly teleport from position (10,0) to position (-10,0) - the mirror-image point reflected through the world origin. If (10,0) was, say, 10 units to the right of the world's center, the character would suddenly jump to 10 units to the LEFT of the world's center instead. The player would see the character vanish from one spot and reappear far away on the opposite side of the world, rather than seeing it turn around where it stood. WHY THE ERROR SCALES WITH DISTANCE FROM THE ORIGIN ------------------------------ This particular bug's severity depends entirely on how far the character is from the world origin - a character standing very close to (0,0) would barely appear to move at all when this bug occurs, while a character far from the origin (as in this exercise) would jump dramatically. This is exactly why the bug can be easy to miss in early testing (often done near a scene's origin) and only become obvious once objects are tested further from the origin - a genuine, realistic reason this class of bug is easy to overlook initially. WHY THIS WORKS AS AN ANSWER ------------------------------ The answer computes the actual incorrect resulting position using this chapter's own rotation-matrix formula, translates that numeric result into the concrete, observable symptom (a sudden teleport to the mirrored position rather than an in-place turn), and identifies why the bug's visibility depends on the object's distance from the origin - explaining why it's a realistic, easy-to-miss bug rather than an obviously broken one.