Thread: Rotation in 3D?

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question Rotation in 3D?

    Say I have this:

    Xpos = cos( myRot )
    Ypos = sin( myRot )

    What would this be:

    Zpos = ?

    I am assuming I would use another rotation var, but I would have to use it in my Y and Z dimentions. Am I going to use tan() at all? cos() or sin() again? Where would I use them? I am at a loss here, please help!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    First, your 2d rotation is wrong. What you get from sine and cosine are the axis-aligned offsets for movement of 1 at an angle given by the trig argument.
    Correct 2d rotation is calculated as follows. Given a point P, you get the point P', which is P rotated around the origin by an angle of A in your favourite angle system, with the formula
    P' = P cos(A) + Q sin(A) where Q is the point rotated counterclockwise by 90 degrees. In other words,
    P'x = Px cos(A) - Py sin(A)
    P'y = Py cos(A) + Px sin(A)
    or in matrix form:
    Code:
    P' = / cos(A)  -sin(A) \
         \ sin(A)   cos(A) / * P
    Now it gets complicated. See, a 2d rotation is pretty simple, because it is always around the z-axis, i.e. if you stuck a pencil through the sheet of paper, you could then rotate around that pencil.

    In 3d, rotation is far more complex, because it can be around an arbitrary axis. There are the three base axis rotations pitch, yaw and roll. In the typical object space, where x is left-right, y is up-down and z is front-back, i.e. depth, pitch is rotation about the x axis, yaw is around the z axis and roll is about the y axis. Think of a plane.
    When you have these rotations, it's only the other two coordinates that change, using the standard sine/cosine scheme. Not sure which is sine and which is cosine, but that's pretty easy to work out and doesn't really matter anyway.
    These rotations are simple and can be represented by a 3x3 matrix that consists of the 4 fields in the above 2x2 matrix in the right places, filled up with the values of the identity matrix in the other fields.

    Rotations about an arbitrary axis are more complex. I won't go into deriving the formula. Look it up somewhere on the web or buy the excellent book "Mathematics for 3d Game Programming and Computer Graphics".
    The matrix for a rotation of an angle of R about an axis represented by the unit vector A is given by this formula and matrix (C is cos(R) and S is sin(R)):
    Code:
         / C + Ax²(1-C)      AxAy(1-C) - AzS   AxAz(1-C) + AyS \
    P' = | AxAy(1-C) + AzS   C + Ay²(1-C)      AyAz(1-C) - AxS | * P
         \ AxAz(1-C) - AyS   AyAz(1-C) + AxS   C + Az²(1-C)    /
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I don't quite understand what you are saying, it's "over my head"; which is okay though, becuase I got it to work. My resualt: Zpos = sin( mySecondRot ). But with a couple tweaks, I had to use 90 degrees less than X and Y, and I had to make it negitive.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    But if zpos = sin ( some angle ) - where does the original point's position come in? All points do not rotate to the same z position. (Ditto for your original equations.)
    About the only 3D math I remember is the distance formula, unfortunately...

    Looking at Wolfram's "Rotation Matrix" article, it's simple enough for me to implement. (Although I'd have to remember how to do matrix multiplication apperantly, and memory seems to say that was painful...) (Also, I got there by google-ing for "vector rotation")
    Hope that helps a bit.
    Edit: I learned matrix multiplication in Alegbra II, and vectors in Precalc - to give you and idea of what math you might need.
    Last edited by Cactus_Hugger; 12-24-2006 at 12:07 AM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Queatrix
    I don't quite understand what you are saying, it's "over my head";
    Do you at least understand the part where I said that your rotation calculation is completely wrong in the first place? The only point you can rotate correctly this way is (1,0).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. camera rotation matrix
    By Vick jr in forum Game Programming
    Replies: 5
    Last Post: 05-26-2009, 08:16 AM
  2. 3D rotation problem, help please!
    By GOBLIN-85 in forum Game Programming
    Replies: 22
    Last Post: 03-13-2009, 04:37 PM
  3. A 3D rotation problem
    By GOBLIN-85 in forum C++ Programming
    Replies: 0
    Last Post: 03-08-2009, 07:17 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. rotation in 3d
    By DavidP in forum Game Programming
    Replies: 3
    Last Post: 11-04-2003, 10:06 PM