Collision detection and response in the assignment Marek TrtΓ­k PA199 2  Position of the ball: 𝐩 = 𝐩 π‘₯, 𝐩 𝑦, π‘Ÿ ⊀ , where π‘Ÿ is the radius of the sphere.  If 𝐩 βˆ’ π‘Ÿ > π‘Ÿπ‘” then GAME OVER.  If 𝐩 + π‘Ÿ β‰₯ π‘Ÿπ‘ βˆ’ 𝑀 𝑝 ∧ 𝐩 βˆ’ π‘Ÿ ≀ π‘Ÿπ‘ + 𝑀 𝑝 then β€œbroad phase with paddles”.  If 𝐩 + π‘Ÿ β‰₯ π‘Ÿπ‘ βˆ’ 𝑀 𝑏 ∧ 𝐩 βˆ’ π‘Ÿ ≀ π‘Ÿπ‘ + 𝑀 𝑏 then β€œbroad phase with bricks”.  Otherwise, no collision. Collision detection: Broad phase π‘Ÿπ‘ π‘Ÿπ‘ π‘Ÿπ‘” 𝑂 πœƒ π‘πœƒ 𝐩 𝑀 𝑝 𝑀 𝑏 πœ‘ 𝑝 3 Collision detection: Broad phase π‘Ÿπ‘ π‘Ÿπ‘ π‘Ÿπ‘” 𝑂 πœƒ π‘πœƒ 𝐩 𝑀 𝑝 𝑀 𝑏 πœ‘ 𝑝 # Colliding with Paddles (brick wall case is similar) def broad_phase(positions,𝑀 𝑝,πœ‘ 𝑝): π‘Ÿπ‘, πœƒ 𝑝= positions[0] for π‘Ÿπ‘ β€², πœƒ 𝑝 β€² in positions[1:]: if min_difference(πœƒ, πœƒ 𝑝 β€² ) < min_ difference(πœƒ, πœƒ 𝑝): π‘Ÿπ‘, πœƒ 𝑝= π‘Ÿπ‘ β€², πœƒ 𝑝 β€² if min_difference(πœƒ, πœƒ 𝑝) ≀ πœ‘ 𝑝: return narrow_phase_case_1(𝐩, π‘Ÿπ‘) else return narrow_phase_case_2(𝐩,π‘Ÿ,πœƒ,π‘Ÿπ‘,πœƒ 𝑝,𝑀 𝑝,πœ‘ 𝑝) 4  Case 1: β€œmin_difference(πœƒ, πœƒ 𝑝) ≀ πœ‘ 𝑝”. def narrow_phase_case_1(𝐩, π‘Ÿπ‘): 𝐧 = 𝐩 / |𝐩| return -𝐧 if |𝐩| < π‘Ÿπ‘ else 𝐧 Collision detection: Narrow phase 𝑂 πœƒ x y 𝐩 πœƒ 𝑝 πœ‘ 𝑝 βˆ’ 𝐩 |𝐩| = 𝐧 𝐩 𝐩 |𝐩| = 𝐧 π‘Ÿπ‘ 5  Case 2: β€œmin_difference(πœƒ, πœƒ 𝑝) > πœ‘ 𝑝”. def narrow_phase_case_2(𝐩,π‘Ÿ,πœƒ,π‘Ÿπ‘,πœƒ 𝑝,𝑀 𝑝,πœ‘ 𝑝): sign = 1 if on_left(πœƒ, πœƒ 𝑝 + πœ‘ 𝑝) else -1 𝑨 = to_cartesian(π‘Ÿπ‘-𝑀 𝑝, πœƒ 𝑝 + sign*πœ‘ 𝑝) 𝑩 = to_cartesian(π‘Ÿπ‘+𝑀 𝑝, πœƒ 𝑝 + sign*πœ‘ 𝑝) ෝ𝒒 = closest_point_on_line(𝑨𝑩, 𝐩) return 𝐩 βˆ’ ෝ𝒒 / |𝐩 βˆ’ ෝ𝒒| if 𝐩 βˆ’ ෝ𝒒 ∈ (0, Ϋ§π‘Ÿ else None Collision detection: Narrow phase π‘Ÿπ‘ 𝑂 πœƒ x y πœƒ 𝑝 πœ‘ 𝑝 𝐩 𝐩 βˆ’ ෝ𝒒 𝐩 βˆ’ ෝ𝒒 = 𝐧 ෝ𝒒 𝑩 𝑀 𝑝 𝑨 6 𝐧 𝐯 𝑝  Ball’s velocity: 𝐯 = 𝑣 π‘₯, 0, 𝑣𝑧 ⊀, 𝐯 = 𝑣0, where 𝑣0 is the fixed speed.  We have the unit collision normal 𝐧 = 𝑛 π‘₯, 𝑛 𝑦, 0 , 𝐧 = 1 from the collision detection.  Velocity of a paddle is 𝐯 𝑝. Collision response 𝐯 𝐩 7 Collision response 𝐯 𝑝 𝐯 𝐧 We can model the situation as 𝐯 𝐩 𝐧 𝐯 𝑝 Compute the relative velocity 8 Collision response βˆ’π― 𝑝 𝐯 𝐧 Δ𝐯 𝐧 Δ𝐯 Δ𝐯𝑑 Δ𝐯 𝑛 Decompose Δ𝐯 IMPORTANT Continue only if Δ𝐯 β‹… 𝐧 < 0 Δ𝐯 𝑛 = 𝐧 β‹… Δ𝐯 𝐧 Δ𝐯𝑑 = Δ𝐯 βˆ’ Δ𝐯 𝑛 9 Δ𝐯𝑑 ′ β€œBounce of the paddle” velocity change: Δ𝐯 𝑛 β€² = βˆ’2Δ𝐯 𝑛  β€œMatch paddle’s velocity” velocity change: Δ𝐯𝑑 β€² = βˆ’πœ‡ 𝑝 min Δ𝐯 𝑛 , Δ𝐯𝑑 Δ𝐯𝑑 |Δ𝐯𝑑| , if Δ𝐯𝑑 > 0, where 0 ≀ πœ‡ 𝑝 ≀ 1 is a β€œfriction” coefficient.  So, the collision response velocity is: π―π‘Ÿπ‘’π‘  = 𝐯 + Δ𝐯 𝑛 β€² + Δ𝐯𝑑 β€²  The final velocity is then: 𝐯 ≔ 𝑣0 𝐯 π‘Ÿπ‘’π‘  𝐯 π‘Ÿπ‘’π‘  , NOTE: π―π‘Ÿπ‘’π‘  > 0. Collision response Δ𝐯 𝑛 β€²π―π‘Ÿπ‘’π‘  Δ𝐯𝑑 Δ𝐯 𝑛 𝐯 10  Polar coordinates:  Always normalize the angles to the range ‫ۦ‬0, 2πœ‹) before comparison.  Consider using normalization directly in:  Conversion from the Cartesian to polar coordinates.  Operators for addition and subtraction of angles.  Alternatively, in comparison operators.  Otherwise, assert angles are normalized before comparisons.  When implementing angle comparison algorithm, keep in mind the case of passing the polar axis (in CW or CCW direction). Implementation notes 11  Recommendations:  Build tests and test scenes for collision detection and response algorithms. => Do not build the complete scene of the game (all paddles all wall bricks). => Test function β€œclosest_point_on_line” is different situations (configurations of line’s points and the reference point). => Test all phases of the collision detection in separate test scenes. => Test collison response in separate test scenes (for different velocities of the ball and the paddle). Implementation notes