Results 1 to 10 of 18

Thread: Direction between 2 Vectors

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Quote Originally Posted by User137 View Post
    I had this function in my old codes:...
    Your code is perfectly fine. You don't need ABS in the last line though as ArcCos returns values from 0 to Pi. Values inside SQRT will never be negative as they are elevated to square. Detailed math is explained here. As you can see, your code does exactly that - the first part of code calculates dot product and second part divides it by multiplication of two vector lengths. P.S. if a>1 check was probably due to vectors not being of unitary length, which you should normalize instead.

  2. #2
    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.

  3. #3
    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.

  4. #4
    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.

  5. #5
    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.

  6. #6
    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.

  7. #7
    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.

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
  •