Can't calculate angle between entities :(

Blitz3D Forums/Blitz3D Programming/Can't calculate angle between entities :(

Shifty Geezer(Posted 2004) [#1]
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!


jfk EO-11110(Posted 2004) [#2]
How about to parent an entity to an other entity, then check the ENtityPitch etc. with the LOCAL flag, then unparent it again. Well, I didn't test it, but I guess it maybe works.


GfK(Posted 2004) [#3]
Use DeltaPitch and DeltaYaw.


Jeppe Nielsen(Posted 2004) [#4]
I think you need to transfer your dot# variable into an angle like this:

Text 0,104,"Angle =" + Acos(dot#)



Shifty Geezer(Posted 2004) [#5]
When I say angle, I'm only trying to find whether the target is within a cone and how much. I don't need the result in degrees and don't need to know whether it's left/right of the Z axis; only how far it's off from in line. Dot product is the simplest and most effective way to do this and the way used in 3D all over the place, so I'm thinking there must be a way to use dot products in Blitz.

Can anyone explain why the X value for the Z direction is a negative when it actually points in the positive direction? Maybe understanding this will help me find where my code's going wrong?


Floyd(Posted 2004) [#6]
A superficial look at your code suggests that you have source and dest reversed in the call to TFormNormal.


gpete(Posted 2004) [#7]
Is this needed for some sort of targeting method--- perhaps for a weapon with limited traverse?


Jeppe Nielsen(Posted 2004) [#8]
Ok, try this code in the archives:

http://www.blitzbasic.co.nz/codearcs/codearcs.php?code=871


Shifty Geezer(Posted 2005) [#9]
Thanks all. Got it working with corrections to the transform code.