hi all, glad to see your exploring the wonders of vectors.
what i usually do i allways make a user defined type:
type vector
x as float
y as float
z as float
endtype
just to hold vectors without having to make a vector3 until i need to use their calculations.
things to research about vectors include:
- vector dot products
- transformation matrices to rotate , scale , and translate vectors (can use homogenous coordinates)
- vector cross prducts.
here is a code snippet i made which you can use to rotate a vector
use left and right to control the dot
set display mode 800,600,32
sync on : sync rate 30
sync : sync
`example of use
init3dMath()
type vector
x as float
y as float
z as float
endtype
type ship
position as vector
direction as vector
endtype
spaceracer as ship
spaceracer.position.x = 400
spaceracer.position.y = 300
spaceracer.position.z = 0
spaceracer.direction.x = 0
spaceracer.direction.y = -2
spaceracer.direction.z = 0
do
inc yAng# , leftkey() - rightkey()
rotateVector3(spaceracer.direction.x,spaceracer.direction.y,spaceracer.direction.z , 0,0,yAng#)
spaceracer.direction.x = vector3(0,1)
spaceracer.direction.y = vector3(0,2)
spaceracer.direction.z = vector3(0,3)
inc spaceracer.position.x , spaceracer.direction.x
inc spaceracer.position.y , spaceracer.direction.y
inc spaceracer.position.z , spaceracer.direction.z
dot spaceracer.position.x , spaceracer.position.y , rgb(255,0,0)
sync
loop
`--------------------------------- 3D MATH LIBRARY ---------------------------------
`initialise3dMath - call at start of program
function init3dMath()
dim matrix4(1,4,4) as float
dim vector3(1,4) as float
endfunction
`function to make a matrix 4
function makeMatrix4(matNum,ax#,ay#,az#,aw#,bx#,by#,bz#,bw#,cx#,cy#,cz#,cw#,dx#,dy#,dz#,dw#)
matrix4(matNum,1,1) = ax#
matrix4(matNum,1,2) = ay#
matrix4(matNum,1,3) = az#
matrix4(matNum,1,4) = aw#
matrix4(matNum,2,1) = bx#
matrix4(matNum,2,2) = by#
matrix4(matNum,2,3) = bz#
matrix4(matNum,2,4) = bw#
matrix4(matNum,3,1) = cx#
matrix4(matNum,3,2) = cy#
matrix4(matNum,3,3) = cz#
matrix4(matNum,3,4) = cw#
matrix4(matNum,4,1) = dx#
matrix4(matNum,4,2) = dy#
matrix4(matNum,4,3) = dz#
matrix4(matNum,4,4) = dw#
endfunction
`function to make a vector 3
function makeVector3(vecNum,x#,y#,z#)
vector3(vecNum,1) = x#
vector3(vecNum,2) = y#
vector3(vecNum,3) = z#
vector3(vecNum,4) = 1
endfunction
`function to rotate a movement vector by an x y z ang;e
`rx ry rz = angles which the vector is to be rotated by
`vx vy vz = vector to rotate
`after calling this function you can acces the result by grabbing the specified array posiition in vector(0,xyz)
function rotateVector3(vx#,vy#,vz#,rx#,ry#,rz#)
vecNum = 1
matNum = 1
ax# = (COS(rz#)*COS(ry#))+(SIN(rz#)*SIN(rx#)*SIN(ry#))
ay# = (SIN(rz#)*COS(ry#))-(COS(rz#)*SIN(rx#)*SIN(ry#))
az# = COS(rx#)*SIN(ry#)
bx# = -(SIN(rz#)*COS(rx#))
by# = COS(rz#)*COS(rx#)
bz# = SIN(rx#)
cx# = (SIN(rz#)*SIN(rx#)*COS(ry#))-(COS(rz#)*SIN(ry#))
cy# = -(COS(rz#)*SIN(rx#)*COS(ry#))-(SIN(rz#)*SIN(ry#))
cz# = COS(rx#)*COS(ry#)
dw# = 1.0
makeVector3(vecNum,vx#,vy#,vz#)
makeMatrix4(matNum,ax#,ay#,az#,0,bx#,by#,bz#,0,cx#,cy#,cz#,0,0,0,0,dw#)
`matrix multiplication in homogenous coordinates
for i = 1 to 4
inc resultX#,(matrix4(matNum,1,i)*vector3(vecNum,i))
inc resultY#,(matrix4(matNum,2,i)*vector3(vecNum,i))
inc resultZ#,(matrix4(matNum,3,i)*vector3(vecNum,i))
inc resultW#,(matrix4(matNum,4,i)*vector3(vecNum,i))
next i
makeVector3(0,resultX#,resultY#,resultZ#)
endfunction
all these things can help find vectors which are really useful.
maths rules
ps. i like your sphere gravitation thing bob. very cool
Tomu.