This week's book giveaway is in the Java in General forum.We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# 3d vectors

Nick George
Ranch Hand
Posts: 815
Note: this is largely me trying to crystalize my thoughts. Comments greatly appreciated.

so, supposin' we got us a 2d vector, we'll say v=(Δx,Δy). Supposin' we want to rotate that vector by π/2 degrees, we'd simply convert v to polar, increment θ by π/2, and convert back to rectangular. I'm wondering how this translates to 3d.

I'm going to guess that one could form a coordinate system similar to 2d polar with 3d polar, with two angles, (<x,<z,r). If a simple conversion method can be obtained, a similar method of rotation could be implemented.

SO:

the task: convert (Δx,Δy,Δz) to (r,<x,<z).

r is easy, root(Δx^2+Δy^2+Δz^2)

rotating along the x axis causes changes in y and z. If I'm not mistaken, it's nothing more than tan(Δy/Δz).

It would then follow that <z is tan(Δy/Δx)

I then effect the desired change upon the angles, and go back to rectangular.

(r*cos(<z),r*sin(<z),r*cos(<x))

Alright, upon reviewing this, I have decided that I have created a Cube instead of a Sphere.

One way to make sure I'm getting a sphere would be to include the third angle, but I bet I don't need to. I sure hope I manage to grab Jim's attention...

[ December 16, 2004: Message edited by: Nick George ]

Thomas Manthey
Greenhorn
Posts: 12
As far as I remember my lessons in mathematics the solution lies in some special matrix-operations. Maybe the following website (although in german) may be of help: http://www.3dsource.de/deutsch/3Dmathe.htm#Rotation

Michael Herrmann
Ranch Hand
Posts: 60
Here is a great tutorial on 3d that will probably help you.

Jim Yingst
Wanderer
Sheriff
Posts: 18671
[NG]: I sure hope I manage to grab Jim's attention...

Ummm... better late than never? I hope, anyway...

[NG]: I then effect the desired change upon the angles, and go back to rectangular.

(r*cos(<z),r*sin(<z),r*cos(<x))

I think you probably want something more like

(r*cos(<z)*sin(<x),r*sin(<z)*sin(<x),r*cos(<x))

assuming that <x is the absolute angle with the axis. (As opposed to <z which is evidently the angle between the x axis and the projection into the x-y plane. Which seems like a strange naming convention, but I'm trying to play along. Other refereces will probably use different naming conventions.)

BTW, you win bonus points for using "effect" correctly as a verb.

The link to Ahab's Math Tutorial looks good. Can't comment on the German stuff. (Not without a lot more work which I'm too lazy to do.) You may be wondering: why use matrices here? What's the point? The thing is, usually if you're interested in rotating a single point (x1,y1,z1) to a new coordinate, you're also interested in rotating a set of attached points (x[i],y[i],z[i]). Where "attached" means all the points are part of a single rigid body rotating about the origin. (Or about some other point, if you want to combine transposition and rotation in a single operation, which is certainly possible.) Calculating a sine or cosine in general takes a lot more work than a single multiplication, which in turn takes notably more work than a single addition. By using matrices, you get to generalize the procedure for calculating the results of a given rotation, using only a few sine/cosine calculations as a fixed cost for the rotation, plus about 9 multiplications per point (plus a few additions that no one really cares about). Much more efficient than recalbulating a sine &/or cosine for each point you wish to rotate.

One way to make sure I'm getting a sphere would be to include the third angle, but I bet I don't need to.

I'm not sure what you mean by "getting a sphere" here, but in general I think you probably need a third angle to capture all the possibilities here. In general rotation in 3-D needs three angles of some sort. Imagine that you've got a globe (as in, a spherical map of the Earth) and you want to put it in some arbitrary orientation. If you want to point the North pole of the globe in a particular direction, that requires two angles of some sort (or 1 if your cooridinate axes just happen to be oriented just right with respect to the desired rotation, but that's cheating). Once you've got the north pole pointed in the right direction, it still takes one more angle to cover rotations about the polar axis. There are many different ways to specify the 3 different rotational degrees of freedom you need here (which may be intermingled with the 3 different tranpositional degrees of freedom you may also require) but in general, for a rotation in 3 dimensions you need 3 angles. It doesn't look like Ahab's tutorial gets into the details much for this, but you can problably get what you need googling "rotation matrix".

Hope that helps...
[ December 29, 2004: Message edited by: Jim Yingst ]

Ellen Zhao
Ranch Hand
Posts: 581
try 3D Transformation

I hardly remember the polar coordinates playing a more important role than homogeneous coordinates in the field of 3D transformation.

Nick if you are really into 3D grahics, make sure you are well aware of how the graphics pipeline works. This is very fundamental and essential.

I used the book Computer Graphics Using OpenGL by Hill when I was writing the thesis "3D Transformation". Highly recommended for beginners.
[ December 29, 2004: Message edited by: Ellen Zhao ]

Ellen Zhao
Ranch Hand
Posts: 581
Sorry I posted my last reply without having read Jim's reply. He's got the point on the transformation matrices...

Nick You can play with the Graphics Transformation Matrices here. It's implemented in Java. But if you want to quickly get your game done, just resort to a quick reference on OpenGL. It's a great API with embeded 3D rotation, translation.., you don't have to know that much math to get your work done...Enjoy!

Jim Yingst
Wanderer
Sheriff
Posts: 18671