# rotation sanity check

Like a few others on this forum, I’ve been tearing my hair out over 3d rotations. I’ve tried a thousand different things, and always end up with the wrong solution. I’ve tried it Hpr-style and Quat-style, and I’ve been reading everything I can find on the topic of rotation, but everything’s either too simple (“here’s how to rotate on one axis!”) or too complicated (–insert crazy math here–). I’m really at the end of my rope, and I figure that I must be working off a wrong assumption somewhere. Can anyone help?

Here’s a simple example. In space, I have the camera at (0,0,0). I have a point at (20,20,20). What I want is to have the camera rotate to face that point. Now, trig-wise, this is pretty basic. The point is 45 degrees off of every axis (xy, yz, xz). So I naturally started with the noob mistake of saying:
camera.setHpr(-45,45,0)
or
camera.setHpr(-45,45,45) or some combination like that. Obviously, the problem here is that Euler angles are dependent on the order in which they’re applied. I dig it.

On to quaternions. My foray into that didn’t yield much, either. After a week of research and toil, I produced an algorithm that will rotate to a point as long as that point is on one of the planes that the camera is on (e.g. (20, 20, 0), (20, 0, 20), and (0, 20, 20) all work flawlessly), but adding the third dimension throws it out of wack.

Finally, I decided to use NodePath.lookAt() the point, and printed out the HPR values. What I got was this:

VBase3(-45, 35.2644, 0)

So my fundamental question is this: how on earth do I get “35.2644?” What does that represent mathematically? I mean, If I’m going to set a rotation, I need to know what values to set. I have all the angles of everything to everything else (except, obviously the P angle). What am I missing??

Let’s put three points in space:

Point P is at (0,0,0)
Point Q is at (20,20,0)
Point R is at (20,20,20)

And let’s add some lines that form a triangle.

Line L is from P to Q.
Line M is from Q to R.
Line N is from R to P.

There’s a right angle at point Q, triangle PQR is a right triangle, but it’s not a 45-45-90 triangle, because line L and line M aren’t of equal length:

Line L is 20 * sqrt(2)
Line M is 20

In fact, one of the angles on this triangle is 35.2644 degrees. The correct pitch is, in fact, 35.2644 degrees.

So next, let me address the bigger question behind this post. I have this sort of problem all the time, because I’m terrible at math. It takes me forever to figure this sort of thing out.

So basically, I’ve learned a collection of tricks to make rotation easier. It will take you time to learn the tricks, but once you know them, you’re set.

First rule: you have four representations to choose from: Quat, Matrix, HPR, and Axis-Angle. Panda can convert freely. Use them all. Each one makes certain things easy. When I write rotation code, I use a lot of conversions - I always use the representation that’s easiest for the task at hand.

So tell me — what was the specific type of motion you were trying to achieve? I’d be happy to help you work out the math.

Thank you thank you thank you! It was so simple once you pointed it out, but I totally wouldn’t have see it. Sure enough, it was a blind spot in my early assumptions. Once I changed my algorithm to use the correct triangle for pitch, it worked like a charm.

As a bonus, it works with the quaternion code I had put together–which use setFromAxisAngle()–as well as the old Euler stuff I had abandoned. Now I can get back to work on my actual game.

So I, too, am doing a standard 3D space shooter. I managed to get the player ship controlled with a mouse and keyboard ok, but I ran into this problem getting the enemy ships to track an arbitrary target properly. Since I want to incorporate drift (imperfect aim), leading, flocking, etc…having control over where a given ship was pointed was pretty elemental.

Thanks again!

How come you don’t just let panda do the work, using ‘lookAt’?

You know…I honestly don’t remember! I know I had a reason, but somewhere along the line, I think solving this problem became something of a mini-obsession in itself. Well, I guess whether I use it or not, it did prompt me to get my head back into the math I’ll need for this project after a 10 year hiatus from it.