Jump to content

Convert degrees to vector


TonyL

Recommended Posts

Hope this isn't a newbie question but I didn't see anything about it when I searched. I have a camera rotated in space and I need to convert the direction the camera is facing into a vector. Does anyone know how I would do this?

Thanks

Link to comment
Share on other sites

Hope this isn't a newbie question but I didn't see anything about it when I searched. I have a camera rotated in space and I need to convert the direction the camera is facing into a vector. Does anyone know how I would do this?

Thanks

Hi,TonyL,Maybe the easiest trick is by using the "Look at" of your camera,that is,let the camera face the null object,then subtract the position of your camera from the null,the result is the facing vector.

more rigorously,the facing vector=R(z)R(y)R(x)[0 0 -1]T

R(z),R(y),R(x) is the roation matrix about the z,y,x respectively,[0 0 -1]T is the camera original facing direction. but I would not like to get through such pain way :P

Link to comment
Share on other sites

Hi,TonyL,Maybe the easiest trick is by using the "Look at" of your camera,that is,let the camera face the null object,then subtract the position of your camera from the null,the result is the facing vector.

Actually (I didn't explain this before), the null object in your example is essentially what I'm solving for. I want to have an object in the dead center of the camera's field of view and directly facing the camera. We'll get the distance from the camera from a user-controlled slider. The idea is if I can get the direction it's looking at in terms of a normalized vector, then I can use the distance and position of the camera to determine that object's position in space. This centering is just for initial (and final) placement, after that the camera will orbit the object (right now a grid of cubes) and the cubes will reconfigure to again face the camera and be in the middle of the shot.

more rigorously,the facing vector=R(z)R(y)R(x)[0 0 -1]T

R(z),R(y),R(x) is the roation matrix about the z,y,x respectively,[0 0 -1]T is the camera original facing direction. but I would not like to get through such pain way :P

I don't quite understand your equation, could you explain this a little more? How would this look as an Hscript or Python equation inside Houdini, for example?

Link to comment
Share on other sites

Actually (I didn't explain this before), the null object in your example is essentially what I'm solving for. I want to have an object in the dead center of the camera's field of view and directly facing the camera. We'll get the distance from the camera from a user-controlled slider. The idea is if I can get the direction it's looking at in terms of a normalized vector, then I can use the distance and position of the camera to determine that object's position in space. This centering is just for initial (and final) placement, after that the camera will orbit the object (right now a grid of cubes) and the cubes will reconfigure to again face the camera and be in the middle of the shot.

I don't quite understand your equation, could you explain this a little more? How would this look as an Hscript or Python equation inside Houdini, for example?

Hello, Tony! Please look the example file.

the main function is writen in 'Expressions' Tab of 'aliases and variables' menu

here is the code

vector nd(float x,float y, float z)
{
nx =-(cos(z)*sin(y)*sin(x) - sin(z)*cos(x));
ny =-(sin(z)*sin(y)*sin(x) + cos(z)*cos(x));
nz =-(cos(y)*sin(x));
return vector3(nx, ny, nz);
}

this the same what was said upper. Rotating 'xform1' SOP => setting the direction of 'line3' SOP

Hope this helps!

Edited by mic
  • Like 1
Link to comment
Share on other sites

Hello, Tony! Please look the example file.

the main function is writen in 'Expressions' Tab of 'aliases and variables' menu

here is the code

vector nd(float x,float y, float z)
{
nx =-(cos(z)*sin(y)*sin(x) - sin(z)*cos(x));
ny =-(sin(z)*sin(y)*sin(x) + cos(z)*cos(x));
nz =-(cos(y)*sin(x));
return vector3(nx, ny, nz);
}

this the same what was said upper. Rotating 'xform1' SOP => setting the direction of 'line3' SOP

Hope this helps!

forgot to attach the file...

rotations_to_vector.hipnc

Link to comment
Share on other sites

Thanks mic for your code, I was finally able to try it out in my project (my wife and I were putting on a gathering that took all my time the last few days). It's definitely closer, that's for sure. The equations for X and Z seem to be working, but Y isn't there yet, which you can check out in the attached file.

I'm using the degree to vector expression to help place objects in space at a specific point of the camera's field of view (right now it's dead center). I have a distance variable in the data node of gridobject1 which is affecting a sphere. When working correctly the sphere should be right in the middle of cam1's field of view, and would only appear to get bigger or smaller depending on the distance variable. As you can see, as you increase distance the sphere drops off the bottom of the screen.

Also, right now z rotation is zero, but if you change that you'll notice that X (and I think Z as well) will also be off.

box_transition_02.hipnc

Link to comment
Share on other sites

As much as I love doing all the math myself, I found it easier to just accomplish this using some linear algebra in a Python Object. In the attached scene I quickly wrote a simple object that just takes a pointed to camera and a distance, and transforms whatever is inside/connected to the object along the along the vector of the camera to the specified distance.

box_transition_hom.hipnc

Link to comment
Share on other sites

As much as I love doing all the math myself, I found it easier to just accomplish this using some linear algebra in a Python Object.

I must be missing something here. Why can't you just use a Fetch object to fetch the camera transform and then just put -ch("distance") into the sphere object's tz?

Link to comment
Share on other sites

  • 1 year later...

Hello, Tony! Please look the example file.

the main function is writen in 'Expressions' Tab of 'aliases and variables' menu

here is the code

vector nd(float x,float y, float z)
{
nx =-(cos(z)*sin(y)*sin(x) - sin(z)*cos(x));
ny =-(sin(z)*sin(y)*sin(x) + cos(z)*cos(x));
nz =-(cos(y)*sin(x));
return vector3(nx, ny, nz);
}

this the same what was said upper. Rotating 'xform1' SOP => setting the direction of 'line3' SOP

Hope this helps!

I know this is an old thread, but thanks for that example. Here it is in Python for anyone else in the future. If the code is in the hou.session module (Python Source Editor window) then it can be called on parameter expressions.

import math
import hou

def angleToVector(rx,ry,rz,length):
     vx = -(math.cos(rz) * math.sin(ry) * math.sin(rx) - math.sin(rz) * math.cos(rx))
     vy = -(math.sin(rz) * math.sin(ry) * math.sin(rx) + math.cos(rz) * math.cos(rx))
     vz = -(math.cos(ry) * math.sin(rx))
     return hou.Vector3(vx,vy,vz) * length

On a parameter expression it can be called like this for each component of a vector. In this case I used it for gravity in a DOP. The fourth argument is a length multiplier, so I wanted the force to be 400 in the example instead of the unit length vector.

# For X axis parameter
hou.session.angleToVector(0,0,-25,400)[0]

# For Y axis parameter
hou.session.angleToVector(0,0,-25,400)[1]

# For Z axis parameter
hou.session.angleToVector(0,0,-25,400)[2]

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...