简体   繁体   中英

3d geometry: how to align an object to a vector

i have an object in 3d space that i want to align according to a vector. i already got the Y-rotation out by doing an atan2 on the x and z component of the vector. but i would also like to have an X-rotation to make the object look downwards or upwards.

imagine a plane that does it's pitch yaw roll, just without the roll.

i am using openGL to set the rotations so i will need an Y-angle and an X-angle.

I would not use Euler angles, but rather a Euler axis/angle. For that matter, this is what Opengl glRotate uses as input.

If all you want is to map a vector to another vector, there are an infinite number of rotations to do that. For the shortest one, (the one with the smallest angle of rotation), you can use the vector found by the cross product of your from and to unit vectors.

axis = from X to

from there, the angle of rotation can be found from from.to = cos(theta) (assuming unit vectors)

theta = arccos(from.to)

glRotate(axis, theta) will then transform from to to .

But as I said, this is only one of many rotations that can do the job. You need a full referencial to define better how you want the transform done.

You should use some form of quaternion interpolation ( Spherical Linear Interpolation ) to animate your object going from its current orientation to this new orientation.

If you store the orientations using Quaternions (vector space math), then you can get the shortest path between two orientations very easily. For a great article, please read Understanding Slerp, Then Not Using It .

If you use Euler angles, you will be subject to gimbal lock and some really weird edge cases.

Actually...take a look at this article. It describes Euler Angles which I believe is what you want here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM