Difference between revisions of "User:Pedro Oval/Calculate rotation for pointing in a direction"
Pedro Oval (talk | contribs) m (moved User:Pedro Oval/Calculate rotation to point in a direction to User:Pedro Oval/Calculate rotation for pointing in a direction: Better name, avoids ambiguity between point as a verb and as a substantive.) |
Pedro Oval (talk | contribs) (Expand text, add authorship comment) |
||
Line 1: | Line 1: | ||
Here are two | Here are two functions that, given a vector to point at, return the rotation that corresponds to that vector. | ||
The first function, <code>PointAt2Rot</code>, makes the forward (X) vector point exactly in that direction, while keeping the up (Z) vector pointing "as up as possible". It is similar to what llLookAt does, except that the vector that points to the target is X (forward) instead of Z (up), and the result is a rotation, not a continuously updating state of pointing in the given direction. | |||
The second function, <code>PointAtHoriz2Rot</code>, points to the horizontal that is closest to the given vector, while the up vector points exactly up. It is useful if the final resulting rotation must be horizontal. | |||
Note that both functions have trouble handling the case where the target is directly above or below the source, and in that case the resulting rotation might be wrong. | |||
<lsl> | <lsl> | ||
// Written by Pedro Oval, 2011-01-08 | |||
rotation PointAt2Rot(vector target) | rotation PointAt2Rot(vector target) | ||
{ | { |
Revision as of 20:47, 7 January 2011
Here are two functions that, given a vector to point at, return the rotation that corresponds to that vector.
The first function, PointAt2Rot
, makes the forward (X) vector point exactly in that direction, while keeping the up (Z) vector pointing "as up as possible". It is similar to what llLookAt does, except that the vector that points to the target is X (forward) instead of Z (up), and the result is a rotation, not a continuously updating state of pointing in the given direction.
The second function, PointAtHoriz2Rot
, points to the horizontal that is closest to the given vector, while the up vector points exactly up. It is useful if the final resulting rotation must be horizontal.
Note that both functions have trouble handling the case where the target is directly above or below the source, and in that case the resulting rotation might be wrong.
<lsl> // Written by Pedro Oval, 2011-01-08
rotation PointAt2Rot(vector target) {
vector fwd = llVecNorm(target); vector left = llVecNorm(<0., 0., 1.> % fwd); vector up = fwd % left; return llAxes2Rot(fwd, left, up);
}
rotation PointAtHoriz2Rot(vector target) {
vector up = <0., 0., 1.>; vector left = llVecNorm(up % target); vector fwd = left % up; return llAxes2Rot(fwd, left, up);
} </lsl>