Thread: Vector rotation

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    Vector rotation

    After some research and the right google searches I found this information regarding vector rotation.

    #local AngleX = degrees(atn((T.z-L.z)/vlength(<T.x,T.y,0>-<L.x,L.y,0>));
    #if (T.y>L.y)
    #local AngleZ = degrees(atn((L.x-T.x)/(T.y-L.y)));
    #end

    #if (T.y<L.y)
    #local AngleZ = degrees(atn((L.x-T.x)/(T.y-L.y)))+180;
    #end

    #if (T.y=L.y)
    #if (T.x>L.x)
    #local AngleZ=-90;
    #else
    #local AngleZ=90;
    #end
    #end

    object { Gun rotate <AngleX,0,AngleZ> translate L }

    or you can do this instead:
    #local NY=vnormalize(T-L);
    #local NX=vnormalize(vcross(NY,z)); // z is down in this example
    #local NZ=vcross(NX,NY);
    object { Gun matrix < NX.x,NX.y,NX.z, NY.x,NY.y,NY.z, NZ.x,NZ.y,NZ.z, L.x,L.y,L.z > }
    Now this is for the POV ray tracer program. But, I think I can convert it to Direct3D.

    So to rotate a point that is at L to face T.
    Code:
    void MatrixRotateObjectOntoVector(D3DXVECTOR3 Target,D3DXVECTOR3 Location,D3DXMATRIX &RotMatrix)
    {
      D3DXVECTOR3 NX,NY,NZ,;
    
      NY=Target-Location;
      D3DXVec3Normalize(&NY,&NY);
    
      D3DXVec3Cross(&NX,&NY,&(-Camera.UpVector));
      D3DXVec3Normalize(&NX,&NX);
    
      D3DXVec3Cross(&NZ,&NX,&NY);
    
      //Setup rotation and translation
      RotMatrix._11=NX.x;
      RotMatrix._12=NX.y;
      RotMatrix._13=NX.z;
      RotMatrix._14=0.0f;
    
      RotMatrix._21=NY.x;
      RotMatrix._22=NY.y;
      RotMatrix._23=NY.z;
      RotMatrix._24=0.0f;
    
      RotMatrix._31=NZ.x;
      RotMatrix._32=NZ.y;
      RotMatrix._33=NZ.z;
      RotMatrix._34=0.0f;
    
      RotMatrix._41=Location.x;
      RotMatrix._42=Location.y;
      RotMatrix._43=Location.z;
      RotMatrix._44=1.0f;
    }
    Last edited by VirtualAce; 04-09-2005 at 06:44 PM.

Popular pages Recent additions subscribe to a feed