Dear charltonian,
A much simpler solution than the perspective projection suggested in the
comment below, and arguably a more attractive one, is the orthographic
projection. I shall give you the formulas to convert (x, y, z) coordinates
into (x, y) coordinates after rotation about the axes.
Suppose that we rotate the cube by Rx degrees about the x axis, by Ry
degrees about the y axis, and by Rz degrees about the z axis.
First, apply the x-axis rotation to transform coordinates (x, y, z)
into coordinates (x0, y0, z0).
x0 = x
y0 = y*cos(Rx) + z*sin(Rx)
z0 = z*cos(Rx) - y*sin(Rx)
Then apply the y-axis rotation to (x0, y0, z0) to obtain (x1, y1, z1).
x1 = x0*cos(Ry) - z0*sin(Ry)
y1 = y0
z1 = z0*cos(Ry) + x0*sin(Ry)
Finally, apply the z-axis rotation to obtain the point (x2, y2).
x2 = x1*cos(Rz) + y1*sin(Rz)
y2 = y1*cos(Rz) - x1*sin(Rz)
Hey presto, you can now plot (x2, y2) in two dimensions.
Regards,
leapinglizard |
Request for Answer Clarification by
charltonian-ga
on
09 Apr 2005 02:32 PDT
Ok, thanks leapinglizard, I had actually worked through a similar
solution myself, although I had utilised a combination matrix for all
the rotations in one. I actually pulled it from a C++ manual and after
dealing with a typo, got it to function
But this, as with yours has a problem, which unfortunately was not
covered in the first question: the Gimble/Gimbal lock.
I really want to rotate my cube through its internal axes rather than
the global axes - if you can suggest a method for doing this (using
global angles of rotation, not euler) then we have a double or quits
scenario.
(essentially I'll pay up in a week or so, but if you can do this last
part, and I can get it to work, I'll 'tip-up' to a total of $50)
ps. my matrix is:
{cos(rY) * cos(rX) - sin(rZ) * sin(rY) * sin(rX), cos(rY) * sin(rX) +
sin(rZ) * sin(rY) * cos(rX), -cos(rZ) * sin(rY)}
{cos(rZ) * sin(rX), cos(rZ) * cos(rX), sin(rZ)}
{sin(rY) * cos(rX) + sin(rZ) * cos(rY) * sin(rX), sin(rY) * sin(rX) -
sin(rZ) * cos(rY) * cos(rX), cos(rZ) * cos(rY)}
|
Clarification of Answer by
leapinglizard-ga
on
09 Apr 2005 03:14 PDT
I'm not sure what you mean by internal axes. Do you mean that you want
to rotate the cube around lines that run perpendicularly through the
center of each face? If so, why not just position the cube so that its
center is at the origin? The corners would then be at (-h, -h, -h),
(h, -h, -h), (-h, h, -h), (h, h, -h), (-h, -h, h), (h, -h, h), (-h, h,
h), (h, h, h) for a cube with side length 2h. Rotation around the axes
would then have the effect of rotating the cube in place.
leapinglizard
|
Request for Answer Clarification by
charltonian-ga
on
10 Apr 2005 07:25 PDT
ok, sorry I'm not explaing this very well.
The cube is always located at point (0,0,0) with the dimensions you
have listed. There is never any translation or scaling (or shearing
for that matter) only rotation around the central point.
You are correct, I want to rotate the cube through the normal of the
faces (which initially would line up with the global x,y,z axes)
The problem I have is that the rotation matrices only seem to rotate
the cube through the global axes, I would like to know how to adjust
the matrix in order to rotate through the normals?
|
Clarification of Answer by
leapinglizard-ga
on
12 Apr 2005 20:02 PDT
I think I know what the matter is, but I'll have to write a program
and run some experiments when I can grab a spare moment. Are you
anxious for the solution, or can you wait a few days?
leapinglizard
|
Request for Answer Clarification by
charltonian-ga
on
13 Apr 2005 00:53 PDT
Yes, I can definitely wait.
I should point out that I'm essentially a programmer myself, but ~I
only work through fairly high level languages - so please no c++ based
answers otherwise I'll have to pick through it for ages.
|
3D to 2D
========
To convert x,y,z world coordinates to x,y screen coordinates, search
for the term "perspective projection". There are equivalent ways to do
it depending on how you set up your axes and your "eye" point. Here's
one way:
Let the x-y plane represent the screen with the origin in the center.
Let the z axis point into the screen. Put the center of projection
(the "eye") at point (0, 0, -d), that is, distance d behind the center
of the screen.
Let's start with a special equation for a line in 3 dimensions.
x = X1 + u (X2 - X1)
y = Y1 + u (Y2 - Y1)
z = Z1 + u (Z2 - Z1)
Where u = 0 to 1. When u = 0 the equations return the endpoint
(X1,Y1,Z1) and when u = 1 the equations return the endpoint
(X2,Y2,Z2). When u is between 0 and 1 we get a point on the line.
Let the first endpoint be the "eye" and the second endpoint be the
world point (X,Y,Z).
x = 0 + uX
y = 0 + uY
z = -d + u(Z + d)
We want to find where the line intersects the "screen," so we find the
value of u that intersects the x-y plane. That is, we find u where
z=0.
z = 0
-d + u(Z+d) = 0
u(Z+d) = d
u = d/(Z+d)
Plugging in to the other equations:
x = (d/(Z+d)) X
y = (d/(Z+d)) Y
This is the basic transformation. You still need to convert the x and
y values to appropriate pixel ranges for your screen.
Rotations
=========
When doing the rotations, it is important to understand that the order
you do the rotations in affects the outcome. You can't just specify
three angles, you also have to state what order you do the rotations
in.
For example, imagine a book lying flat on a desk. Let the desk be the
x-y plane and let the z axis point up out of the desk.
Rotate the book 90 degrees about the z axis (turn it left) and then
rotate it 90 degrees about the x axis (tilt it up)
will end up differently from
Rotate the book 90 degrees about the x axis (tilt it up) and then
rotate it 90 degrees about the z axis (turn it left while looking
down).
(Note: When we say "rotate about an axis" we mean we are looking down
the pointy part of that axis's arrow (it is poking us in the eye) and
we rotate in the plane of the other two axes. From this viewpoint, the
angles indicate counter-clockwise rotations.)
The rotation equations below just do one step. The x,y,z values are
the "inputs" and the x', y', z' values are the "outputs". To do one
rotation after the other, you'd take the outputs from one step and
plug them in as the inputs to the next step.
Rotation about the z axis:
x' = x cos theta - y sin theta
y' = x sin theta + y cos theta
z' = z
Rotation about the x axis:
y' = y cos theta - z sin theta
z' = y sin theta + z cos theta
x' = x
Rotation about the y axis:
z' = z cos theta - x sin theta
x' = z sin theta + x cos theta
y' = y
I might have got them a little mixed up there. It might depend on how
you set up the axes, I don't remember.
Computer graphics routines usually use matrix math to represent the
rotation equations and other 3D transformations. This has a nice
effect that you can multiply all the matrices for the individual steps
(in the proper order) and get a result matrix that represents the
combined steps. Then you could use that matrix to transform all your
world points without a bunch of in-between steps.
Recommended searches
====================
I tried to find something for you with Google, but everything I found
was either too complicated or for a specific computer language or
program and didn't really explain the basic math. But if you look on
Google or other places remember these terms:
"perspective projection" is the name for the 3D to 2D conversion.
"3D transformations" includes the rotations and other things you can
do in points in 3D.
Try looking in your library for a book on computer graphics. I got the
above info from a book called "Computer Graphics" by Donald Hearn and
M. Pauline Baker (Prentice Hall, 1986) but any computer graphics book
with this kind of math in it will work. |