WARNING THE ATTACHMED FILE CONTAINS DAMAGED CODE PLEASE SEE THE SOURCE UPDATE AT THE BOTTOM OF THIS THREAD
*************************************************
Hehe...stupidly easy !...I rewrote the DBPro matrix commands a while back as they confused me so converting them to DBC was a doddle.
Ok include the attached file to your project and use
Gosub Setup3dMath
...to initialise the system and you will have access to these commands.
=====================
VECTOR COMMANDS
=====================
You have access to 500 vectors (if you need more your will have to redimention the arrays in the 'Setup3DMath' gosub)
The commands to use these are:
SETTING VALUES
==============
SetVector2(Num,x#,y#) - Sets vector 'Num' to the x and y values given
SetVector2X(Result,x#)-Lets you set the x componant of the 2D vector
SetVector2Y(Result,x#)-Lets you set the y componant of the 2D vector
SetVector3(Num,x#,y#,z#) - Sets vector 'Num' to the x, y and z values given
SetVector3X(Result,x#)-Lets you set the x componant of the 3D vector
SetVector3Y(Result,x#)-Lets you set the y componant of the 3D vector
SetVector3Z(Result,x#)-Lets you set the z componant of the 3D vector
Vector Operations
=================
AddVector2(Result,VecA,VecB) - Vector 'result' stores the values of vectors 'VecA' and 'VecB' added together
AddVector3(Result,VecA,VecB) - Vector 'result' stores the values of vectors 'VecA' and 'VecB' added together
SubtractVector2(Result,VecA,VecB) - Vector 'result' stores the values of vectors 'VecB' subtracted from 'VecA'
SubtractVector3(Result,VecA,VecB) - Vector 'result' stores the values of vectors 'VecB' subtracted from 'VecA'
ScaleVector2(Result,Source,Value) - Vector 'result' stores the values of vector 'Source' Multiplied by 'Value'
ScaleVector3(Result,Source,Value) - Vector 'result' stores the values of vector 'Source' Multiplied by 'Value'
DivideVector2(Result,Source,Value) - Vector 'result' stores the values of vector 'Source' divided by 'Value'
DivideVector3(Result,Source,Value) - Vector 'result' stores the values of vector 'Source' divided by 'Value'
NormalizeVector2(Result,VecNum) - Normlizes the Vector 'VecNum' and stores the result in vector 'Result'
NormalizeVector3(Result,VecNum) - Normlizes the Vector 'VecNum' and stores the result in vector 'Result'
CopyVector2(Result,Source)-Copies the contents of the 2D vector 'source' into vector 'result'
CopyVector3(Result,Source)-Copies the contents of the 3D vector 'source' into vector 'result
Returning Values
================
Xvector2(Vec) - Returns the X componant of the 2D vector 'Vec'
Yvector2(Vec) - Returns the Y componant of the 2D vector 'Vec'
Xvector3(Vec) - Returns the X componant of the 3D vector 'Vec'
Yvector3(Vec) - Returns the Y componant of the 3D vector 'Vec'
Zvector3(Vec) - Returns the Z componant of the 3D vector 'Vec'
IsEqualVector2(VecA,VecB) - Will comparethe two 2D vectors and return a 1 isthey are exactly equal
IsEqualVector3(VecA,VecB) - Will comparethe two 2D vectors and return a 1 isthey are exactly equal
LengthVector2(VecNum) - Returns the length of vector 'VecNum'
LengthVector3(VecNum) - Returns the length of vector 'VecNum'
DotProductVector2(VecA,VecB) - Finds the dot product of VecA and VecB where both are 2D vectors
DotProductVector3(VecA,VecB) - Finds the dot product of VecA and VecB where both are 3D vectors
CrossProductVector3(Result,VecA,VecB) - Finds the cross product of the two 3D vectors VecA and VecB and stores the result in the 3D vector 'Result'
Advanced Operations
===================
ReflectVector3(vector,normal) - This is used to reflect a vector off an object, given that you know the normal of the face of the object.
I used this to help make a phsyics engine a while ago, the tech demo saw 200 balls moving in a room, if they collided with another ball or a wall then sparkys collision dll would give me the normal of the surface hit, and using that i could reflect the balls motion so it bounced off the wall.
GetAngleVector3(VecA,VecB) - This retrieves the angle between vector 'VecA' and vector 'VecB'
Vector3ToCamera(vec) - This sets a vector to point in the direction of the camera
Vector3ToObject(vec,obj) - This sets a vector to point in the same direct as an object, i used this to set the direction of bullets as they left a gun.
I store the bullets speed in a vector, so when the bullet is made i set its vector to the direction of the player's gun, and then scale the vector to set the speed of the bullet.
=====================
MATRIX COMMANDS
=====================
You have access to 50 matrices (again you can redimesion the arrays in the DBC-3Dmath.dba to get more if you need them)
The commands are:
MakeIdentityMatrix(Mnum): Sets the specified matrix to be an identity matrix.
MakeXRotationMatrix(Mnum,angle#): Sets the specified matrix to be a rotation matrix in the x axies for the specified number of degrees
MakeYRotationMatrix(Mnum,angle#): Sets the specified matrix to be a rotation matrix in the y axies for the specified number of degrees
MakeZRotationMatrix(Mnum,angle#): Sets the specified matrix to be a rotation matrix in the z axies for the specified number of degrees
MakeTranslationMatrix(Mnum,X#,y#,z#): Sets the specified matrix to be a translation matrix. It is set to translate the specified distance in the x,y and z axies.
MakeScaleMatrix(Mnum,x#,y#,z#): Sets the specified matrix to be a scale matrix. The scales are specified in the x,y and z axies.
MultiplyMatrix(mres,m1,m2): Multipies 2 matrices (m1 & m2) together and stores the result in matrix 'mres'
MultiplyMatrixVector3(VecRes,VecSource,Matrix): Multiplies the matrix 'matrix' with the vector 'vecsource' and stores the resultant vector in 'vecres'. This is basically the magic function that uses your matrix to transform a vector.
Ok without further adure....an example of a function to rotate a vector in all three axies.
Function RotateVector(VectorNum, x#, y#, z#)
`Ok so first we need a matrix to do the x rotating, this will`
`be matrix '1' and the angle is obviously x#
MakeXRotationMatrix(1,x#)
`next we need a matrix to do the y rotating, this will be matrix 2`
MakeYRotationMatrix(2,y#)
`finally we need a matrix to do the z rotating, this will be matrix 3`
MakeZRotationMatrix(3,z#)
`Ok it should be know that by multiplying two matrices together we `
`end up with one matrix that does the job of both. So lets multiply
`our matrices.`
MultiplyMatrix(1,1,2)
MultiplyMatrix(1,1,3)
`Ok so all our matrices are multiplied so now we have matrix '1' which
`holds the ability to translate a vector in all three axies in the
`values specified earlier. So now the magic bit
MultiplyMatrixVector3(VectorNum,VectorNum,1)
`This bit takes our vector, transforms it using matrix 1 and then puts
`the result back in our vector
`So thats it, our vector now has been rotated. And you can use this
`function wherever you like.`
Endfunction
============================
Well I hope this is of use to someone !
As usual this is all free and totally open source feel free to poke around and play with it as much as you like. My only reuqest is that any changes you make please share with me as it really helps me learn new things and ideas for new features.
Thanks people.
p.s. Oh before I go, those wishing to modify the code will want a version with less crazy array names...I use such stupid names so that there is no chance that they will conflict with any arrays already in your projects.
To start you off:
Setup3dMath:
`matrices arrays`
dim gdertsamx#(50,4,4)
Dim Tgdertsamx#(2,4,4)
`vector arrays`
Dim tgdertsaVector2#(500,2)
Dim tgdertsaVector3#(500,3)
Return
The '50' in this dim
"dim gdertsamx#(50,4,4)" is the number of matrices you have access to.
The '500' in this dim
"Dim tgdertsaVector2#(500,2)" is the number of 2D vectors you have access to.
The '500' in this dim
"Dim tgdertsaVector3#(500,2)" is the number of 3D vectors you have access to.
If you want a version with sensible array names just post here and ask.
Good-night all
M.I.A is pending