Skip to content

Commit

Permalink
fixed slerp so it always takes the shortest path.
Browse files Browse the repository at this point in the history
  • Loading branch information
pokepetter committed Dec 24, 2024
1 parent b809d60 commit efe03f4
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions ursina/ursinamath.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,25 @@ def lerp_angle(start_angle, end_angle, t):

def slerp(q1, q2, t):
costheta = q1.dot(q2)

# ensure shortest path by flipping q2 if dot product is negative
if costheta < 0.0:
q2 = -q2
costheta = -costheta
q1 = q1.conjugate()
elif costheta > 1.0:
costheta = 1.0
costheta = clamp(costheta, -1.0, 1.0)

costheta = clamp(costheta, -1.0, 1.0) # ensure valid range for acos

theta = acos(costheta)
if abs(theta) < 0.0001:
return q2

sintheta = sqrt(1.0 - costheta * costheta)
if abs(sintheta) < 0.0001:
return (q1+q2)*0.5
return (q1 + q2) * 0.5

r1 = sin((1.0 - t) * theta) / sintheta
r2 = sin(t * theta) / sintheta
return (q1*r1) + (q2*r2)
return (q1 * r1) + (q2 * r2)



Expand Down

0 comments on commit efe03f4

Please sign in to comment.