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.

DarkBASIC Discussion / Anyone interested in matrices (the 3D math kind)

Author
Message
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 11th Mar 2006 18:22
Hey folks,
A long while back I wrote a big collection of vector commands for DBC. Just recently I got learning about matrices and find them very useful and thus I was wondering if you guys would like me to knock up a DBC compatible matrices function addon.
It will be completly DBC compatible without having the enhanced version.

It would take a few days to make so I just wanted to ask if anyone would be interested before I dive in.

Thanks people

M.I.A is pending
Dodic
19
Years of Service
User Offline
Joined: 6th Nov 2005
Location: SNM (Serbia&Montenegro)
Posted: 11th Mar 2006 18:24
I like the idea very much, and i am very interested !

Currently working on (trying) making a new 3d engine for/within dark basic classic out of basic 2d commands, with vertex edit supported.
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 11th Mar 2006 18:59
...um..hehe...well...that was surprising.

Not your reply..Ive just been looking at my code. Suprisingly easy to convert it actually....Give me 20 minutes or so.

M.I.A is pending
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 11th Mar 2006 19:33 Edited at: 29th Jun 2006 17:45
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.





============================


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

Attachments

Login to view attachments
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 11th Mar 2006 19:52 Edited at: 14th Mar 2006 10:45
Oh a bit of advice:
Read the page at the link below to learn about vectors. It was writen by the infamous Philip and it is a brilliant tutorial.
http://www.kentaree-studios.com/tutorials/vectors/

For those with a strong grasp of vectors and want to know about matrices. I learnt from here:
http://www.gamedev.net/reference/articles/article877.asp

And just to round it all off...a wee demo showing jumping, sliding inertia, all over a matrix landscape.

Good-night again !

M.I.A is pending

Attachments

Login to view attachments
code master
21
Years of Service
User Offline
Joined: 4th Dec 2003
Location: Illinois
Posted: 11th Mar 2006 20:09
looks cool, but i get:

Quote: "syntax error : Differ between function call and decleration"


This is for dbc non enhnaced, right?

the error line was the first time the SetVector3 command was called:
Quote: "
SetVector3(1, 0, -0.2, 0)"


later

Dodic
19
Years of Service
User Offline
Joined: 6th Nov 2005
Location: SNM (Serbia&Montenegro)
Posted: 12th Mar 2006 00:28
Same error, same line.


I hope you will be able to fix this, and it really seams like great work !

Currently working on (trying) making a new 3d engine for/within dark basic classic out of basic 2d commands, with vertex edit supported.
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 12th Mar 2006 16:31
Sorry about that..will have a check now.

M.I.A is pending
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 13th Mar 2006 19:06
Sorry about the delay...we just had the biggest storm this area of uganda has seen in years and it completly knocked out the hydro plant...spent today teaching computing without power (nice challenge) and rigging up a generator....luckily all is back again now so I will have another look tommorow.

M.I.A is pending
Dmitry K
22
Years of Service
User Offline
Joined: 14th Oct 2002
Location: Russia
Posted: 13th Mar 2006 21:17
Your DotProduct function is too complex.


Specs: P4 2.4GHz, DIMM 256 266Mhz, 60GB HD 7200rpm ATA-133, GeForce NX6600, CD-RW+DVD, WinXP
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 14th Mar 2006 06:43 Edited at: 14th Mar 2006 10:09
Thanks very much.
I'm going to try and download the demo of DBC tonight so I can fix this tommorow as I can't find the problem yet.
Sorry again for the delay.

[EDIT]
AHAH ! found the problem....and it isnt major lukily.

Apparantly DBC doesnt like it if you pass integers to a function that wants floats so instead of

SetVector3(1, 0, -0.2, 0)
we need to write
SetVector3(1, 0.0, -0.2, 0.0)"

And it will work !

I have also replaced the dot product functions so they will be faster now.

Right I'll get on making the neccesary changes in the demo now and repost that. Thanks for your patience people

M.I.A is pending
BearCDPOLD
21
Years of Service
User Offline
Joined: 16th Oct 2003
Location: AZ,USA
Posted: 14th Mar 2006 10:54
Baggers you're the man.


I'm going to eat you!
Sonic 91 Software
20
Years of Service
User Offline
Joined: 19th Mar 2005
Location: In a Cryptic Crossworld!
Posted: 14th Mar 2006 12:57
how many avatars do you have, dodic!? every time i visit the forums (more or less), you have a different avatar!

I am a Christian and I know that God loves me. That is what makes my life so great!
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 14th Mar 2006 13:24
BearCDP: No problem, just let me know if anything is wrong and I'll fix it. I just couldn't imagine how I used to make anything without vectors since I started using them.

Before Phalex arives and asks be if I can one for quaternions...sorry mate they are still a little out my league...As soon as I understand them I'll release one though.

Seeya

M.I.A is pending
Dodic
19
Years of Service
User Offline
Joined: 6th Nov 2005
Location: SNM (Serbia&Montenegro)
Posted: 14th Mar 2006 19:57 Edited at: 14th Mar 2006 19:57
I change avatars in every 15-30 days, but not anymore, i found one that i`m conforatable with

Currently working on (trying) making a new 3d engine for/within dark basic classic out of basic 2d commands, with vertex edit supported.
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 16th Mar 2006 08:11
Just before I resign this thing as finished are there any request for features? expansions, extra commands ? fixing of anything found broken ?

M.I.A is pending
Baggers
20
Years of Service
User Offline
Joined: 31st May 2004
Location: Yonder over dem dere hills
Posted: 29th Jun 2006 17:43
Christ, sorry people but I've just found some big bugs in the matrix code. If you are making use of these functions please update the source with the one attached.
Once again sorry for the inconvinience

M.I.A is pending

Attachments

Login to view attachments

Login to post a reply

Server time is: 2025-05-25 02:17:17
Your offset time is: 2025-05-25 02:17:17