Control of Orthographic camera position and orientation from Euler Angles

梦想的初衷 提交于 2020-03-06 04:05:44

问题


Attempting to control orthorgraphic camera position (x,y,z) and up vector (top of camera) from euler angles.

I have a real world device with an IMU that outputs rotation along each axis (x,y,z). I'm trying to translate these angles into camera eye position and up vector direction. If the physical device is altered, the change in orientation should be reflected in the change in position of the camera.

I'm having a hard time with the math behind this mapping, and have not had a ton of luck in what I've attempted. I can get single axis rotations to work but multiple axes has been a problem. Any help in mapping yaw,pitch and roll from physical device onto the position and up vector of camera would be amazing. The math is proving to be quite a problem for me.

The distance from camera to object and target point should remain constant.

radius = camera.target.distanceTo(camera.eye)
pos = get_data()
#convert to radians and cast to float
roll = float(pos['x'])*(math.pi/180.0)
pitch = float(pos['y'])*(math.pi/180.0)
yaw = float(pos['z'])*(math.pi/180.0)

x = camera.eye.x
y = camera.eye.y
z = camera.eye.z

x = x*math.cos(pitch)*math.sin(yaw)
y = y*math.cos(yaw)*math.cos(pitch)
z = z*math.sin(pitch)

eye = Point3D.create(x,y,z)

camera.eye = eye
camera.target = target      
camera.upVector = up

回答1:


If your Euler angles represent an absolute rotation, you can calculate a rotation matrix R. How you do this depends on the definition of the Euler angles, which should be described in the documentation. It might be:

R = rotateY(yaw) * rotateX(pitch) * rotateZ(roll)

Again, this is just an example. The correct formula depends on the order of Euler angles and their interpretation.

The camera's model matrix can then be calculated as:

cam = translation(target) * R * translation(0, 0, radius)

(or -radius for left-handed coordinate systems). translation() calculates a translation matrix.

The intuition behind this is that you move your camera to the target, then rotate it according to R, and then move it back by radius.

If you have this matrix, the camera's up-vector will be its second column, the eye-vector will be its fourth column. If you just need a view-matrix, use cam's inverse.

Note: The explanations above assume that vectors are treated as column vectors. If they are treated as row vectors (e.g. common in DirectX), the calculations have to be adapted accordingly.



来源:https://stackoverflow.com/questions/40182783/control-of-orthographic-camera-position-and-orientation-from-euler-angles

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!