Here is a simple example of vectors, in my crappy version of pong
Rem Project: Pong!
Rem Created: 28/11/2002 15:46:48
Rem ***** Main Source File *****
rem Standard Setup Code
set window on
sync on : sync rate 30
color backdrop RGB(0,0,0) : hide mouse
set text font "arial" : set text size 12 : set text transparent
rem setup objects!
make object box 1, 100, 1, 1
make object box 2, 100, 1, 1
make object box 3, 2, 1, 8
make object box 4, 1, 1, 51
make object sphere 5, 1.6
rem place objects!
position object 1, 50, 0, 50
position object 2, 50, 0, 0
position object 3, 0.5, 0, 25
position object 4, 100.5, 0, 25
position object 5, 50, 0, 25
rem make camera
position camera 0, 50, 65, 25
point camera 0, 50, 0, 25
rem set ball vector
rf=make vector2(ball)
set vector2 ball, -1.1, 0.2
rem main loop
do
rem controls
if upkey()=1 then move object 3, 1.5
if downkey()=1 then move object 3, -1.5
rem limit paddle to arena
if object position z (3) > 45.5 or object position z (3) < 4.5
if object position z (3) > 45.5 then z#=45.5
if object position z (3) < 4.5 then z#=4.5
position object 3, 0.5, 0.0, z#
endif
rem collide ball
object_hit=object collision(5,0)
if object_hit>0
if object_hit=1
set vector2 ball, x vector2(ball)*1.05, y vector2(ball)*-1.1
endif
if object_hit=2
set vector2 ball, x vector2(ball)*1.05, y vector2(ball)*-1.1
endif
if object_hit=3
set vector2 ball, x vector2(ball)*-1.05, y vector2(ball)*1.2
endif
if object_hit=4
set vector2 ball, x vector2(ball)*-1.05, y vector2(ball)*1.2
endif
endif
rem move ball
position object 5, object position x(5)+ x vector2(ball), 0.0, object position z(5) + y vector2(ball)
rem out of limits ball
if object position x(5) < -2.0 or object position x(5) > 110.0 or object position z(5)> 55.0 or object position z(5)< -5.0
position object 5, 50, 0, 25
set vector2 ball, -1.2, 0.2
endif
sync
loop
Heres a little more complex usage of vectors:-
Rem Project: Lightmap test 1
Rem Created: 01/12/2002 09:25:18
Rem ***** Main Source File *****
rem Standard Setup Code
sync on : sync rate 0
color backdrop rgb (0,0,0)
set text font "arial" : set text size 12 : set text transparent
for obj=1 to 2
if obj=1
VXa#=0.0 : VYa#=0.0 : VZa#=8.0
VXb#=0.0 : VYb#=0.0 : VZb#=0.0
VXc#=8.0 : VYc#=0.0 : VZc#=8.0
endif
if obj>1
VXa#=8.0 : VYa#=0.0 : VZa#=0.0
VXb#=8.0 : VYb#=0.0 : VZb#=8.0
VXc#=0.0 : VYc#=0.0 : VZc#=0.0
endif
gosub Create_lightmap
next obj
rem setup camera
position camera 0, 4.0, 8.0, 4.0
point camera 0, 4.0, 0.0, 4.0
rem view loop
do
sync
loop
Create_lightmap:
rem setup all key variables
LX#=5.5 : LY#=2.0 : LZ#=8.0 :`Light 1 position
rem lightmap size
sze#=64
rem make vectors
moo=make vector3(edgeA)
moo=make vector3(edgeB)
moo=make vector3(trinorm)
moo=make vector3(trinormV)
moo=make vector3(light)
moo=make vector3(lightN)
rem make object
make object triangle obj, VXa#, VYa#, VZa#, VXb#, VYb#, VZb#, VXc#, VYc#, VZc#
rem texure object
if VZa#<VZb#
scale object texture obj, -1.0, 1.0
endif
if VZa#>=VZb#
scale object texture obj, 1.0, -1.0
endif
rem make bitmap
create bitmap 1, sze#, sze#
rem calculate required variables
edgeAL#= SQRT((VXb#-VXa#)^2 + (VYb#-VYa#)^2 + (VZb#-VZa#)^2)
edgeBL#= SQRT((VXc#-VXa#)^2 + (VYc#-VYa#)^2 + (VZc#-VZa#)^2)
rem setup vectors
set vector3 edgeA, VXb#-VXa#, VYb#-VYa#, VZb#-VZa#
set vector3 edgeB, VXc#-VXa#, VYc#-VYa#, VZc#-VZa#
rem calculate triangle normal using crossproduct
cross product vector3 trinorm, edgeA, edgeB
rem normalise triangle vector
length#= SQRT((x vector3(trinorm)^2) + (y vector3(trinorm)^2) + (z vector3(trinorm)^2))
set vector3 trinormV, (x vector3(trinorm)/length#), (y vector3(trinorm)/length#), (z vector3(trinorm)/length#)
rem calculate step values
StepHX#=(VXc#-VXa#)/sze#
StepHY#=(VYc#-VYa#)/sze#
StepHZ#=(VZc#-VZa#)/sze#
StepVX#=(VXb#-VXa#)/sze#
StepVY#=(VYb#-VYa#)/sze#
StepVZ#=(VZb#-VZa#)/sze#
rem **LIGHTMAPPING LOOP******************************************************************
for n=0 to sze#
for m=0 to sze#
rem setup the light vector and normalise
set vector3 light, (VXa# + (m * StepHX#) + (n * StepVX#)) - LX#, (VYa# + (m * StepHY#) + (n * StepVY#)) - LY# , (VZa# + (m * StepHZ#) + (n * StepVZ#)) - LZ#
Llength#= SQRT((x vector3(light)^2) + (y vector3(light)^2) + (z vector3(light)^2))
set vector3 lightN, (x vector3(light)/Llength#), (y vector3(light)/Llength#), (z vector3(light)/Llength#)
rem find the dot product for the angle between vectors, kudos to lambert.
DP# = dot product vector3(lightN, trinormV)
b# = (2.0*DP#)/Llength#
if b#*255 > 255 then b#=1.0
rem write colour to bitmap
if stepHX#<0
dot abs(m-64), abs(n-64), rgb(255.0*b#,255.0*b#,255.0*b#)
else
dot m, n, rgb(255.0*b#,255.0*b#,255.0*b#)
endif
next m
next n
get image obj, 0, 0, sze#, sze#
set current bitmap 0
texture object obj, obj
rem *************************************************************************************
return
Specs:- 1GHZ athlon, Radeon8500, 192mb ram, winxp