Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Currently working on steering behavior for my AI and just hit a little mathematical bump.

I'm in the process of writing an align function, which basically tries to match the agent's orientation with a target orientation. I've got a good source material for implementing this behavior but it uses euler angles to calculate the rotational delta, acceleration, and so on. This is nice, however I store orientation as a quaternion and the math library I'm using doesn't provide any functionality for deriving the euler angles. But if it helps I also have rotational matrices at my disposal too.

What would be the best way to decompose the quaternion or rotational matrix to get the euler information?

I found one source for decomposing the matrix, but I'm not quite getting the correct results. I'm thinking it may be a difference of column/row ordering of my matrices but then again, math isn't my strong point. http://nghiaho.com/?page_id=846

share|improve this question
1  
Do you really need an Euler-angle decomposition, or do you need a way to implement your target-facing behavior directly in terms of quaternions? –  Nathan Reed Sep 3 '12 at 22:02
    
Well, if you have a source for a quaternion based implementation I'm all ears. Don't want to derail the original question though. –  KlashnikovKid Sep 3 '12 at 22:25
    
You could post a new question about it if you like. I would need to know more details of the desired behavior to be specific, but the general idea is just to lerp from the current quaternion toward the target quaternion. –  Nathan Reed Sep 3 '12 at 22:33
    
Ah yeah, interpolating would be an easy solution. However, I'm not directly moving the entities but updating acceleration to meet rotational velocity targets which is probably naturally easier with euler angles. –  KlashnikovKid Sep 4 '12 at 12:42

2 Answers 2

You should really be storing the component vectors (rotation, translation, scale, velocity, etc.) in addition to the matrix and quaternion forms. Not only does that eliminate the problem your having, it reduces compound numerical errors that come up over time from floating point limits.

share|improve this answer
    
Ended up just doing this for simplicity sake. Going to leave the question open a bit longer though since it doesn't quite answer the original question. –  KlashnikovKid Sep 3 '12 at 22:33
    
+1, not only numerical accuracy, but there's quite a frequent issue of having two quats (q and -q) representing the exact same orientation. Most libraries give randomly either of the two as a result from a matrix-to-quat and vice-versa conversion. So one more reason not to use plain quats.. also, use them only for special reasons (e.g. slerp, multiple slerp, etc.) –  teodron Sep 4 '12 at 8:32

Here's the code from pyeuclid:

class Quaternion:
    def get_euler(self):
        t = self.x * self.y + self.z * self.w
        if t > 0.4999:
            heading = 2 * math.atan2(self.x, self.w)
            attitude = math.pi / 2
            bank = 0
        elif t < -0.4999:
            heading = -2 * math.atan2(self.x, self.w)
            attitude = -math.pi / 2
            bank = 0
        else:
            sqx = self.x ** 2
            sqy = self.y ** 2
            sqz = self.z ** 2
            heading = math.atan2(2 * self.y * self.w - 2 * self.x * self.z,
                                 1 - 2 * sqy - 2 * sqz)
            attitude = math.asin(2 * t)
            bank = math.atan2(2 * self.x * self.w - 2 * self.y * self.z,
                              1 - 2 * sqx - 2 * sqz)
        return heading, attitude, bank
share|improve this answer
    
For some reason, I don't quite get the same angles back. I'm using the new DirectXMath library so maybe there is a difference in computing the quaternion from the yaw, pitch, and roll values. Anywho, ended up going with the other answer though since I can just keep the euler cached instead of recomputing it for AI queries. –  KlashnikovKid Sep 3 '12 at 22:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.