Results 1 to 10 of 18

Thread: Direction between 2 Vectors

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    I understand the issue yes, this is the problem that i have been mainly trying to solve.

    What do you mean by "convert the rotation variable angles into a vector" ? the rotation variable is already a vector ?
    Download the Ziron Assembler
    Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.

  2. #2
    Quote Originally Posted by Colin View Post
    the rotation variable is already a vector ?
    no it's not=) you just store the angles it in a vector format that's it. what you need to do is convert these angles into a 3d direction vector.
    ok, here's how you do it:
    Code:
    var 
      Dir: TD3DXVector3;
      sh, ch, sv, cv: Single;
    ...
      SinCos(rotation.x, sh, ch);
      SinCos(rotation.y, sv, cv);
      Dir.x := ch * cv; Dir.y := sv; Dir.z := sh * cv;
    then you find the "diff" vector just as you do now.
    interpolate between the Dir and diff vector (Dir is the old camera direction, diff is the new camera direction).
    so you might want to do something like this:
    Code:
      Dir.x := Dir.x + (diff.x - Dir.x) * tween;
      Dir.y := Dir.y + (diff.y - Dir.y) * tween;
      Dir.z := Dir.z + (diff.z - Dir.z) * tween;
      D3DXVec3Normalize(Dir, Dir);
    and finally convert the Dir vector to rotation angles. I don't know why you are using a 3d vector for rotation
    angles, but here it is:
    Code:
      rotation := D3DXVector3(
        arctan2(Dir.z, Dir.x),
        arcsin(Dir.y),
        0
      );
    overall this approach should work fine. however, there is still a more consistent and reliable way of spherically interpolating quaternions instead of vectors,
    but that is a tale for another time.
    Last edited by Dan; 17-11-2012 at 03:59 PM.

  3. #3
    Hello Dan,

    Thanks for the code, and for the explanation, it has helped me to understand the idea behind the method. I have updated the procedure with your code and now it works exactly as it should.

    Code:
    //
    // sets the look at target of the camera
    //
    procedure TCameraController.setTarget(const targ: TD3DXMatrix);
    const
      tween = 0.01;
    var
      pos, diff, Dir: TD3DXVector3;
      sh, ch, sv, cv: Extended;
    begin
      SinCos(rotation.x, sh, ch);
      SinCos(rotation.y, sv, cv);
      Dir := D3DXVector3(ch * cv, sv, sh * cv);
    
      pos := D3DXVector3(targ._41, targ._42+1, targ._43); // + 1 look slightly above the object
    
      D3DXVec3Subtract(diff, pos, position);
      D3DXVec3Normalize(diff, diff);
    
      Dir := D3DXVector3(
                Dir.x + (diff.x - Dir.x) * tween,
                Dir.y + (diff.y - Dir.y) * tween,
                Dir.z + (diff.z - Dir.z) * tween
              );
      D3DXVec3Normalize(Dir, Dir);
    
      rotation := D3DXVector3(
                    arctan2(Dir.z, Dir.x),
                    arcsin(Dir.y),
                    0
      );
    end;
    Once I finish adding an Actor class, then maybe i will come back to working on the camera class to look into the quaternion alternatives.

    Thanks again
    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
  •