Game Development Community

2 Element Vector Math [Solved]

by Jesse Allen · in Torque 2D Beginner · 12/14/2013 (2:12 am) · 5 replies

I ran across a problem where I'm having trouble trying to perform math on 2 element vectors (location coordinates). What I'm trying to do is alter a set of coordinates and store the altered coordinates as a 2 element vector in a variable.

So, if we use..
echo(%worldPosition);
...we are going to get a 2 element vector (i.e. 0 0 )

Where the trouble comes in is when I try to alter that 2 element vector. Let's use (0 0) for an example. Here's what I'm attempting:

%posLeft = %worldPosition - 2 SPC 0;
echo(%posL);

Let's assume %worldPosition = 0 0. The above will return (-2). It won't return (-2 0), just (-2). But the 'y' value is very important as well, what has happened to it? I've tried several different ways, but no matter how I perform math on the %worldPosition coordinates only the 'x' value is returned. The above could be interpreted as (%worldPosition - 2) SPC (0) as well, so I've tried alterations. As an example, I've tried things like:

%posLeft = %worldPosition - (2 SPC 0);

All to no avail. When performing any math on the 2 element vector (coordinates), only the 'x' value is ever returned.

Is there a particular way of performing math on a 2 element vector in order to return (as expected) a 2 element vector result?

Thanks in advance for any help, of course. Cheers!

#1
12/14/2013 (2:54 am)
Despite over-trying to solve the problem, I suppose I still didn't give it enough investigation. I found the answer I needed at:

garagegames.github.io/Torque2D/TorqueScriptDocs/html/group__Vector2Math.html

In case anyone's curious, I used this and it works:

%moveL = 2 SPC 0;
%posL = Vector2Sub(%worldPosition, %moveL);
echo(%worldPosition);
echo("Left:",%posL);

Cheers!
#2
12/14/2013 (3:30 am)
Seems like you've found the solution, but in case you're wondering what was going on in your original code: TorqueScript stores everything as a string. Vectors are just strings with two words. But when you perform arithmetic, the engine casts them to an integer, performs the operation, then casts them back into strings. So when you were subtracting positions using (-), it was doing something like this: atoi("0 0") - atoi("2 0"). atoi just converts the first number it finds, so you ended up with 0 - 2, which was then converted back into the "-2" you got as a result.

What you could have done is performed the component-wise maths directly:
%pos = %obj.getPosition(); // Say it's "0 0"
%vec = "2 0";
%newPos = getWord(%pos, 0) - getWord(%vec, 0) SPC getWord(%pos, 1) - getWord(%vec, 1);
// %newPos $= "-2 0"

Short answer: use vector maths functions. :(
#3
12/14/2013 (9:26 am)
you can also just edit the components of a vector:

%pos = %obj.getPosition();
%pos.x -= 2;

This is in Torque3D dunno if its in Torque2D, but it might be worth checking out if you don't want to keep using getWord or setword
#4
12/14/2013 (12:47 pm)
Yes, string accessors are available in T2D as well.

.x .y .z .w (index 0 to 3)
.r .g .b .a (index 0 to 3)
.width .height (index 0 and 1)
._n (index n)
.count (get a count of words equivalent to "getWordCount(...)")
#5
12/14/2013 (7:33 pm)
Thanks guys, all very good advice/tips!