Vector Help

Blitz3D Forums/Blitz3D Programming/Vector Help

What is a vector? Please, if you can, explain it in low-level-detail.

It's a geometric entity with both length and a direction. It's usually visualized as a straight line with an arrow on the end.

http://en.wikipedia.org/wiki/Euclidean_vector

Ok, thanks. So what's the deal with align to vector then? Could you give me an example?

Better yet.....could you just explain this part of the 'driver' demo.

	;align car to wheels
	zx#=(EntityX( wheels[2],True )+EntityX( wheels[4],True ))/2
	zx=zx-(EntityX( wheels[1],True )+EntityX( wheels[3],True ))/2
	zy#=(EntityY( wheels[2],True )+EntityY( wheels[4],True ))/2
	zy=zy-(EntityY( wheels[1],True )+EntityY( wheels[3],True ))/2
	zz#=(EntityZ( wheels[2],True )+EntityZ( wheels[4],True ))/2
	zz=zz-(EntityZ( wheels[1],True )+EntityZ( wheels[3],True ))/2
	AlignToVector car,zx,zy,zz,1,.2
	
	zx#=(EntityX( wheels[1],True )+EntityX( wheels[2],True ))/2
	zx=zx-(EntityX( wheels[3],True )+EntityX( wheels[4],True ))/2
	zy#=(EntityY( wheels[1],True )+EntityY( wheels[2],True ))/2
	zy=zy-(EntityY( wheels[3],True )+EntityY( wheels[4],True ))/2
	zz#=(EntityZ( wheels[1],True )+EntityZ( wheels[2],True ))/2
	zz=zz-(EntityZ( wheels[3],True )+EntityZ( wheels[4],True ))/2
	AlignToVector car,zx,zy,zz,3,.2


Essentially, that is using the position of each of the wheels to calculate a vector which runs from one wheel to another. It then aligns the body of the car so that it matches that vector. Or in even simpler terms, it finds out where the wheels are and lines the body up so that it "fits" on top of them.

Vectors can be used for, a basic example, asteroids, or more complicated, a collision.

In the game asteroids, the ship has a speed, and a direction it's going in.

A vector can be normalised because 0 and 1. You can obtain something like a vector, be plugging an angle value (the direction your space craft is travelling), into SIN and COS

eg,

speed = 2
angle = 25

x_move = cos(angle) * speed
y_move = sin(angle) * speed

x_pos = x_pos + x_move
y_pos = y_pos + y_move

oval x_pos, y_pos, 2, 2

will draw an oval, travelling at an angle of 25, and moving at a speed of 2. It will basically move 2 pixels per frame. That would be a basic example of a vector.

Cool Ross C. So gabriel, when you said
that is using the position of each of the wheels to calculate a vector which runs from one wheel to another.
What does zx,zy,and zz mean? (what is their abbreviation), and possibly could you explain the parameters of AlignToVector in detail?

Please answer that one, because i'm still unsure about what that does too.

Aligntovector has the x y and z of the first point and the x y and z of the second point so remember a little arrow between those two points. Then imagin the shape and its x axis if you make the last parmenter 1 (i think) it will align the x axis and the shape to the line I told you to remember.

hope this helped :)

I think it helped :| So with the
zx#=(EntityX( wheels[2],True )+EntityX( wheels[4],True ))/2
zx=zx-(EntityX( wheels[1],True )+EntityX( wheels[3],True ))/2


AlignToVector stores that calculation/proceedure and uses it to make a vector?

zx,zy,zz is a vector.

Oh. Thanks big10p.

In each step, Mark totals the X, Y and Z positions of two wheels and divides by two. In other words, he finds the average position of two wheels. He does this because there are two sets of wheels on a car.

For example, let's do the tilting from left to right. You have the front axis, which has the left-front and right-front wheels on it. But you also have the back axis. You can't really align the car to only one - the back wheels would be off the ground if you align it to the front wheels - so in order to find the best alignment, you calculate the average between the two.

Each component is calculated separately. I have no idea why he prefixes everything with z, but zx, zy and zz are the x, y and z components of the vector.

Now you also asked to explain precisely what the AlignToVector command does? It takes the axis you specify in the fourth parameter ( x, y or z ) and orients the entity so that the axis you specify is pointing along the line of the vector you pass as the first three parameters.

Again, an example. Let's suppose you have a missile, and it's modelled such that it points down the Z axis. If you now say

AlignToVector(1,0,0,3)

Your vector is (1,0,0) so it points to the right. Your axis is the z axis. It now aligns your missile so that the z axis points along that vector, which is also equivalent to the xaxis. So now your missile, which was pointing down the z axis ( into the screen ) is now pointing along the x axis ( to the right. )

That's as well as I can explain it without pictures.

Nice one :o)

Thanks :)

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


Ok, it's starting to help ALOT =)