Game Development Community

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...

#1
03/19/2005 (6:57 pm)
Vectors in TorqueScript are space separated strings just like the arguments to a lot of the other T2D functions. e.g. "x y".

You can use getWord(, ) to grab individual elements of the vector. So getWord("1 5", 0) would return 1. The SPC operator will stick two strings together and insert a space. So

%a = "x" SPC "y";

results in %a being equal to "x y". Similarly, you can use getWords(, , ) to extract a number of elements at once. It returns another space separated string. Also helpful (moreso in terms of dealing with the lists of objects returned by pick() in fxSceneGraph2D) is getWordCount(), which does pretty much what you'd expect.

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.
#2
03/19/2005 (7:24 pm)
Thank you. That helps me sort it out a bit better in my brain.