Tutorials › dbpro Professional
Vectors
A cone chases a ball along a figure of eight, using Professional's vector commands.
Vectors live in slots
Like objects and images, vectors have numbers. MAKE VECTOR3 creates one (it returns a value
you can ignore), SET VECTOR3 fills it, and X VECTOR3, Y VECTOR3, Z VECTOR3
read it back:
vectors_slots.dba excerptDownload
r = make vector3(1)
set vector3 1, 10, 0, 5
position object 1, x vector3(1), y vector3(1), z vector3(1)From here to there
To go from the chaser to the target, take target − chaser. Its length is the distance; normalised to a length of 1, it is the direction. Multiply by a speed and add it to the position:
vectors_chase.dba excerptDownload
subtract vector3 3, 2, 1 : rem v3 = v2 - v1
distance# = length vector3(3)
normalize vector3 3, 3
multiply vector3 3, 0.8
add vector3 1, 1, 3 : rem v1 = v1 + v3See SUBTRACT VECTOR3, LENGTH VECTOR3, NORMALIZE VECTOR3,
MULTIPLY VECTOR3 and ADD VECTOR3.
vectors.dbaDownload
rem Vectors - OpenDBC tutorial (DarkBASIC Professional)
sync on : sync rate 60
autocam off
color backdrop rgb(14, 16, 28)
rem Vectors live in numbered slots, like objects
r = make vector3(1) : rem position of the chaser
r = make vector3(2) : rem position of the target
r = make vector3(3) : rem direction from chaser to target
make object cone 1, 10 : color object 1, rgb(255, 120, 60)
xrotate object 1, 90 : fix object pivot 1
make object sphere 2, 8 : color object 2, rgb(90, 220, 255)
set vector3 1, 0, 0, 0
position camera 0, 55, -60
point camera 0, 0, 0
do
t = wrapvalue(t + 1)
rem The target moves on a figure of eight
set vector3 2, sin(t) * 40, 0, sin(t * 2) * 20
position object 2, x vector3(2), y vector3(2), z vector3(2)
rem direction = target - chaser, then normalised to length 1
subtract vector3 3, 2, 1
distance# = length vector3(3)
normalize vector3 3, 3
rem Step 0.8 units along the direction unless already there
if distance# > 5
multiply vector3 3, 0.8
add vector3 1, 1, 3
endif
position object 1, x vector3(1), y vector3(1), z vector3(1)
point object 1, x vector3(2), y vector3(2), z vector3(2)
text 10, 10, "Distance: " + str$(distance#, 1)
sync
loopTry this
- Make the chaser slow down as it gets close: scale by
distance# / 50. - Use
DOT PRODUCT VECTOR3to tell whether the target is in front or behind. - Rotate a point with a 4×4 matrix:
MAKE MATRIX4,ROTATE Y MATRIX4andTRANSFORM COORDS VECTOR3.