Entity Vector facing

Blitz3D Forums/Blitz3D Programming/Entity Vector facing

_PJ_(Posted 2010) [#1]
Most probably know by now I'm a real moron when it comes to the 3D math side of things. I'm confident that at least one of the 3D functions can do this, but I'm asking here because, well, I'm laszy and way too stressed with trying to get around the weirdness of gimbal-locks and non 0-360 rotations when Pitch and Roll are both involved.

What I am trying to get, is a means of acquiring an entities global/universal facing (orientation) in terms of a set of vectors.
The vector dimensins based on the global coordinate space, so that X refers to the global X dimension, Y to the vertical and Z to the default fowaards/backwards.
Something like TFormVector or ?

Of course, Localised vectors would always be pointing 0,0,0 since an entity cannot face any other direction than that which it is facing, right? :)

So, if I had something nice and simple like a Primnitive cone (I choswe a cone cos it's easier to see where it's pointed) for now, we'll assume the cone's facing is actually along the axis of the point, in the Y dimension if the cone is stood upright. This differs from the default when CreateCone() is used, but bear with me :)

Now, if that cone is tilted forwards (pitch) 90 degrees, so it now lies parallel to the 'ground', its facing will be in the Z direction (assuming X / Yaw has been 0 all along)

What I want, is a means of identifying the orientation as a vector, so for the cone now, it would be something like:

X=0,Y=0,Z=1

Where the Z=1 means in a positive direction along the Z axis.

In the same way, if the cone was turned back through it's 90 degrees back to where it began, the vectors would be:

X=0, Y=1, Z=0

If the Cone was again tilted forwards 90degrees (pitch) so that it was once more parallel to the ground, but then rotated 90degrees ANTICLOCKWISE (Yaw), the vectors would indicate:

X=-1, Y=0, Z=0

However, if it was turned now clockwise but through only 45Degrees (Yaw), then the vectors would show:

X=0-0.5, Y=0, Z=0.5

Sorry if I've waffled on a bit, I just wanted to make sure I exlained what I wanted.


Floyd(Posted 2010) [#2]
Assuming you know the direction of interest within the original entity then it is just a matter of using TFormVector. In your example that vector was (0,1,0). The default "forward" vector is (1,0,0).

Anyway, if (x,y,z) is the vector in the Entities space you use

TFormVector x,y,z, source_entity, 0

to transform from entity space into world space. Note 0 is the magic number to indicate world rather than a typical entity.


_PJ_(Posted 2010) [#3]
By direction of interest, I think that I would mean the actual "frontfacing" of the entity according to Blitz, i.e. the direction the entity is actually facing in Blitz terms...
I'm not entirely sure I get it yet, but I'll play around with TFormVector for a bit and see i I can see it working, if I still get stuck I'll come bak :) Thanks!


_PJ_(Posted 2010) [#4]
I'm certainly missing something...
Aside from the fact that the below code doesn;t actually seem to do anything, I think I'm mainly puzzled about how to tell TFormPoint that I want to consider the current forward direction of the entity in question.

; Test TFormVector

;Setup the scene
Graphics3D 1024,768,32,6
SetBuffer BackBuffer()

AmbientLight 128,128,128

Global Camera=CreateCamera(False)
CameraClsColor Camera,32,64,192
MoveEntity Camera,0,2,0

Global Ground=CreatePlane(8,False)
EntityColor Ground,32,160,0

Global Cone=CreateCone(3,True,False)
PositionEntity Cone,0,1,6,True
EntityColor Cone,160,160,192
ScaleMesh Cone,0.67,1,0.67
RotateMesh Cone,90,0,0 ; This ensures Cone is "pointing" along the Z Axis by default (I hope)

Global Sun=CreateLight(True)
PositionEntity Sun,-50,50,0,True
LightColor Sun,192,224,96
PointEntity Sun,Cone

Global Vectors[3]

While Not KeyDown(True)

	GetMovement(Cone)
	If KeyDown(57) Then ShowVectorKey(Cone)
	DisplayData(Cone)
VWait(60)
Flip 

Wend

ClearWorld
EndGraphics
End

Function DisplayData(Entity)
	
	TFormPoint 0,0,1,Cone,False
	Local XVector=0-(TFormedX)
	Local YVector=TFormedY
	Local ZVector=TFormedZ
	
	UpdateWorld
	RenderWorld
	
	Color 0,0,0
	Rect 1,1,1022,94,True
	Color 255,255,255
	Rect 0,0,1024,96,False
	Text 32,32,"Pitch: "+Str(Int(EntityPitch(Entity)))+" Yaw: "+Str(Int(EntityYaw(Entity)))+" Roll: "+Str(Int(EntityRoll(Entity)))
	Color 255,0,0
	Text 32,64,"X Vector: "+Str(Int(XVector))

	Color 0,255,0
	Text 288,64,"Y Vector: "+Str(Int(YVector))
	
	Color 0,0,255
	Text 656,64,"Z Vector: "+Str(Int(ZVector))
End Function

Function ShowVectorKey(Entity)

	Local IterVectors

	If (Not(Vectors[0]*Vectors[1]*Vectors[2]))
		
		For IterVectors=1 To 3
			Vectors[IterVectors-1]=CreateCylinder(4,True,Camera)
			ScaleMesh Vectors[IterVectors-1],0.1,0.5,0.1
			RotateMesh Vectors[IterVectors-1],90,0,0				;Same as with cone, we want the vector 'bars' to "point forwards" rather than "upwards"
			RotateEntity Vectors[IterVectors-1],-90*(IterVectors=2),-90*((IterVectors=1)+(90*IterVectors=3)),0,True	;Now we point the Bars to the correct relative facing
			EntityColor Vectors[IterVectors-1],(IterVectors=1) Shl 8,(IterVectors=2) Shl 8,(IterVectors=3) Shl 8	; Red=X green = Y Blue = Z
			PositionEntity Vectors[IterVectors-1],EntityX(Entity,True),EntityY(Entity,True),EntityZ(Entity,True),True
			MoveEntity Vectors[IterVectors-1],0,0,1				;Prevent the bars from being stuck inside our parent mesh
		Next
	End If

End Function			
	
Function GetMovement(Entity)
	If MouseDown(1) Then MoveEntity Entity,0,0,1
	If MouseDown(2) Then MoveEntity Entity,0,0,-1
	TurnEntity Entity,KeyDown(200)-KeyDown(208),KeyDown(205)-KeyDown(203),0
End Function	
		



Stevie G(Posted 2010) [#5]
1. You haven't declared XVector , YVector etc.. as floats.

2. Remove the str(int( )) from the text display otherwise you won't see the correct results.

3. Tformedx, y and z are function returns so require brackets, i.e. tformedx(), tformedy()

Stevie


Floyd(Posted 2010) [#6]
Also TFormPoint should be TFormVector.

A vector, which you can visualize as an arrow, has direction and length. If an entity is positioned at (0,0,0) then TFormPoint and TFormVector amount to the same thing. After moving the entity, leaving orientation unchanged, points are different but vectors remain the same.

Also note that I said the vector (1,0,0) was forward. In fact the forward direction is Z+, so that should have been (0,0,1). I'm claiming temporary dyslexia.


Matty(Posted 2010) [#7]
As others have said:

TFormVector 0,0,1,MyEntity,0

will give you the vector in world space that represents the 'forwards facing' vector of the entity.

The TForm commands (and AlignToVector) are very useful.


_PJ_(Posted 2010) [#8]
Ah IDEal and it's autocomplete sytrikes again. Thansk all.

I purpoisefully kept the values to int's just so it was easier to see the change between 0, 1 etc. though in a 'working version' yes, I will use the raw floats.


Also note that I said the vector (1,0,0) was forward. In fact the forward direction is Z+, so that should have been (0,0,1). I'm claiming temporary dyslexia.

I think that confused me alittle at first, but guessed that it perhaps should have been 0,0,1 Yay me! :) But thank you for clarifying. We all get that temprorary dyslexia, me more than most I think! My keyboard can't spell at all :P

____________________________

why are tese mistakes so obvious with hindsight? :S
---------------------------

Anywho... it works beautifully and now does precisely what it says on the tin!


; Test TFormVector

;Setup the scene
Graphics3D 1024,768,32,6
SetBuffer BackBuffer()

AmbientLight 128,128,128

Global Camera=CreateCamera(False)
CameraClsColor Camera,32,64,192
MoveEntity Camera,0,2,0

Global Ground=CreatePlane(8,False)
EntityColor Ground,32,160,0

Global Cone=CreateCone(3,True,False)
PositionEntity Cone,0,1,6,True
EntityColor Cone,160,160,192
ScaleMesh Cone,0.67,1,0.67
RotateMesh Cone,90,0,0 ; This ensures Cone is "pointing" along the Z Axis by default (I hope)

Global Sun=CreateLight(True)
PositionEntity Sun,-50,50,0,True
LightColor Sun,192,224,96
PointEntity Sun,Cone

While Not KeyDown(True)

	GetMovement(Cone)
	DisplayData(Cone)
VWait(60)
Flip 

Wend

ClearWorld
EndGraphics
End

Function DisplayData(Entity)
	
	TFormVector 0,0,1,Cone,False
	Local XVector#=TFormedX()
	Local YVector#=TFormedY()
	Local ZVector#=TFormedZ()
	
	UpdateWorld
	RenderWorld
	
	Color 0,0,0
	Rect 1,1,1022,94,True
	Color 255,255,255
	Rect 0,0,1024,96,False
	Text 32,32,"Pitch: "+Str(Int(EntityPitch(Entity)))+" Yaw: "+Str(Int(EntityYaw(Entity)))+" Roll: "+Str(Int(EntityRoll(Entity)))
	Color 255,0,0
	Local sString$=DisplayFloatString(XVector#)
	Text 32,64,"X Vector: "+sString$

	Color 0,255,0
	sString$=DisplayFloatString(YVector#)
	Text 288,64,"Y Vector: "+sString$
	
	Color 0,0,255
	sString$=DisplayFloatString(ZVector#)
	Text 656,64,"Z Vector: "+sString$
End Function
	
Function GetMovement(Entity)
	If MouseDown(1) Then MoveEntity Entity,0,0,1
	If MouseDown(2) Then MoveEntity Entity,0,0,-1
	TurnEntity Entity,KeyDown(200)-KeyDown(208),KeyDown(205)-KeyDown(203),0
End Function	
	
Function DisplayFloatString$(fFloat#)
	Local sReturn$=Str(fFloat#)
	Local nDec=Instr(sReturn$,".")
	If (nDec)
		If (Len(sReturn$))>(nDec+2) Then sReturn=Left(sReturn,nDec+2)
	End If
	Return sReturn$
End Function


You've all been a great help.