Game Development Community

Utilizing vectors

by Simon Chow · in Torque Game Builder · 12/01/2006 (8:22 pm) · 9 replies

Hi!

I'm trying to utilize a vector, but I'm having trouble accessing its members. I instantiated it with
$MyVect = "1.0 2.0 3.0 4.0";

and when I echo it it prints it out appropriately; however, I haven't been able to figure out how to access an individual member.

On a deeper level, how does torque decide that my string is a vector and not a string? Does it try to tokenize/parse the string right when it reads the instantiation line in the script? Basically, how can I verify that my script knows that it is a vector and not a string? Is there a way to explicitly specify a type?

I want to perform various operations on the vector (normalize, scale, etc) and I've been using the console commands to do so (VectorNormalize(), VectorScale(), etc.) and I don't get any script errors. Should I be using other functions to manipulate my vector?

It's a lot of questions, I know, thanks very much for your time!

#1
12/01/2006 (8:31 pm)
Nearly everything in TorqueScript is just a string. There is no difference between a vector and a string, except that vectors are formatted a specific way - a list. Lists are strings with values seperated by a space. To get the values, use
getWord(%MyVect, 0); //gets the first value (1.0 in your case)

//OR

getWords(%MyVect, 1, 3); //gets the list "2.0 3.0 4.0"

Hope that clears it up a bit. Take a look at lists and vector in TGB Reference and the Glossary in the docs.

Also, I didn't know about those vector methods - I always use t2dVectorAdd/Sub/Scale, etc.

And there aren't any types in Torquescript. Strings everywhere!

Hope that helps :)
#2
12/01/2006 (9:20 pm)
It should be noted that a vector in TGB is only two elements while your example shows four. That's because TGB is a 2D engine so vectors are in two dimensions. Anything more than two elements is considered a list, including sentences.
"One two three four put your feet on the floor" is a list of ten elements
"one two" is a list of two elements
"1.0 2.0" is a list of two elements AND a vector ;)
"1 2 3" is a list of three elements, or in TGE could be considered a vector, but not in TGB
Hopefully it's making sense. Tom's tips on accessing members are right on. I'll just add that there is shortcut, firstword(%list/vec) that just returns the first word without having to pass in a zero.
#3
12/04/2006 (2:18 pm)
Wonderful! Thanks!
#4
04/03/2007 (9:10 pm)
I have the following code, my enemy is a box. And i'm just doing some echo to see from which way i hit it.
but when i'm in TGB i have the following statement:
"string always evaluates to 0"
for my Box up case and Box Down case.

So if i Hit it from the left or right, it's ok.
But from the top or bottom both say "Box Up"

any idea why?

function Enemy::onCollision (%srcObject, %dstObject, %srcRef, %dstRef, %time, %normal, %contacts, %points)
{
   switch$(%normal)
   {
      case "0.0 -1.0" :
         echo("normal " @ %normal);
         echo("Box Up");
                 
      case "0.0 1.0" :
         echo("normal " @ %normal);
         echo("Box Down");
         
      case "-1.0 0.0" :
         echo("normal " @ %normal);
         echo("Box Left");
         
      case "1.0 0.0" :
         echo("normal " @ %normal);
         echo("Box Right");
   }
   
}
#5
04/03/2007 (10:19 pm)
I'm affraid, you shouldn't do that, because the precision of %normal is not only one decimal digit.
In this case you can get "0.0000 1.0000" != "0.0 1.0". You can use this workaround:

translate vector to an angle (i.e. polar coords.) and compare angles:

// assume 0 degree is "0.0 1.0";
function isDownside(%normal)
{  
   %angle = mRadToDeg(mAtan(getWord(%normal, 0), getWord(%normal, 1)));
   
   if(%angle > -1 && %angle < 1 )
      return true;
   
   return false;
}

Hm, "string always evaluates to 0" appears only if you forget $ after a switch statement or you try compare 2 strings without $ operator.
#6
04/04/2007 (6:22 am)
Thanks, i'll check that out tonight
#7
04/04/2007 (7:34 am)
But what i don 't understand is why it detected the left and right side of the box correctly?
#8
04/04/2007 (10:08 pm)
I think, an angle comparison is more stable than a string comparison. You can't control TGB and don't know in witch format goes %normal. In case of a left or right direction it may have only 1 decimal digit. I don't say what this approach won't work.
#9
04/05/2007 (6:44 am)
YEah, i did the angle thing last night :)
worked perfectly. Thanks :)