Vector Help

Blitz3D Forums/Blitz3D Programming/Vector Help

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

here is another simple example of aligning car wheels. hopefully it does not add confusion.

Graphics3D 640,480,0,2
SetBuffer BackBuffer()
SeedRnd MilliSecs()
HidePointer 

Global rfheight#,rrheight#,lfheight#,lrheight#

;create body object
Global body = CreateCube()
ScaleEntity body,5,1,8

;create "wheel" objects
Global lr = CreateWheel(255,0,0) 
Global lf = CreateWheel(0,255,0) 
Global rr = CreateWheel(0,0,255) 
Global rf = CreateWheel(0,255,255) 

randomwheelheights()

;create camera And a pivot To rotate around
piv=CreatePivot()
cam = CreateCamera(piv)
PositionEntity cam,0,50,-200
PointEntity cam,piv
CameraZoom cam,15

sptlight=CreateLight()
RotateEntity sptlight,40,0,0

;#################################################################################
While Not KeyHit(1) ; start main loop
	TurnEntity piv,0,MouseXSpeed(),0 ; turn masterpivot based on mouse x movement
	MoveMouse 320,240 ; center cursor

	If KeyHit(57) Or MouseHit(1) Then randomwheelheights()
	
	alignbody()

	UpdateWorld()
	RenderWorld()
	
	Text 10,10,"hit space or mouseclick to randomize wheel heights. mouseX rotates camera"

	Flip
	Cls
Wend ; end main loop
;#################################################################################
End ; end program

Function CreateWheel(red,green,blue)
temp = CreateCylinder()
EntityColor temp,red,green,blue
RotateEntity temp,90,90,0
ScaleEntity temp,2,1,2
Return temp
End Function 

Function randomwheelheights()
	PositionEntity lr,-4,Rnd(0.0,4),-5
	PositionEntity lf,-4,Rnd(0.0,4),5
	PositionEntity rr,4,Rnd(0.0,4),-5
	PositionEntity rf,4,Rnd(0.0,4),5
End Function

Function alignbody()
	;align car to wheels
	;first get vectors and align for right to left (x axis)
	zx#=(EntityX( rf,True )+EntityX( rr,True ))/2 - (EntityX( lf,True )+EntityX( lr,True ))/2
	zy#=(EntityY( rf,True )+EntityY( rr,True ))/2 - (EntityY( lf,True )+EntityY( lr,True ))/2
	zz#=(EntityZ( rf,True )+EntityZ( rr,True ))/2 - (EntityZ( lf,True )+EntityZ( lr,True ))/2
	AlignToVector body,zx,zy,zz,1,1
	
	;now get vectors and align for front to back (z axis)
	zx#=(EntityX( lf,True )+EntityX( rf,True ))/2 - (EntityX( lr,True )+EntityX( rr,True ))/2
	zy#=(EntityY( lf,True )+EntityY( rf,True ))/2 - (EntityY( lr,True )+EntityY( rr,True ))/2
	zz#=(EntityZ( lf,True )+EntityZ( rf,True ))/2 - (EntityZ( lr,True )+EntityZ( rr,True ))/2
	AlignToVector body,zx,zy,zz,3,1
	
	bodypos=((EntityY(rf)+EntityY(lf)+EntityY(lf)+EntityY(lr)) / 4)+3 ; average height of wheels + offset height
	PositionEntity body,0,bodypos,0
End Function