Results 1 to 10 of 18

Thread: Direction between 2 Vectors

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

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

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

  4. #4
    That sounds good, but I seem to be having a lot of problems trying to incorporate this into my camera class, could you give some example? I appreciate the help. Thanks

    I have made some changes and changed the lerp call to some linear interpolation which is working very smoothly and looks fantastic, i will post the code tomorrow when i'm able.
    Last edited by Colin; 16-11-2012 at 10:20 PM.
    Download the Ziron Assembler
    Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.

  5. #5
    well first of all remove the interpolation and make sure everything is working like this:
    rotation := rV;

  6. #6
    Hello Dan, Yes it does work fine, expect still the limitation on the Y axis,

    However after a bit testing i see the problem is on the xz rotation it takes 2*PI for full rotation, on the Y Axis, it would technically take 4*PI, so instead i will do a basic test for flipping the camera, and changing the right and up vectors so the view will then be upside down.
    Last edited by Colin; 17-11-2012 at 10:36 AM.
    Download the Ziron Assembler
    Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.

  7. #7
    OK i have my camera system working exactly how i want it except for 1 problem, I have uploaded test build, press N to spawn a ball, then if you press down and to the left so the ball rolls around the camera will at 1 point spin in the opposite direction back to the ball.

    New code:

    Code:
    //
    // sets the look at target of the camera
    //
    procedure TCameraController.setTarget(const targ: TD3DXMatrix);
    const
      tween = 0.01;
    var
      pos, rV: TD3DXVector3;
      diff: TD3DXVector3;
    begin
      pos := D3DXVector3(targ._41, targ._42+5, targ._43); // + 5 look slightly above the object
    
      D3DXVec3Subtract(diff, pos, position);
      D3DXVec3Normalize(diff, diff);
    
      rV := D3DXVector3(
                arctan2(diff.z, diff.x),
                arcsin(diff.y),
                0
              );
    
    
      rotation.x := (rV.x * tween) + rotation.x * (1 - tween);
      rotation.y := (rV.y * tween) + rotation.y * (1 - tween);
      rotation.z := (rV.z * tween) + rotation.z * (1 - tween);
    end;
    
    //
    // sets the position of the camera somewhere behind the object, either facing or by velocity
    //
    procedure TCameraController.Follow(m: TD3DXMatrix; heading: TD3DXVector3);
    const
      Dist = 20;
      MaxDist = 50;
      DefHeight = 15;
    var
      tweenSpeed: TD3DXVector3;
      absdist: TD3DXVector3;
    
      pos: TD3DXVector3;
      gPoint: TD3DXVector3;
    begin
      pos := D3DXVector3(m._41, m._42, m._43);
    
      if (heading.x > 0.00001) and (heading.z > 0.00001) then begin
        D3DXVec3Normalize(heading, heading);
        gPoint.x := m._41 + -Dist*heading.x;
        gPoint.z := m._43 + -Dist*heading.z;
        gPoint.y := m._42 + DefHeight;
      end else begin
        D3DXVec3Normalize(heading, pos);
        gPoint.x := m._41 + -Dist*heading.x;
        gPoint.z := m._43 + -Dist*heading.z;
        gPoint.y := m._42 + DefHeight;
      end;
    
      absdist := D3DXVector3(abs(pos.x-position.x),
                abs(pos.y-position.y),
                abs(pos.z-position.z)
              );
    
      if absdist.x > MaxDist then
        tweenSpeed.x := 0.005 * (absdist.x - (MaxDist-1))
      else
        tweenSpeed.x := 0.005;
    
      tweenSpeed.y := 0.005;
    
      if absdist.z > MaxDist then
        tweenSpeed.z := 0.005 * (absdist.z - (MaxDist-1))
      else
        tweenSpeed.z := 0.005;
    
      position.x := (gPoint.x * tweenSpeed.x) + position.x * (1 - tweenSpeed.x);
      position.y := (gPoint.y * tweenSpeed.y) + position.y * (1 - tweenSpeed.y);
      position.z := (gPoint.z * tweenSpeed.z) + position.z * (1 - tweenSpeed.z);
    end;
    else everything is nice, camera moves very smoothly for the most part, just this problem that is a pain...

    Any ideas?

    Thanks.
    Last edited by Colin; 17-11-2012 at 03:18 PM.
    Download the Ziron Assembler
    Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.

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
  •