Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / Matrix (as in 3d Math) functions

Author
Message
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 22nd Feb 2006 20:37 Edited at: 23rd Feb 2006 11:27
Just putting this here in case it should be needed by anyone.
I was trying to work out how to use the matrix functions for dbpro. I got stuck and so wrote my own.

Here's the snippet to be included in your program (yeah the array names are really stupid...this is to limit the chance that you wuld already have used these arrays in your program)


And here are a list of functions (just the basics at the mo...if i make more I'll add them)

Gosub SetupMatrixSystem: This must be called at the begining of your program to initialise the arrays for use by the matrix functions.
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.



So I hope this is helpful to someone. It's as usual free to use in any project though a little nod of the head in my direction in any credits wouldnt go a miss.
Goodnight then !


p.s. If someone would test that example function for me...this computer I am on cant compile...though I have tested the matrix functions it uses.
Thanks people

[EDIT]
Ahh booger....chances that this might not be working right folks...I'm having a play around with it now.
Sorry

[Edit]
Ok think it's fixed !

[Edit]
Being the fool I am I managed to not see that some of the matrices use -sin() instead of sin()....ah well...fixing now.
Anything else anyone spots please let me know !

DSG
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Hampshire, England
Posted: 23rd Feb 2006 00:57
At first glance, I can see that your rotation matrices are not setup correctly.

The first sine operation in the Make Y rotation matrix should be inverted. The same is true for the second sine operation on the X and Z matrix (I'm assuming a left-handed coordinate system here of course!)

Danny Gregory
BSc Computer Science - UG
University of Southampton
Ianhfar
20
Years of Service
User Offline
Joined: 8th Jul 2004
Location: UK
Posted: 23rd Feb 2006 09:52
I Agree with DSG

rx = [ 1 0 0 0]
[ 0 cos(x) -sin(x) 0]
[ 0 sin(x) cos(x) 0]
[ 0 0 0 1]

ry = [ cos(y) 0 sin(y) 0]
[ 0 1 0 0]
[-sin(y) 0 cos(y) 0]
[ 0 0 0 1]

rz = [ cos(z) -sin(z) 0 0]
[ sin(z) cos(z) 0 0]
[ 0 0 1 0]
[ 0 0 0 1]

to illustrate DSG's Point LHS = DirectX
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 23rd Feb 2006 11:20 Edited at: 23rd Feb 2006 11:30
Bugger...and of course your right. Thanks so much for spotting that...it wasn't made very clear on this tutorial im working from.
Righto, I'll change that now !

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 23rd Feb 2006 18:43
i been meaning to make something like this for awhile now, but never got off my butt to do it. I've sort of gotten lazy with DB lately.

So...how 'bout a quaternion library next?


Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 24th Feb 2006 05:46 Edited at: 24th Feb 2006 06:33
Well...I didn't know what they were but I'm reading an article on it now...I'll have a study and if theres any chance of me managing it (and needing it !) I'll give it a go.

[EDIT]
Ooch!...theres some tricky math there!...Will definately take me a few more reads of it to understand. We shall see though...before then I have class so better do that first.

[EDIT]...actually probably won't be doing this yet, this stuff is a little beyond me at the moment.

Login to post a reply

Server time is: 2024-11-23 07:07:23
Your offset time is: 2024-11-23 07:07:23