Results 1 to 10 of 18

Thread: Direction between 2 Vectors

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    I dont think the man is looking for an angle between two vectors. He is asking for the following: given two positions (origin and target) in 3d space he needs the Yaw and Pitch values which will orient the camera looking from the origin at the target. Colin, what I think you are looking for is simply: rV.y := ArcSin(Dir.y); where Dir is a normalized direction vector (Dir := normlize(targ.x - posX, targ.y - posY, targ.z - posZ))
    Last edited by Dan; 15-11-2012 at 05:00 AM.

  2. #2
    Dan, exactly the answer I needed, this is not the first time your post/replies have saved me from myself, I can't thank you enough but thank you very much

    @User137, Thanks for your reply, your function will also be useful for other purposes. Thanks.

    So my code so far (which works)

    Code:
    //
    // sets the look at target of the camera
    //
    procedure TCameraController.setTarget(const targ: TD3DXMatrix);
    var
      pos, rV: TD3DXVector3;
      diff: TD3DXVector3;
    begin
      pos := D3DXVector3(targ._41, targ._42, targ._43);
    
      D3DXVec3Subtract(diff, pos, position);
      D3DXVec3Normalize(diff, diff);
    
      rV := D3DXVector3(
                arctan2(diff.z, diff.x),
                arcsin(diff.y),
                diff.z
              );
    
      D3DXVec3Lerp(rotation, rV, rotation, 0.1);
    end;
    my only problem now is using the DX function MatrixLookAt for matView seems to have a rotation limit on the yaxis.
    Last edited by Colin; 15-11-2012 at 03:11 PM.
    Download the Ziron Assembler
    Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.

  3. #3
    Quote Originally Posted by Colin View Post
    my only problem now is using the DX function MatrixLookAt for matView seems to have a rotation limit on the yaxis.
    It could be due to gimbal lock.

  4. #4
    I was reading about this a few hours ago and I think you may be right, so I think I will add in some quaternion routines and see if I can solve it. Thanks
    Download the Ziron Assembler
    Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.

  5. #5
    Glad you got it solved, although not by my tip This is how the function ended up to:
    Code:
    function Angle(v1, v2: TVector): single;
    begin
      norm(v1.x, v1.y, v1.z);
      norm(v2.x, v2.y, v2.z);
      result:=arccos(dot(v1, v2));
    end;
    Didn't test yet, but i realized that normalized vectors, when their length is multiplied is 1. That leaves it with just dot product.

  6. #6
    Quote Originally Posted by User137 View Post
    Didn't test yet, but i realized that normalized vectors, when their length is multiplied is 1. That leaves it with just dot product.
    Actually, dot product of two 3D vectors is an indirect measure of angle between them. So if you need to test some specific cases, you may omit the arccos altogether.

  7. #7
    Code:
      
      rV := D3DXVector3(
        arctan2(diff.z, diff.x),
        arcsin(diff.y),
        diff.z //why do you need this?
      );
    
      D3DXVec3Lerp(rotation, rV, rotation, 0.1); // interpolating angles is almost never a good idea, I would bet that whatever trouble you are having is probably caused by this. gimbal lock is a fairly rare phenomenon in 3d graphics and doesn't happen consistently, especially not in your case.
    what I suggest you do if you want to achieve smooth camera tranitions is interpolate direction vectors. or even better - spherically interpolate rotation quaternions.

Bookmarks

Posting Permissions

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