Align to vector

Blitz3D Forums/Blitz3D Beginners Area/Align to vector

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