# Camera rotation to direction vector

I need help on something I’m struggling with very much.

I am working on a TouchDesigner patch where I have a Camera COMP with rotation on x, y and z axes. I need the formulas to convert such rotations in a direction vector, that I am going to use in other programs to define the direction I am looking at. The point is having camera and for instance a microphone pointing in the same direction.

I have implemented the rotation matrices.

With this equations implemented I get the direction vector correctly but only when rotating on a single axe. But when I rotate on multiple axes it gets weird.

Example: the initial camera vector is {0,0,-1} with no rotation. So basically the camera is pointing in the negative-Z axes. With Y-rotate 45° the equation gives me {-0.707, 0, -0.707}, and it looks like the camera is actually pointing in that direction.

Now, if I add an X-rotate 90° the equation gives me {-0.707, -0.707, 0}, which is weird because the camera is now pointing in the negative Y, as if the vector was {0, -1, 0}.

You may want to use the Camera COMP’s worldTransform matrix to compute your view vector in world space.

1 Like

is it capable of giving me the direction vector as I need to?

Why don’t you try?

I’ve tried but the result doesn’t look correct.
I want to rotate the direction vector of the camera using the quaternions provided by the Camera Comp.

Assuming the initial vector to be [0, 0, -1] since the camera is pointing in the negative z axe, I apply the quaternion [0.9998, 0.0198, 0, 0] that comes from a 90° rotation on the X axe.

If I rotate the vector I get [0., 0.03959208, -0.999208] which is not what I was expecting, since the camera is now pointing in the positive Y axe.

What is going on?

ViewVectorExample.toe (4.7 KB)

Can you please be as kind as explaining it a little bit? How do I use the values in your example to rotate my vector?

The example shows how you can use the `worldTransform` member to apply a pre-transform to another `Geo COMP`, it also shows you how you can use the `Object CHOP` to transform `tx`, `ty`, and `tz` channels towards an object in the scene. In both cases, the transform was applied to a vector facing -Z so it will face the same direction as the camera.

So you don’t need to rotate your vector anymore, it’s already using the same transformation as the camera to face its view direction. It’s important to note that the `worldTransform` member of Object COMPs is dependable, meaning that all expressions referring to it will auto-cook when its value changes.

You have not explained how are you meaning to share this vector with the other applications, so I made two different examples, maybe I’m missing a python script, so here it goes:

The expression is:
`op('cam1').worldTransform * tdu.Vector(0, 0, -1)`

And it returns a tdu.Vector.

If I had to share this data in real-time I would probably use the CHOP solution and send via OSC.

Thank you all very much for the support. I’ll implement this solution and let you know in case something is still not clear.