Here is a function I wrote to help someone recently on this forum with getting a direction vector for 3D physics.
It includes a normalizing function which just simply converts all the vector values to be between 0.0 and 1.0.
Also a function to get the angle between 2 3D position vectors.
//Fills empty vector with a normalized direction vector.
// 1 = FWD, 2 = REV, 3 = UP, 4 = DOWN, 5 = RIGHT, 6 = LEFT
Function GetObjectDirectionVector( objID as integer, direction as integer, outDirectionVector as integer )
tempObj = CreateObjectBox( 1.0, 1.0, 1.0 )
objXPos# = GetObjectWorldX( objID )
objYPos# = GetObjectWorldY( objID )
objZPos# = GetObjectWorldZ( objID )
SetObjectPosition( tempObj, objXPos#, objYPos#, objZPos# )
SetObjectRotation( tempObj, GetobjectWorldAngleX( objID ), GetobjectWorldAngleY(objID), GetobjectWorldAngleZ( objID ) )
select direction
case 1 :
MoveObjectLocalZ( tempObj, 1.0 )
endcase
case 2 :
MoveObjectLocalZ( tempObj, -1.0 )
endcase
case 3 :
MoveObjectLocalY( tempObj, 1.0 )
endcase
case 4 :
MoveObjectLocalY( tempObj, -1.0 )
endcase
case 5 :
MoveObjectLocalX( tempObj, 1.0 )
endcase
case 6 :
MoveObjectLocalX( tempObj, -1.0 )
endcase
SetVector3( outDirectionVector, GetObjectWorldX( tempObj ) - objXPos# , GetObjectWorldY( objID ) - objYPos#, GetObjectWorldZ( tempObj ) - objZPos# )
DeleteObject( tempObj )
NormalizeVec3( outDirectionVector )
EndFunction
Function NormalizeVec3( vec3ID as integer )
length as float
length = GetVector3Length( vec3ID )
length = Sqrt( length )
SetVector3( vec3ID, GetVector3X( vec3ID ) / length , GetVector3Y( vec3ID ) / length, GetVector3Z( vec3ID ) / length )
EndFunction
function GetAngleBetweenPositionVectors( vector1 as integer, vector2 as integer)
s# = Sqrt( GetVector3Length( vector1) * GetVector3Length(vector2 ) )
angle# = Acos( GetVector3Dot( vector1, vector2 ) / s# )
endfunction angle#
The coffee is lovely dark and deep,and I have code to write before I sleep.