Associated Topics || Dr. Math Home || Search Dr. Math

### Raytrace of a Star Sapphire

```
Date: 12/11/96 at 21:04:33
From: Charles
Subject: Angle from one 3-dimensional vector to another in terms of
rotating on x,y,and z axis.

Hello,

I am a HS graduate with no college math who is _very_ rusty on pre-
calc and geometry/trig.  My problem comes from an attempt to create a
raytrace of a star sapphire that acts like a real star sapphire, which
always faces the camera.  To do this, I need to find the degrees of
rotation to use to face the star towards the camera position no matter
where it is.  If memory serves, (which is questionable) the following
formula _should_ apply:

rotation on x axis = degrees (atan((CamY-ObjY)/(CamZ-ObjZ)))
rotation on y axis = degrees (atan((CamZ-ObjZ)/(CamX-ObjX)))
rotation on z axis = degrees (atan((CamX-ObjX)/(CamY-ObjY)))

Where:
CamX,Y,Z = 3-dimensional vector of Camera
ObjX,Y,Z = 3-dimensional vector of Object
Rotation refers to star on the sapphire.

I just can't seem to get the results I want from this.  I remember
something about the tangent of 90 causing problems, but I'm not too
sure.  I will really appreciate it if you can help clear this up for
me.

Thanks...
```

```
Date: 12/12/96 at 09:39:01
From: Doctor Jerry
Subject: Re: Angle from one 3-dimensional vector to another in terms
of rotating on x,y,and z axis.

Hi Charles,

I may be able to help you, but I've got to get the problem clear in
are my impressions and, such as they are, my understanding:

You have a camera at, say, (a,b,c), which can be thought of as a point
or a position vector from the origin.  There is an object at the point
(A,B,C).  There is a plane (the plane of the star) going through the
point (A,B,C).

My understanding starts to fade as I think about the plane.  A plane
containing (A,B,C) can have any orientation.  The orientation of a
plane is usually specified by its normal vector.  This is a vector
that is perpendicular (normal) to the plane.  You appear to be saying
that the orientation of the plane, the normal vector to the plane, is
the vector {A,B,C}.

In any case, if {n1,n2,n3} is a normal to a plane through (A,B,C), do
you want to know how to aim the camera so that the film plane is
parallel to the plane with normal {n1,n2,n3}?  Specifically, do you
want the angles the normal vector makes with the x-, y-, and z-axes?

-Doctor Jerry,  The Math Forum
Check out our web site!  http://mathforum.org/dr.math/
```

```
Date: 12/12/96 at 12:51:42
From: Anonymous
Subject: Re: Angle from one 3-dimensional vector to another in terms
of rotating on x,y,and z axis.

Sorry for being unclear.  The object is originally created at (0,0,0)
with a surface normal for the star being positive y.  The object is
then translated to any other position, (1,0,3) for example, after
which, I need the star texture on it to rotate in such a way that the
surface normal now faces directly at the camera, at location (2,0,-1).
I am trying to find out what formula to use to determine the rotations
necessary (in degrees) first on the x, then the y, and finally the z
axis in order to face the _texture_ towards the camera at all times,
no matter the position of the object or the camera.  I hope this
clears things up a bit.  I am very grateful for your assistance.

Charles
```

```
Date: 12/14/96 at 13:24:54
From: Doctor Jerry
Subject: Re: Angle from one 3-dimensional vector to another in terms
of rotating on x,y,and z axis.

Hi Charles,

I solved a sample problem (see below), which may help you decide if
I'm on the right track and, if I am, whether you want to pursue this
approach.  Before we get to this, I need to  make a comment or two.
First, although I haven't solved this kind of problem before and don't
find a full solution in the books I own, I'm certain that the problem
has been solved many times before.  There are individuals who
To act on this, I'll post a memo on the Math Forum.  Someone may offer
to help.

I think that your problem boils down to this:

If u and v are unit vectors based at the origin, you want to:

(i) rotate v about the x-axis, giving a new vector p
(ii) rotate p about the y-axis, giving a vector q
(iii) rotate q about the z-axis, giving a vector r, such that r = u.

I took

u = {1/Sqrt[6],1/Sqrt[6],-2/Sqrt[6]}

v = {1/Sqrt[21],4/Sqrt[21],2/Sqrt[21]}

Since u and v are unit vectors, their third components give the
cosines of the angles these vectors make with the z-axis.  I
visualized the vector u as lying on a cone whose vertex is at the
origin and vertical axis. The third component of u determines the
cone.  I first rotate v about the x-axis until it lies on the upper
nappe (in this case) of the cone.  This gives p.  Next, I rotate p
about the y-axis until it lies on the lower nappe of the cone.  This
gives q.  Finally, I rotate q about the z-axis until it coincides with
u.  Each rotation is done using a rotation matrix. These matrices are:

M1(a) =    1     0      0
0   cos a  -sin a
0   sin a   cos a

M2(b) =    cos b     0    -sin b
0       1       0
sin b     0     cos b

M3(c) =    cos c   -sin c    0
sin c    cos c    0
0       0      1

I choose a so that the third component of M1(a)*v is equal to the
absolute value of the third component of u.  I got  a = 0.527509 in

I choose b so that the third component of M2(b)*p is equal to the
third component of u.  I got b = pi.
Then q = {-0.218217,0.534523,-0.816497}.

Finally, I choose c so that the first component of M3(c)*q is equal to
the first component of u.  I got c = 3.53939.
Then r={0.408248,-0.408249,-0.816497}, which is u.

There were several decision points in this procedure, requiring some
visualization.  I graphed the vectors as I went.  I used Mathematica
for my calculations and graphs.

-Doctor Jerry,  The Math Forum
Check out our web site!  http://mathforum.org/dr.math/
```
Associated Topics:
High School Calculus
High School Linear Algebra

Search the Dr. Math Library:

 Find items containing (put spaces between keywords):   Click only once for faster results: [ Choose "whole words" when searching for a word like age.] all keywords, in any order at least one, that exact phrase parts of words whole words

Submit your own question to Dr. Math
Math Forum Home || Math Library || Quick Reference || Math Forum Search