Vector Utilities...
by Matt Van Gorkom · in Torque Game Builder · 03/19/2005 (5:38 pm) · 2 replies
Pardon my ignorance, but I'm attempting to teach myself vector math in my attempt to create realistic car physics (top-down).
My question: when using the vector console utility functions such as vectorAdd2D(v1, v2) how do I need to have the vectors defined as? Do I need to use arrays or is a string fine? The case is probably that I don't know enough about vectors yet...
Thanks for the help, my brain is cramping...
My question: when using the vector console utility functions such as vectorAdd2D(v1, v2) how do I need to have the vectors defined as? Do I need to use arrays or is a string fine? The case is probably that I don't know enough about vectors yet...
Thanks for the help, my brain is cramping...
Torque Owner Matt Edwards
You can use getWord(
results in %a being equal to "x y". Similarly, you can use getWords(
Incidentally, T2D and Torque in general pass a lot of simple structures like vectors and colors around as space separated strings. Its the only really convenient way to return multiple values from a function.
Anyway, here's a dopy example that replicates VectorAdd2D. Hopefully it'll illustrate what I'm talking about:
function AddVectors( %v1, %v2 ) { %x1 = getWord( %v1, 0 ); %y1 = getWord( %v1, 1 ); %x2 = getWord( %v2, 0 ); %y2 = getWord( %v2, 1 ); echo( "%v1 has" SPC getWordCount(%v1) SPC "words" ); echo( "And the y component of %v2 is" SPC getWords(%v2, 1, 1) ); return (%x1 + %x2) SPC (%y1 + %y2); } // EDIT: $a = AddVectors( "1 2", "3 4" ); // results in $a $= "4 6" $a = VectorAdd2D( "1 2", "3 4" ); // same thing as ^Basically, vectors are passed around as strings and you use getWord() and SPC to pull them apart and put them back together respectively.
Hope that helps.