Three.js - how do up vectors work with lookAt()? - vector

I'm trying to understand how up vectors and lookAt() work together in three.js. I'm setting the up vector of this axisHelper, so that the Y axis always points at the target geo, which marks the position of the up vector. It works as expected for X and Y, rotating the axes around the Z axis; and when I try to adjust the Z value of the up vector I would expect the axes to rotate around the X axis, but nothing happens.
http://jsfiddle.net/68p5r/4/
[Edit: I've added geo to show the up target position.]
I have a dat.gui interface manipulating the up vector to demonstrate, but the problem exists when I set the vector manually as well.
I suspect the problem is around line 74:
zControl.onChange(function(value) {
axes.up.set(this.object.x, this.object.y, value);
axes.lookAt(new THREE.Vector3(0, 0, 1));
});
When I update the up vector, I instruct the axisHelper to update its orientation onscreen by redoing its lookAt() down its Z axis. Changing the X and Y works as expected, why not the Z?
(This is also the case if I use geo instead of an axisHelper: http://jsfiddle.net/68p5r/5/)

When you call Object.lookAt( vector ), the object is rotated so that its internal z-axis points toward the target vector.
But that is not sufficient to specify the object's orientation, because the object itself can still be "spun" on its z-axis.
So the object is then "spun" so that its internal y-axis is in the plane of its internal z-axis and the up vector.
The target vector and the up vector are, together, sufficient to uniquely specify the object's orientation.
three.js r.63
Tip: An axis in three.js should always have unit length; be sure to call axis.normalize() in your code.

I assume your title meant rotate on Z instead of X?
Anyways, the culprit seems to be axes.lookAt(new THREE.Vector3(0, 0, 1)); if you change that to axes.lookAt(new THREE.Vector3(0, 1, 0)); for all methods then Y doesn't rotate as expected. You are telling the axis helper to look down a specific axis (in your case Z). Hence why Z value isn't working.
Is there an example of what your trying to accomplish that might help us?
Maybe someone else can give a bit more in depth explanation of what's happening. Hopefully my answer will push you in the right direction.

Here's how I came to understand the problem:
The lookAt and up vectors determine the orientation of an object like so:
The lookAt vector is applied FIRST, which sets the X and Y rotations, locking the direction the object's Z axis points.
THEN the up vector determines how the object rotates around the Z axis, to set the direction the object's Y axis points -- it won't affect the X and Y rotations at all.
In my example, the axisHelper is looking down its blue Z axis in the direction of the lookAt vector, which is a point in space at (0, 0, -1) -- so the X and Y rotations have already been set. The only thing left to do is figure out how to rotate the axisHelper around its Z axis, which means setting the X and Y points of the up vector -- moving the up vector forward and backward along the Z axis won't change anything.
Here's a fiddle with a demo illustrating this relationship: the blue arrow is the lookAt axis, and the green arrow is the up axis.
https://jsfiddle.net/hrjfgo4b/3
Links to jsfiddle.net must be accompanied by code

Related

How would I get the vector3 rotation needed to rotate towards vector3 coordinates

So the biggest issue with all the answers I've seen is that I cannot use quaternions. I need to rotate a camera to face a vector3 coordinate position but I can only use x, y, and z for the rotation. I've looked for awhile and can't really figure it out.
I have a raycast hitting a point, I use the point for the target coordinates I need the camera to face, using the cameras position I need to get a vector 3 rotation that I can set the camera to in order for the camera to be pointing directly at the coordinates
So the biggest issue with all the answers I've seen is that I cannot use quaternions.
This is plain wrong. If you can use Lua, you can use quaternions. Simply write your own quaternion implementation in pure Lua (or port an existing one).
I need to rotate a camera to face a vector3 coordinate position but I
can only use x, y, and z for the rotation. I've looked for awhile and
can't really figure it out.
An X, Y & Z rotation vector means you're using Euler angles (which still leaves multiple questions concerning orientation and order of rotation application open).
I have a raycast hitting a point, I use the point for the target coordinates I need the camera to face, using the cameras position I need to get a vector 3 rotation that I can set the camera to in order for the camera to be pointing directly at the coordinates
First you'll have to determine the direction the point is from the camera using the camera pos. You haven't specified which vector library you use, so I'll assume the following:
vector.new creates a new vector from a table;
+ and - on two vectors perform addition / subtraction;
the components can be accessed as .x, .y, .z
local direction = raycast_hit_pos - camera_pos
-- x/z-rotation
local function horizontal_rotation(direction)
local xz_dist = math.sqrt(direction.x^2 + direction.z^2)
return math.atan2(direction.y, xz_dist)
end
-- y-rotation
local function vertical_rotation(direction)
return -math.atan2(direction.x, direction.z)
end
-- gets rotation in radians for a z-facing object
function get_rotation(direction)
return vector.new{
x = horizontal_rotation(direction),
y = vertical_rotation(direction),
z = 0
}
end
Depending on orientation and the meaning of your rotation axes you might have to shuffle x, y and z around a bit, flipping some signs.

Rotate an object on its local axis using global rotation?

I want an object that rotates on a relative axis (the axis rotates with it) to rotate as if it's axis hadn't moved.
In a game (not my own) I have an object that stays in view of the player at all times (it follows the camera). I also want said object to keep the same rotation relative to the camera so it always looks like the same orientation to the player. It is simple to make it hold an orientation with modified X or Z values relative to the camera yaw, but once I rotate the object on the Y axis, the X axis has moved to the camera pitch makes the object rotate incorrectly (the roll would be affected too).
I am pretty sure it will require either a matrix or a combination of sin/cos, but I have had no luck in finding an answer.
Note: Rotation is in pitch, yaw, and roll using radians on the x, y, and z-axis respectively.

Angle, which way to rotate to

For a game i'm trying to calculate the angle between where i'm looking at and the position of another object in the scene. I got the angle by using the following code:
Vec3 out_sub;
Math.Subtract(pEnt->vOrigin, pLocalEnt->vOrigin, out_sub);
float angle = Math.DotProductAcos(out_sub, vec3LookAt);
This code does give me the angle between where im looking at and an object in the scene. But there's a small problem.
When i don't directly look at the object but slightly to the left of it, then it says i have to rotate 10 degrees in order to directly look at the object. Which is perfectly correct.
But, when i look slightly to the right of the object, it also says i have to rotate 10 degrees in order to look directly to the object.
The problem here is, the i have no way to tell which way to rotate to. I only know its 10 degrees. But do i have to rotate to the left or right? That's what i need to find out.
How can i figure that out?
I feel the need to elaborate on Ignacio's answer...
In general, your question is not well-founded, since "turn left" and "turn right" only have meaning after you decide which way is "up".
The cross product of two vectors is a vector that tells you which way is "up". That is, A x B is the "up" that you have to use if you want to turn left to get from A to B. (And the magnitude of the cross product tells you how far you have to turn, more or less...)
For 3D vectors, the cross product has a z component of x1 * y2 - y1 * x2. If the vectors themselves are 2D (i.e., have zero z components), then this is the only thing you have to compute to get the cross product; the x and y components of the cross product are zero. So in 2D, if this number is positive, then "up" is the positive z direction and you have to turn left. If this number is negative, then "up" is the negative z direction and you have to turn left while upside-down; i.e., turn right.
You also need to perform the cross product on the vectors. You can then get the direction of the rotate by the direction of the resultant vector.

3d rotation around the origin

I know there are plenty of questions about 3d rotation that have been answered here but all of them seem to deal with rotational matrices and quaternions in OpenGL (and I don't really care if I get gimbal lock). I need to get 3d coordinates EX:(x,y,z) of a point that always must be the same distance, I'll call it "d" for now, from the origin. The only information I have as input is the deltax and deltay of the mouse across the screen. So far here is what I have tried:
First:
thetaxz+=(omousex-mouseX)/( width );
thetaxy+=(omousey-mouseY)/( height);
(thetaxy is the angle in radians on the x,y axis and thetaxz on the x,z axis)
(I limit both angles so that if they are less than or equal to 0 they equal 2*PI)
Second:
pointX=cos(thetaxz)*d;
pointY=sin(thetaxy)*d;
(pointX is the point's x coordinate and pointY is the y)
Third:
if(thetaxz)<PI){
pointZ=sqrt(sq(d)-sq(eyeX/d)-sq(eyeY/d));
}else{
pointZ=-sqrt(abs(sq(d)-sq(eyeX/d)-sq(eyeY/d)));
}
(sq() is a function that squares and abs() is an absolute value function)
(pointZ should be the point's z coordinate and it is except at crossing between the positive z hemisphere and negative z hemisphere. As it approaches the edge the point gets stretched further than the distance that it is always supposed to be at in the x and y and seemingly randomly around 0.1-0.2 radians of thetaxz the z coordinate becomes NAN or undefined)
I have thought about this for awhile, and truthfully I'm having difficulty warping my head around the concept of quaternions and rotational matrices however if you can show me how to use them to generate actual coordinates I would be glad to learn. I would still prefer it if I could just use some trigonometry in a few axis. Thank you in advance for any help and if you need more information please just ask.
Hint/last minute idea: I think it may have something to do with the z position affecting the x and y positions back but I am not sure.
EDIT: I drew a diagram:
If you truly want any success in this, you're going to have to bite the bullet and learn about rotation matrices and / or quaternion rotations. There may be other ways to do what you want, but rotation matrices and quaternion rotations are used simply because they are widely understood and among the simplest means of expressing and applying rotations to vectors. Any other representation somebody can come up with will probably be a more complex reformulation of one or both of these. In fact it can be shown rotation is a linear transformation and so can be expressed as a matrix. Quaternion rotations are just a simplified means of rotating vectors in 3D, and therefore have equivalent matrix representations.
That said, it sounds like you're interested in grabbing an object in your scene with a mouse click and rotating in a natural sort of way. If that's the case, you should look at the ArcBall method (there are numerous examples you may want to look over). This still requires you know something of quaternions. You will also find that an at least minimal comprehension of the basic aspects of linear algebra will be helpful.
Update: Based on your diagram and the comments it contains, it looks like all you are really trying to do is to convert Spherical Coordinates to Cartesian Coordinates. As long as we agree on the the notation, that's easy. Let θ be the angle you're calling XY, that is, the angle between the X axis rotated about the Z axis; this is called the azimuth angle and will be in the range [0, 2π) radians or [0°, 360°). Let Φ be an angle between the XY plane and your vector; this is called the elevation angle and will be in the range [-π/2, +π/2] or [-90°, +90°] and it corresponds to the angle you're calling the XZ angle (rotation in the XZ plane about the Y axis). There are other conventions, so make sure you're consistent. Anyway, the conversion is simply:
x = d∙cos(Φ)∙cos(θ)
y = d∙cos(Φ)∙sin(θ)
z = d∙sin(Φ)

Axis Rotation Question

By using left hand rule, I rotate one object left and right using y axis, and rotate up/down using x axis.
After first object is rotated to the right, the up/down rotation should be using z axis.
However, when I try to rotate using z axis, after the first rotation, it has the same effect when I rotate using y axis.
Anyone has any ideas?
Thanks
The proper order of rotations in order to keep everything straight is roll, pitch, yaw. That is, rotation around the X axis, rotation around the Y axis, rotation around the Z axis.
Not sure what your question is, but if you're asking why this happens, the answer is that rotations are not commutative. That is, a rotation of theta about axis A followed by a rotation of phi around axis B is not the same as rotation of phi around axis B followed by a rotation of theta around axis A.
If you're asking why a sequence of operations that seems okay when you visualise it fails to work in code, be sure you're using a right-handed coordinate system. Also, it might be helpful to work through your various rotation matrices for the x, y and z axes using the unit vector (1,0,0) - in fact, if you do it on paper you'll get a better intuition for what's happening.
Thanks for all the answers:
Sorry I didn't state the problem clearly.
That's the typical gimbal lock problem.
and my solution is to use quaternion rotation

Resources