Align to vector

Blitz3D Forums/Blitz3D Beginners Area/Align to vector

Boyd600(Posted 2006) [#1]
Hi, I have been using the AlignToVector command to point my camera in my program and after lots of mashing on my controls i noticed the camera seems slightly out of whack, are there any rounding errors i need to be aware of when using this command?

Hope that's readable, I'm not very good at this Internet forum malarkey


Stevie G(Posted 2006) [#2]
Never had an issue with align to vector. Do you have a huge world( in terms of blitz units ) as this can lead to floating point inaccuracies in some situations? Can you show us the basic code you're using?

Stevie


Boyd600(Posted 2006) [#3]
Hi thanks for the quick reply

 Function Update()

	CamX# = EntityX(MainCam, 1)
	CamY# = EntityY(MainCam, 1)
	CamZ# = EntityZ(MainCam, 1)
	
	Ship1X# = EntityX(Ship1, 1)
	Ship1Y# = EntityY(Ship1, 1)
	Ship1Z# = EntityZ(Ship1, 1)
	
	PositionEntity( SkyBox, CamX#, CamY#, CamZ#,1)
	PositionEntity( SkyBox2, CamX#, CamY#, CamZ#,1)

	TFormVector (0,0,1, SPivot, 0)	
	SpX# = TFormedX()
	SpY# = TFormedY()											; Align Ship to +Z of SPivot
	SpZ# = TFormedZ()
	AlignToVector (Ship1, SpX#, SpY#, SpZ#, 3, 0.1)				
			
	TFormVector (0,1,0, SPivot, 0)	
	SpX# = TFormedX()
	SpY# = TFormedY()											; Align Ship to +Y of SPivot
	SpZ# = TFormedZ()	
	AlignToVector (Ship1, SpX#, SpY#, SpZ#, 2, 0.1)
			
	TFormVector (0,1,0, CPivot, 0)	
	SpX# = TFormedX()
	SpY# = TFormedY()											; Align Cam to +Y of CPivot
	SpZ# = TFormedZ()	
	AlignToVector (MainCam, SpX#, SpY#, SpZ#, 2, 0.1 )
	
	TFormVector (0,0,1, CPivot, 0)	
	SpX# = TFormedX()
	SpY# = TFormedY()											; Align Cam to +Z of CPivot
	SpZ# = TFormedZ()

	AlignToVector (MainCam, SpX#, SpY#, SpZ#, 3, 0.1 )
	
	
	ToPivX# = EntityX(TPivot, 1) - EntityX(MainCam, 1)
	ToPivY# = EntityY(TPivot, 1) - EntityY(MainCam, 1)			; Get Vector to TPivot
	ToPivZ# = EntityZ(TPivot, 1) - EntityZ(MainCam, 1)


	AlignToVector (CPivot, ToPivX#, ToPivY#, ToPivZ#, 3, 1)		; Point Rest Pivot at TPivot

		
	AlignToVector (CamPiv, ToPivX#, ToPivY#, ToPivZ#, 3, 1)	    ; Point CamPiv at TPivot
	
	SmoothMove ( CamPiv, CPivot, 0.1 )							; Move Camera to rest smoothly
	
	; Ship Movement
	MoveEntity (SPivot, 0, 0, SSpeed#)
	PositionEntity ( Ship1,EntityX(SPivot,1),EntityY(SPivot,1),EntityZ(SPivot,1),1 )
	
End Function 


this is a small snippet from my code (its a bit of a sprawling mess) im using pivots to position my camera behind my ship and the camera is pointed towards another pivot infront (TPivot)

I hope this makes sense

[Edit] oh just to add my world isnt very big its just a sky sphere and my ship floating about the world origin point


Stevie G(Posted 2006) [#4]
Do you really need to align the camera to all 3 axis'? If you're trying to simlate pointentity but without potential gimbal lock then aligntovector on the z-axis will acheive this using quarternions.

I'm not sure what your looking to acheive but the dodgy camera is probably as a result of aligning slowly to all 3 axis'. You could probably just get away with 2 .... the yaw/y and pitch/x axis or ...1 as above.

Hope this helps.
Stevie


DJWoodgate(Posted 2006) [#5]
Not sure by what you mean as "out of whack" however there are rounding errors on everything that uses floating point maths. If there is drift in the matrix values due to aligntovector doing its thing on multiple axis then I would try RotateEntity cam,0,0,0 : ScaleEntity cam,1,1,1 every so often and see if it helps. Do the same for the camera pivot if the camera is parented to it. My thinking is that if it starts out working and gradually drifts off then this may be due to scaling issues. Could be something else entirely of course.


Boyd600(Posted 2006) [#6]
Thanks my codes a complete mess at the moment i really should be designing it before have rather than tacking bits on.

Im trying to align my camera Y vector with that of my target pivot so the camera is always pointing up in relation to my ship after a roll, im sure you have already gathered that, is there a better way of doing this?

Thanks alot for your help


Stevie G(Posted 2006) [#7]
I think you'd need to explain a bit about how you control the ship for me or others to help. A working code snipped .. even using a cone for the ship would be very handy.

Stevie


Boyd600(Posted 2006) [#8]
Okie ive hacked out the code the controls are commented, try turning the 'Ship' and rolling left and right at random then let the camera settle, after a bit it settles skewed.

Graphics3D  800, 600, 32
SetBuffer BackBuffer()

Global MainCam = CreateCamera()

; Global Camera Coords and info
Global CamX#    = 0
Global CamY#	= 0
Global CamZ#	= 0
Global CamZoom# = 1.5

; Globals for Ship (Player)
Global Ship1				; Test ship
Global Ship1X#	=0			; Ship X position
Global Ship1Y#	=0			; Ship Y position
Global Ship1Z#	=0			; Ship Z position
Global SThrust#	=0			; Ships Set Speed
Global SSpeed# 	=0			; Ships Current Speed
Global SAcc#    =0			; Ships Rate of Acceleration

; Globals for Pivots
Global SPivot				; Ships Central Pivot
Global TPivot				; Target pivot (both ship and cam points to this)
Global CPivot				; Camera pivot (This is where the camera will rest at)
Global CamPiv				; Camera Movement pivot (This the pivot the camera will be stuck to)
Global MPivot				; Pivot for mouse movement

TestShip()

; Main Game Loop
While Not KeyHit(1)														; Start Main Game Loop
		
	Keys()														
	
	Update()															
	

    RenderWorld()									
	Flip 															
	
Wend																


End																	

; Create ship for control and tracking tests
Function TestShip()
	
	SPivot = CreatePivot ()						; Create Ship Central Pivot
	PositionEntity (SPivot, 0, 0, 0)
	
	Ship1 = CreateCube(SPivot)
	ScaleEntity (Ship1,1,1,2)	
		
	TPivot = CreatePivot (SPivot)						; Create Target Pivot
	PositionEntity (TPivot, 0, 0, 40, 1)				; Position Target in front of ship

	
	PositionEntity ( MainCam,  0, 5,-20, 1 )			; Position Camera behind ship
	CamPiv = CreatePivot()								; Create Camera Current Pivot
	PositionEntity (CamPiv, 0, 5,-20, 1)				; Position Behind Camera	
	EntityParent (MainCam, CamPiv)						; Parent Camera to Pivot


	CPivot = CreatePivot (TPivot)						; Create Camera Rest Pivot
	PositionEntity (CPivot, 0, 5,-20, 1)				; Position Camera Rest Pivot
		
	MPivot = CreatePivot()								; Create 3D Mouse Position Pivot
	PositionEntity (MPivot, 0,0,40, 1)					; Move to +40 on Z Axis
	
	; Set Ships Stats (Test)
	SAcc# = 0.01										; Rate of Acceleration
	
End Function

; Update Ship, Camera and Background
Function Update()

	CamX# = EntityX(MainCam, 1)
	CamY# = EntityY(MainCam, 1)
	CamZ# = EntityZ(MainCam, 1)
	
	Ship1X# = EntityX(Ship1, 1)
	Ship1Y# = EntityY(Ship1, 1)
	Ship1Z# = EntityZ(Ship1, 1)
		

	TFormVector (0,0,1, SPivot, 0)	
	SpX# = TFormedX()
	SpY# = TFormedY()											; Align Ship to +Z of SPivot
	SpZ# = TFormedZ()
	AlignToVector (Ship1, SpX#, SpY#, SpZ#, 3, 0.1)				
			
	TFormVector (0,1,0, SPivot, 0)	
	SpX# = TFormedX()
	SpY# = TFormedY()											; Align Ship to +Y of SPivot
	SpZ# = TFormedZ()	
	AlignToVector (Ship1, SpX#, SpY#, SpZ#, 2, 0.1)
			
	TFormVector (0,1,0, CPivot, 0)	
	SpX# = TFormedX()
	SpY# = TFormedY()											; Align Cam to +Y of CPivot
	SpZ# = TFormedZ()	
	AlignToVector (MainCam, SpX#, SpY#, SpZ#, 2, 0.1 )
	
	TFormVector (0,0,1, CPivot, 0)	
	SpX# = TFormedX()
	SpY# = TFormedY()											; Align Cam to +Z of CPivot
	SpZ# = TFormedZ()

	AlignToVector (MainCam, SpX#, SpY#, SpZ#, 3, 0.1 )
	
	
	ToPivX# = EntityX(TPivot, 1) - EntityX(MainCam, 1)
	ToPivY# = EntityY(TPivot, 1) - EntityY(MainCam, 1)			; Get Vector to TPivot
	ToPivZ# = EntityZ(TPivot, 1) - EntityZ(MainCam, 1)


	AlignToVector (CPivot, ToPivX#, ToPivY#, ToPivZ#, 3, 1)		; Point Rest Pivot at TPivot

		
	AlignToVector (CamPiv, ToPivX#, ToPivY#, ToPivZ#, 3, 1)	    ; Point CamPiv at TPivot
	
	SmoothMove ( CamPiv, CPivot, 0.1 )							; Move Camera to rest smoothly
	
	; Ship Movement
	MoveEntity (SPivot, 0, 0, SSpeed#)
	PositionEntity ( Ship1,EntityX(SPivot,1),EntityY(SPivot,1),EntityZ(SPivot,1),1 )
	
End Function

; Function for all key/mouse commands 
Function Keys()
		
	; Ship Controls
	If KeyDown(200) TurnEntity (SPivot, 2,0,0)								; Up:    Increase Pitch		
	If KeyDown(208) TurnEntity (SPivot, -2,0,0)								; Down:  Decrease Pitch
	If KeyDown(203) TurnEntity (SPivot, 0,2,0)								; Left:  Increase Yaw  (want to add tilt to pitch -
	If KeyDown(205) TurnEntity (SPivot, 0,-2,0)								; Right: Decrease Yaw   turns for coolness)
	If KeyDown(16)  TurnEntity (SPivot, 0,0,2)								; Q:	 Increase Roll
	If KeyDown(18)  TurnEntity (SPivot, 0,0,-2)								; E:     Decrease Roll	
	
	; Camera Controls
	If KeyDown(201) Then CamZoom = CamZoom + 0.1							; PageUp:   Increase Zoom
	If KeyDown(209) And CamZoom > 1.0 Then CamZoom = CamZoom - 0.1			; PageDown: Decrease Zoom 
	If KeyDown(199) Then CamZoom = 1.8										; Home:     Reset Zoom
	
	If CamZoom > 1.0 Then CameraZoom (MainCam, CamZoom )				; Check Zoom no less than 1.0
	If CamZoom < 1.0 Then CamZoom = 1.0
		
	; Test Move cam back and forward
	If KeyDown(210) Then MoveEntity(MainCam, 0,0,0.1)						; Insert: Move Cam forward
	If KeyDown(211) Then MoveEntity(MainCam, 0,0,-0.1)						; Delete: Move Cam back
	
	If KeyDown(30)  And SThrust# < 5.0										; A:      Increase Thrust
		SThrust# = SThrust# + SAcc#
	EndIf
	
	If KeyDown(44) And SThrust# > 0.0										; Z:      Decrease Thrust
		SThrust# = SThrust# - SAcc#
	EndIf
	
	If SSpeed# < SThrust# Then SSpeed# = SSpeed# + SAcc#
	
	If SSpeed# > SThrust# Then SSpeed# = SSpeed# - SAcc#
	


End Function

;Function for smooth movement from point A - B
Function SmoothMove (Ent1, Ent2, Speed#) ; Ent1 is object i want to move, Ent2 is target

	X1# = EntityX(Ent1, 1)			; Object to moves coords
	Y1# = EntityY(Ent1, 1)
	Z1# = EntityZ(Ent1, 1)
	
	X2# = EntityX(Ent2, 1)			; Target coords
	Y2# = EntityY(Ent2, 1)
	Z2# = EntityZ(Ent2, 1)
	
	Xinc# = X2# - X1# 
	Yinc# = Y2# - Y1#
	Zinc# = Z2# - Z1# 
	
	TranslateEntity (Ent1, Xinc# * Speed# , Yinc# * Speed# , Zinc# * Speed# )
	
End Function


Thanks again, very good of you to help


Ross C(Posted 2006) [#9]
Try not to use TurnEntity. Use:

RotateEntity entity, EntityPitch(entity) + x_rot , EntityYaw(entity) + y_rot, EntityRoll(entity) + z_rot


x_rot, y_rot and z_rot being the amount you wish to turn your craft by. Turn entity has a different way of rotating a mesh, than rotateentity. Turn entity sucks really...


Ross C(Posted 2006) [#10]
I don't really see much of a problem with that code really... Limme look again


DJWoodgate(Posted 2006) [#11]
Well it is complicated. My guess is the problem is because you align +Z and +Y of the camera to Cpivot and then go on to align +Z of Campiv (which the camera is parented to) to Tpivot. I think this gives the X axis a bit too much freedom and hence the roll. (If you use mouse inputs for turning you can make the roll wind and unwind by moving the mouse in clockwise or anticlockwise circles). In any event there is a conflict there that you need to resolve.

BTW. Turnentity might suck, but at least it avoids gimbal lock. Maybe it slerps.


Boyd600(Posted 2006) [#12]
Thanks for the help everyone i will see if i can sort it out tonight


Boyd600(Posted 2006) [#13]
Looks like i got it to work, ive now aligned the CPivots +y axis (the cameras rest position) to the TPivots +y axis (the Target 'Look at' pivot) and that seems to stop and skewing.

Im sure there must be better ways to do this, but if it plays ok its ok with me hehe

Thanks everyone for your help


Sir Gak(Posted 2006) [#14]
Sometimes, a solution is best if it is what works; even if it is not the BEST way to do it, a workable way will do until you have completed the program and want to to go back and tweak / optimize it for better ways.