Results 1 to 7 of 7

Thread: [RESOLVED] 3D rotation

  1. #1

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Resolved [RESOLVED] 3D rotation

    So I am facing this problem that I KNOW I have faced before...the solution can probably be found somewhere in a couple of my old projects.

    I have a unit vector that describes a direction.
    I have an object that needs to be rotated accordingly. When no rotation is applied, the object points straight down (0, -1, 0).

    So, as an example, if this direction vector is (0, 1, 0), the object could be rotated 180 degrees on the X or Z axis in order to point in the diretion of the vector.

    I'm looking for the best way retrieve the radians to rotate the object to point in the direction of said unit vector.
    If there are plenty ways to skin this cat, I'd really like the most "efficient" way to calculate this.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: 3D rotation

    There are several solutions, depending on precisely what you want and have.

    1. If you have a start and end direction vector, the easiest solution is a linear interpolation between the two--no angles or trig required. That might be problematic since a linear interpolation of the direction vectors is a nonlinear interpolation when the intermediate vectors are projected back onto the unit sphere by normalizing. This can be overcome with a little bit of calculus and trig. Another problem is that your intermediate vectors may sometimes be the 0 vector, which generally isn't a valid direction.
    2. If you just want the angle between two vectors, the dot product formula is easiest. I explained it in this post.
    3. If you have an axis-angle rotation routine (like the one I described in this thread, using quaternions) you can generate the relevant axis by taking the cross product of your input vectors and calculating the angle using (2). There are degenerate cases like your example when a unique rotation isn't determined, which corresponds to the cross product being the 0 vector. These need to be special-cased; one technique that comes to mind is converting to spherical, where the special cases are all simple.


    (1) is quite elegant if you don't mind a nonlinear rotation. (3) is quite powerful but complicated if you don't already have an axis-angle rotation routine. (2) answers "the best way retrieve the radians to rotate the object", which might not have said precisely what you meant it to say.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: 3D rotation

    Thanks for your reply jemidiah.

    I am actually very proud of myself right now. I figured out the solution you mention as #3 before reading your response. It looks like this at the moment:

    Code:
                    Vector3 axis = Vector3.Cross(this.direction, Vector3.Up);
                    axis.Normalize();
                    float dot = Vector3.Dot(this.direction, Vector3.Up);
    
                    this.world = Matrix.CreateScale(new Vector3(triangleBase, length, triangleBase)) *
                        Matrix.CreateFromAxisAngle(axis, -(float)Math.Acos(dot)) * Matrix.CreateTranslation(this.position);
    Thank you very much for your help!
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: [RESOLVED] 3D rotation

    apparently I must spread before I can rep you again Jed but excellent post
    W o t . S i g

  5. #5

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [RESOLVED] 3D rotation

    Quote Originally Posted by Milk View Post
    apparently I must spread before I can rep you again Jed but excellent post
    Same here unfortunately
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: [RESOLVED] 3D rotation

    BTW, there's a link in my sig to my old Coordinates, Vectors and 3D Volumes project, which has got a maths library and some functions to do a load of this stuff, if you're interested.....
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  7. #7

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [RESOLVED] 3D rotation

    Thanks zaza, I'll definitely check it out!
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width