3d rotation around the origin

11,735

If you truly want any success in this, you're going to have to bite the bullet and learn about rotation matrices and / or quaternion rotations. There may be other ways to do what you want, but rotation matrices and quaternion rotations are used simply because they are widely understood and among the simplest means of expressing and applying rotations to vectors. Any other representation somebody can come up with will probably be a more complex reformulation of one or both of these. In fact it can be shown rotation is a linear transformation and so can be expressed as a matrix. Quaternion rotations are just a simplified means of rotating vectors in 3D, and therefore have equivalent matrix representations.

That said, it sounds like you're interested in grabbing an object in your scene with a mouse click and rotating in a natural sort of way. If that's the case, you should look at the ArcBall method (there are numerous examples you may want to look over). This still requires you know something of quaternions. You will also find that an at least minimal comprehension of the basic aspects of linear algebra will be helpful.

Update: Based on your diagram and the comments it contains, it looks like all you are really trying to do is to convert Spherical Coordinates to Cartesian Coordinates. As long as we agree on the the notation, that's easy. Let θ be the angle you're calling XY, that is, the angle between the X axis rotated about the Z axis; this is called the azimuth angle and will be in the range [0, 2π) radians or [0°, 360°). Let Φ be an angle between the XY plane and your vector; this is called the elevation angle and will be in the range [-π/2, +π/2] or [-90°, +90°] and it corresponds to the angle you're calling the XZ angle (rotation in the XZ plane about the Y axis). There are other conventions, so make sure you're consistent. Anyway, the conversion is simply:

x = d∙cos(Φ)∙cos(θ)
y = d∙cos(Φ)∙sin(θ)
z = d∙sin(Φ)
Share:
11,735
Mac Goldwhite
Author by

Mac Goldwhite

Updated on June 04, 2022

Comments

  • Mac Goldwhite
    Mac Goldwhite almost 2 years

    I know there are plenty of questions about 3d rotation that have been answered here but all of them seem to deal with rotational matrices and quaternions in OpenGL (and I don't really care if I get gimbal lock). I need to get 3d coordinates EX:(x,y,z) of a point that always must be the same distance, I'll call it "d" for now, from the origin. The only information I have as input is the deltax and deltay of the mouse across the screen. So far here is what I have tried:

    First:

    thetaxz+=(omousex-mouseX)/( width ); 
    thetaxy+=(omousey-mouseY)/( height);
    

    (thetaxy is the angle in radians on the x,y axis and thetaxz on the x,z axis) (I limit both angles so that if they are less than or equal to 0 they equal 2*PI)

    Second:

    pointX=cos(thetaxz)*d;
    pointY=sin(thetaxy)*d;
    

    (pointX is the point's x coordinate and pointY is the y)

    Third:

    if(thetaxz)<PI){
     pointZ=sqrt(sq(d)-sq(eyeX/d)-sq(eyeY/d));
    }else{
      pointZ=-sqrt(abs(sq(d)-sq(eyeX/d)-sq(eyeY/d)));
    }
    

    (sq() is a function that squares and abs() is an absolute value function) (pointZ should be the point's z coordinate and it is except at crossing between the positive z hemisphere and negative z hemisphere. As it approaches the edge the point gets stretched further than the distance that it is always supposed to be at in the x and y and seemingly randomly around 0.1-0.2 radians of thetaxz the z coordinate becomes NAN or undefined)

    I have thought about this for awhile, and truthfully I'm having difficulty warping my head around the concept of quaternions and rotational matrices however if you can show me how to use them to generate actual coordinates I would be glad to learn. I would still prefer it if I could just use some trigonometry in a few axis. Thank you in advance for any help and if you need more information please just ask.

    Hint/last minute idea: I think it may have something to do with the z position affecting the x and y positions back but I am not sure.

    EDIT: I drew a diagram: enter image description here

  • Mac Goldwhite
    Mac Goldwhite over 12 years
    Thanks, but I'm not using opengl and I'm not really grabbing an object I'm rotating the scenes camera not transforming the scenes coordinate system. Is there a way to get coordinates from a rotation matrix because then I could use them?
  • andand
    andand over 12 years
    Based on your diagram, I think you just want to convert from Spherical to Cartesian coordinates. I've edited my response to show you how to do that. If you're trying to do anything interactive with the mouse and moving your camera about the origin this way, I don't think you're going to get quite the results you are hoping for. That said, I really think you would be better off at least considering the ArcBall approach.
  • Mac Goldwhite
    Mac Goldwhite over 12 years
    Thank you, I figured that out just before you posted this but thanks anyway! Great help I'd give you plus one but I don't quite have enough rep yet myself. I'll try and come back later because you were very helpful and informative.
  • Mike 'Pomax' Kamermans
    Mike 'Pomax' Kamermans over 3 years
    It appears the link to the arcball paper has become a 404
  • andand
    andand over 3 years
    @Mike'Pomax'Kamermans Thanks for letting me know. I found another source for it and updated the link. Enjoy.