
#Include once "Vector.bi"



Sub Matrix_From_Vectors( Byref Matrix As Single Ptr, Byval Vec As vMatrix_Struct )
    Matrix[0] = Vec.R.X
    Matrix[1] = Vec.R.Y
    Matrix[2] = Vec.R.Z
    
    Matrix[4] = Vec.U.X
    Matrix[5] = Vec.U.Y
    Matrix[6] = Vec.U.Z
    
    Matrix[8]  = Vec.D.X
    Matrix[9]  = Vec.D.Y
    Matrix[10] = Vec.D.Z
    
    Matrix[12] = Vec.P.X
    Matrix[13] = Vec.P.Y
    Matrix[14] = Vec.P.Z
End Sub


Sub Vectors_From_Matrix( Byref Vec As vMatrix_Struct, Byval Matrix As Single Ptr )
    Vec.R.X = Matrix[0]
    Vec.R.Y = Matrix[1]
    Vec.R.Z = Matrix[2]
    
    Vec.U.X = Matrix[4]
    Vec.U.Y = Matrix[5]
    Vec.U.Z = Matrix[6]
    
    Vec.D.X = Matrix[8]
    Vec.D.Y = Matrix[9]
    Vec.D.Z = Matrix[10]
    
    Vec.P.X = Matrix[12]
    Vec.P.Y = Matrix[13]
    Vec.P.Z = Matrix[14]
End Sub


Sub Rot_X( Byref Char As Character_Struct, Byval Angle As Single) 
    Dim tDir As Vector3D, tSin As Single, tCos As Single
    
    tCos = Cos(Angle)
    tSin = Sin(Angle)
    
    'multiply the Direction Vector by the Cosine of the angle
    'multiply the Up Vector by the Sine of the angle,
    'Then, add them together.
    'Since the Up Vector is already Perpendicular,
    'this operation rotates the Direction Vector about the Right Vector.
    'It's the equivelant of rotating an object's "nose" up & down.
    tDir.X = Char.DVec.X*tCOS+Char.UVec.X*tSin
    tDir.Y = Char.DVec.Y*tCOS+Char.UVec.Y*tSin
    tDir.Z = Char.DVec.Z*tCOS+Char.UVec.Z*tSin
    
    'Make sure the temp vector is unit length by normalizing.
    Vector_Normalize tDir
    
    'Now, the temp vector is ready to copy over the Direction Vector variables...
    Char.DVec.X = tDir.X 
    Char.DVec.Y = tDir.Y 
    Char.DVec.Z = tDir.Z    
    
    'Since we rotated the Direction Vector Up/Down, 
    'The Direction Vector, and the Up Vector are no longer Orthographic.
    'We fix this by taking the cross product of the Direction vector, 
    'and the Right vector to get a new Perpendicular vector,
    'which is the inverse of our new Up vector.
    Char.UVec = Vector_Cross( Char.DVec, Char.RVec )
    
    'As stated before, take the inverse...
    Char.UVec.X =-Char.UVec.X
    Char.UVec.Y =-Char.UVec.Y
    Char.UVec.Z =-Char.UVec.Z
End Sub 



Sub Rot_Y( Byref Char As Character_Struct, Byval Angle As Single) 
    Dim tDir As Vector3D, tCos As Single, tSin As Single 
    
    tCos = Cos(Angle)
    tSin = Sin(Angle)
    
    'Here, we basically just...
    'multiply the Direction Vector by the Cosine of the angle
    'multiply the Right Vector by the Sine of the angle,
    'Then, subtract the Right Vector from the Direction Vector.
    'Since the Right Vector is already Perpendicular,
    'this operation rotates the Direction Vector about the Up Vector.
    'It's the equivelant of rotating an object's "nose" left & right.
    tDir.X = Char.DVec.X*tCOS-Char.RVec.X*tSin
    tDir.Y = Char.DVec.Y*tCOS-Char.RVec.Y*tSin
    tDir.Z = Char.DVec.Z*tCOS-Char.RVec.Z*tSin
    
    'Make the temp vector unit length by normalizing...
    Vector_Normalize tDir 
    
    'Now, the temp vector is ready to copy to the actual Direction Vector variable...
    Char.DVec.X = tDir.X
    Char.DVec.Y = tDir.Y
    Char.DVec.Z = tDir.Z
    
    'Since we rotated the Direction Vector left/right, 
    'The Direction Vector, and the Right Vector are no longer orthographic.
    'We fix this by taking the cross product of the Direction vector, 
    'and the Up vector to get a new Perpendicular vector,
    'which is our new Right vector.
    Char.RVec = Vector_Cross( Char.DVec, Char.UVec )
End Sub 



Sub Rot_Z(Byref Char As Character_Struct, Byval Angle As Single) 
    Dim tRight As Vector3D, tCos As Single, tSin As Single 
    tCos = Cos(Angle) 
    tSin = Sin(Angle) 
    
    
    'Here, we basically just...
    'multiply the Right Vector by the Cosine of the angle
    'multiply the Up Vector by the Sine of the angle,
    'Then, add them together.
    'Since the Up Vector is already Perpendicular,
    'this operation rotates the Right Vector about the Direction Vector.
    'It's the equivelant of an object rolling around a line going from the tail, to the nose.
    tRight.X = Char.RVec.X*tCOS+Char.UVec.X*tSin
    tRight.Y = Char.RVec.Y*tCOS+Char.UVec.Y*tSin
    tRight.Z = Char.RVec.Z*tCOS+Char.UVec.Z*tSin
    
    'Make the temp vector unit length by normalizing...
    Vector_Normalize tRight
    
    'Copy it over the Right Vector...
    Char.RVec.X = tRight.X 
    Char.RVec.Y = tRight.Y 
    Char.RVec.Z = tRight.Z 
    
    'Same function as the end of the Rot_X sub. ;)
    Char.UVec = Vector_Cross( Char.DVec, Char.RVec )
    Char.UVec.X =-Char.UVec.X 
    Char.UVec.Y =-Char.UVec.Y 
    Char.UVec.Z =-Char.UVec.Z 
End Sub
