Align an object to others?

Blitz3D Forums/Blitz3D Programming/Align an object to others?

Pongo(Posted 2004) [#1]
I'm trying to figure out a nice straightforward way to do this. I have four objects at random heights, and I would like to align another objects rotation to them. Basically a simple way to place a car body on wheels that may be at different heights.

Edit: I have updated this to do what I want, but I have to admit I don't understand quite how this works. I took the alignment code from the driver.bb sample. can anyone breakdown what this is doing? Why are there 2 align2vectors? I'm guessing one is x alignment and the other is z, and both are required to prevent rolling around one of the axis. Also, the help specifies a range of 0 to 1, so why is one rate set to 1 and the other to 3?

I'd also like to be able to alter the function to be something like "AlignBody(PercentOfAlignment)"


Thanks for any help

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

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

;create body object
Global body = CreateCube()
ScaleEntity body,3,.5,4
PositionEntity body,0,3,0

;create "wheel" objects
Global lr = CreateCylinder()
EntityColor lr,255,0,0
RotateEntity lr,90,90,0

Global lf = CreateCylinder()
EntityColor lf,0,255,0
RotateEntity lf,90,90,0

Global rr = CreateCylinder()
EntityColor rr,0,0,255
RotateEntity rr,90,90,0

Global rf = CreateCylinder()
EntityColor rf,0,255,255
RotateEntity rf,90,90,0

randomwheelheights()


;create camera And a pivot To rotate around
piv=CreatePivot()
cam = CreateCamera(piv)
PositionEntity cam,0,5,-10
PointEntity cam,piv

sptlight=CreateLight()
PositionEntity sptlight,20,-40,-10

;#################################################################################
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) Then randomwheelheights()
	
	alignbody()

	UpdateWorld()
	RenderWorld()
	
	Text 10,10,"hit space to randomize wheel heights. mouseX rotates camera"
	Text 10,25,"RF:Cyan    RR:Blue    LF:Green    LR:Red"

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

End ; end program


Function randomwheelheights()
	rfheight=Rnd(3)
	rrheight=Rnd(3)
	lfheight=Rnd(3)
	lrheight=Rnd(3)

	PositionEntity lr,-3,lrheight,-4
	PositionEntity lf,-3,lfheight,4
	PositionEntity rr,3,rrheight,-4
	PositionEntity rf,3,rfheight,4
End Function


Function alignbody()
	;align car to wheels
	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
	
	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
End Function




Beaker(Posted 2004) [#2]
Two AlignToVector commands to (as you say) guarantee alignment in all directions. The numbers 1 and 3 on the end aren't the range/rate parameter, but the axis (of alignment) parameter:

1=X
2=Y
3=Z

You can add another parameter in the range 0.0 to 1.0 for a kindof "PercentOfAlignment".

Hope this helps.


Pongo(Posted 2004) [#3]
Ahh.

Thanks,...I misread the help and missed the axis of alignment value.

Makes a lot more sense now.