Three magic beans walked into a black box…

What in the world is a Quaternion Slerp? Sage question dear reader! And I, psuedo-lead game developer Martin, am here with the answer you’ve been dying to hear. The simple answer is that the Quaternion Slerp is a calculation which slerps between two quaternions. Not enough of a clue? Try Unity’s definition: according to the Unity Docs, a Quaternion Slerp “spherically interpolates between [quaternions] a and b by [float] t.” Getting frustrated? Now you know how I felt.

I wanted to script what, I thought, was a fairly simple interaction:

  1. Recognize when the player is a certain distance from my NPC
  2. At that distance, swing the player’s camera to look at said NPC
  3. Lock the camera, following the NPC until it stops talking

And, for extra credit, I was going to accomplish these three steps without relying on the Unity Asset Store. Off I scampered to my scripting, blissfully unaware of the headaches ahead.

Fortunately, tasks 1. and 3. proved easy enough. Unity has a game object called a collider which, spoiler alert, detects when something collides with something else. Increase the size of this invisible area beyond the visible NPC and you’re all set. Locking the camera turned out to be as simple as disabling the built-in script that “listens” for keyboard and mouse commands.

Code snippet to dazzle the readers…

So there I was, two thirds of the way to my goal, feeling pretty slick. A quick google search told me that all I needed for objective 2. was, you guessed it, a Quaternion Slerp. “Okay,” I thought, “I’ve run across ridiculous computer jargon before. I can handle this.” Googling intensifies…

“In mathematics, the quaternions are a number system that extends the complex numbers. They were first described by Irish mathematician William Rowan Hamilton in 1843 and applied to mechanics in three-dimensional space. A feature of quaternions is that multiplication of two quaternions is noncommutative.”

Uh huh… What about a “slerp”?

“In computer graphics, Slerp is shorthand for spherical linear interpolation, introduced by Ken Shoemake (that’s his name, I promise) in the context of quaternion interpolation for the purpose of animating 3D rotation. It refers to constant-speed motion along a unit-radius great circle arc, given the ends and an interpolation parameter between 0 and 1.”

Okay, better. “3D rotation” is exactly what I want to do! Now, how do I do it…?

Luckily, I’m a CS major, which basically means I’m working on a degree in trial and error. I still have no clue what a “quaternion” is and I’m pretty flimsy on the whole “spherical linear interpolation” thing, but that’s okay. I have my own definition of a Quaternion Slerp now:

Given the position of two 3D thingys and a speed thingy, rotate the first thingy to face the second thingy.

 

I’m fairly proud of my little script. All of the heavy lifting is handled by Unity’s Quaternion.slerp function, but that’s the one of the beauties of code: abstraction. I don’t need to know all the details. I just need to focus on feeding the function the right information and knowing what to do with the data it gives back. I if I ever manage to come up with some crazy function (which will have an even cooler and more pointless name), I will be able to make tools that the layman coder can use to take advantage of my work. Cool, right?

Leave a Reply

Your email address will not be published. Required fields are marked *