Angles from direction

I have a vector pointing from a source to a target.
How do i find the angles i need to rotate in order to point an arrow to the target ?
(the arrow has the origin in source and initially points to 0,0,-1 )

V1 - vector from source to target
V2 - vector along the arrow ( from the origin to 0,0,-1)

Calculate Unit vectors

If V1 = V2, then no rotation
if V1 = -V2 , then rotation angle = 90.


Calculate v3 ( v3 = v1 cross v2)
Calculate angle using the dot product for v1 and v2.

Rotate about v3 , using angle

I need to rotate around the X, Y and Z axis and not around an arbitrary axis, that is calculating the pitch ,yaw and roll

To make myself more clear, i am building a glu cone and want it to point in the direction of the vector, glu builds it around the current origin so after that i need to rotate around the X,Y and Z axis to make it point the way i want.

You can create a LookAt-matrix that will point the arrow at any position/direction. It works, but I don’t think this is what he wants. What he wants is similar to what I need: a PointAt() function.

Basically I have a camera that is defined with a Vector3 position, and 3 scalar euler angles for pitch, yaw, and roll. The camera works fine when controlling it manually.

What I would like to do is supply a Vector3 target position to the camera, and have the camera rotate from its current orientation to face towards the target position. Like I said, you could always create a LookAt-matrix to do this. The problem is, the LookAt-matrix is vague. What I need is to find the exact yaw/pitch/roll euler angles that will supply this rotation [or the exact yaw/pitch angles that will point the camera at the target]; given only the camera position, target position, and the camera’s current orientation.

So far, my closest attempt is using a ShortestArc quaternion function. But I’m definately doing something wrong. This is driving me crazy. I can’t believe I can’t find a solution. It seems so simple, yet so impossible.

Someone told me that atan() is what I need. Does anybody know if that sounds right?

This is simply a spherical coordinate system problem … hope this code helps …

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Convert an orientation angle set and range to a cartesian point coordinate.
// Parameters:
//   aziRad - Azimuth orientation angle (radians).
//   eleRad - Elevation orientation angle (radians).
//   range - Range from origin to point..
//   x - Receptor for the cartesian X coordinate
//   y - Receptor for the cartesian Y coordinate
//   z - Receptor for the cartesian Z coordinate

template<typename T>
void OrientationToCartesian(
		T aziRad,
		T eleRad,
		T range,
		T &x,
		T &y,
		T &z
	T cPhi = T(::cos(eleRad));
	x = T(range*cPhi*::cos(aziRad));
	y = T(range*cPhi*::sin(aziRad));
	z = T(range*::sin(eleRad));

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Convert a cartesian coordinates set to an orientation angle set. Return the
// magnitude of the input coordinate vector.  NOTE! The vector magnitude must be
// computed for this calculation. It is returned to the user simply as a
// performance convenience (the user may need the magnitude for some reason).
// Parameters:
//   x - Cartesian X coordinate
//   y - Cartesian Y coordinate
//   z - Cartesian Z coordinate
//   vMagn - Receptor for the vector magnitude.
//   aziRad - Receptor for the azimuth angle (radians).
//   eleRad - Receptor for the elevation angle (radians).

template<typename T>
void CartesianToOrientation(
		T x,
		T y,
		T z,
		T &vMagn,
		T &aziRad,
		T &eleRad
	vMagn = T(::sqrt(x*x+y*y+z*z));

	if (::fabs(x)<1.E-07 && ::fabs(y)<1.E-07
		aziRad = T(0.0);
	else if (::fabs(x)<1.E-07)
		aziRad = T(PiByTwo);
		if (y<0)
			aziRad *= T(-1.0);
		aziRad = T(::atan2(y,x));

	if (::fabs(z)>1.E-07)
		if (vMagn>1.E-07)
			eleRad =
				PiByTwo - ::acos(z/vMagn);
			eleRad = 0;
		eleRad = 0;

Example usage:

	Vector3D v(10,10,10);
	float vMagn, aziRad,eleRad;

	CartesianToOrientation(v.x,v.y,v.z, vMagn, aziRad,eleRad);
	cerr << "V: " << v << endl;
	cerr << "Vmag : " << vMagn << endl;
	cerr << "Az : " << aziRad*RadToDeg << endl;
	cerr << "El : " << eleRad*RadToDeg << endl;
	cerr << endl;

	cerr << "V' : " << v << endl;

bai cioclule iti sugerez sa arunci si tu un ochi printr-un manual de algebra liniara

protonovus I have been in your shoes for quite
sometime! I think we are looking for the same thing:
a “robust” way to make a stationary camera track a
moving object, for example an observer sitting in a
control tower in a flight simulation game…
The reason this is difficult is because excluding the
ambiguity that a single source to target vector has
with regard to the orientation, in the example I mentioned
it is difficult to simulate this motion of the camera.

I have searched quite a bit with Google for possible
answers, but since terminology is the key for a
good search, my searches returned nothing useful.

I have also given some thought to the idea of just
simulating the camera as some sort of constrained
motion, but I have yet to work out the math for it.
I have a vague idea of how to get something working,
but it will be a pure hack and will most certainly
not be as smooth and “natural” of a motion as I’d

I am going to keep thinnking about it, but all ideas
and suggestions are welcome. If you could tell us
some more about your quaternion shortest-arc method,
perhaps I can build on it. My promise is that I will
post on this forum any solution I come up with…

OK. I coded that up last night (my hack mentioned
above) and it works – at least “it works” said in a
naive way.

I initialize the camera’s position and orientation
to be facing the moving object (no generality whatsoever),
and at each frame, I reconstruct the vector pointing
in the direction of the camera (the “lookat” vector)
by normalizing the vector formed by subtracting the
camera’s coordinates from the objects coordinates.
Then, I recompute the camera’s “up” vector by using
the new “lookat” vector and the old “right” vector.
Using the new “up” vector I recompute the camera’s
“right” vector (and of course, I normalize them all).
The last two steps are done using vector cross

The extra, but necessary, step that makes the camera’s
rotation realistic is to constrain the camera’s
“right” vector to be parallel to what you consider
the “ground” plane. so you run through this process
again, but this time you first form the up vector
from the “lookat” and the “right” vectors. In this
way you have, basically, introduced an extra
iteration to the process. This can probably be done
at once with little error if any – I will have to
think about this one.

I will edit this post later and add pseudo code.
I hope this helps, but I have to point out that this
is not the perfect way of doing it, and I almost
hate myself for not coming up with a better solution…

I think he’s asking how he can obtain the yaw, pitch, and roll angles from a single vector, or maybe a matrix. I found a website that discusses it, but the method to extract the yaw, pitch, and roll doesn’t work. If I rotate an identity matrix by yaw, pitch, and then roll, then when I extract the yaw, pitch, and roll from the transformed matrix, they should be the same yaw, pitch, and roll I rotated the identity matrix with.

Here’s the website(the sample code at the bottom):

The Rotate(float y, float p, float r) function works right, but the other one doesn’t.

Anyway, does anyone know of a method to extract a yaw, pitch and roll from a matrix that works?