I've been trying to calculate the angle between two entities for ages without success. I'm using this test code along with Chromo's Vector Maths Lib...
Include "VectorMaths_lib.bb"
src.Vector=New Vector
trgt.Vector=New Vector
Const SCREENX = 800
Const SCREENY = 600
; PositionTexture Example
; -----------------------
Graphics3D SCREENX,SCREENY,32,2
SetBuffer BackBuffer()
update = 1
;#Region setup
mdl_fanTube=LoadMesh("models\XYZ.3ds")
source=CopyEntity (mdl_fanTube)
EntityColor source,255,128,128
target=CopyEntity (mdl_fanTube)
EntityColor target,128,128,255
HideEntity mdl_fanTube
PositionEntity target,40,0,0
cam=CreateCamera()
PositionEntity cam,0,75,0
light1=CreateLight(2)
PositionEntity light1,30,20,50
LightColor light1,10,2,0
light2=CreateLight(2)
PositionEntity light2,-30,50,0
LightColor light2,0,10,2
pivot=CreatePivot()
PositionEntity pivot,0,0,0
PointEntity cam,pivot
;#End Region
While Not KeyDown( 1 )
;#Region Key input
If KeyDown(205)
MoveEntity target,0.25,0,-0
ElseIf KeyDown(203)
MoveEntity target,-0.25,0,-0
EndIf
; If KeyDown(209)
; MoveEntity target,-0,-0.25,0
; ElseIf KeyDown(201)
; MoveEntity target,-0,0.25,0
; EndIf
;
If KeyDown(200)
MoveEntity target,-0,-0,0.25
ElseIf KeyDown(208)
MoveEntity target,-0,0,-0.25
EndIf
If KeyDown(72) ;Num pad up
TurnEntity source,1,0,0
ElseIf KeyDown(80) ;num pad down
TurnEntity source,-1,0,0
EndIf
If KeyDown(75) ;Num pad left
TurnEntity source,0,1,0
ElseIf KeyDown(77) ;num pad right
TurnEntity source,0,-1,0
EndIf
If KeyDown(199) ;Home up
TurnEntity target,1,0,0
ElseIf KeyDown(207) ;End down
TurnEntity target,-1,0,0
EndIf
If KeyDown(211) ;Delete left
TurnEntity target,0,1,0
ElseIf KeyDown(209) ;Page Down right
TurnEntity target,0,-1,0
EndIf
;#End Region
src\x=0
src\y=0
src\z=1
TFormNormal src\x,src\y,src\z,0,source
src\x=TFormedX()
src\y=TFormedY()
src\z=TFormedZ()
trgt\x=EntityX(target)-EntityX(source)
trgt\y=EntityY(target)-EntityY(source)
trgt\z=EntityZ(target)-EntityZ(source)
Vector_Normalize (trgt)
dot#=Vector_DotProduct(src,trgt)
RenderWorld
Text 0,0,"current ="
Text 0,12,"X size =" + src\x
Text 0,24,"Y size =" + src\y
Text 0,36,"Z size =" + src\z
Text 0,52,"X size =" + trgt\x
Text 0,64,"Y size =" + trgt\y
Text 0,76,"Z size =" + trgt\z
Text 0,104,"Angle =" + dot#
Flip
Wend The idea is to calculate the angle of Target relative to Source's Z axis.
I'm using test models that are arrows pointing in X,Y, and Z axis so I know which direction the entities point. When I move the entities they travel in the right direction. eg. Moving source in X direction moves it in the direction it's X axis is pointing.
Though the angle dot# works correctly with the source object in default position, rotating the source object makes the result 'screwy'. I've noticed that when I turn the source object around the Y axis so that the source object's Z axis points up-right on the screen, the value for X from the normalized transformed vector (src\x=TFormedX()) is negative, but right on my screen is positive as far as entity movement is concerned. It seems as though TFormNormal flips the X direction.
Any clues or advice grately appreciated!
|