Game Development Community

Vectors in TS

by JeffH · in Torque 3D Professional · 05/18/2013 (10:29 am) · 9 replies

would it be a good idea to make a new console object that is a vector so that we don't always have to use a string for a vector in torqueScript?

Imagine doing this:

%vec = new Vector(%x, %y, %z);

or even doing it this way:

%vec = new Vector(%obj.getPosition());

And instead of using static functions in the TS library:

%vec = new Vector(%obj.getPosition());
%vec.scale(100);
%vec.add(10, 20, 30);
%vec.add(%obj.getPosition());
%vec = %vec.torqueString(); // converts back into a vec string

It is kinda old school torqueScript to keep using string manipulation.

A question that I would have would be:
Is there any way to overload a DefineEngineMethod to support multiple parameter types?

Any thoughts?

~Jeff

#1
05/20/2013 (12:55 am)
You should avoid vector mathematics in TS for several reasons:

- it is a lot slower
- you cannot specify a precision
- you round many possible bugs, because atof.. atoll and the rest usually break stuff and 0.0, 0.0f, 0.f can return a different value despite the fact that is the same value
- you can easily debug your results in c++
#2
05/20/2013 (6:51 am)
Sure, and there's already a vector class in Torque so you'd only have to expose it to script. Should be a quick project - let us know when you're finished..... ;p
#3
05/20/2013 (12:09 pm)
@Ivan, everything is slower in Script :P, and script is nice because you don't have to recompile, which is why I sometimes prefer script over c++.

I believe they are slow because they are strings. If they weren't strings, it would be faster. If there is any speed improvement than this will definetly be worth it!

@Richard, yeah I know about all the ts functions. Thanks!
#4
05/20/2013 (12:26 pm)
I wasn't referring to the TS functions. There is a vector class in the engine. If you expose the class (and its members) to the console you can let the engine do the math without passing it back and forth through the TS math utility functions, using the syntax you demonstrated in your original post.

[Edit]
Did some crawling and technically it's Point3F, but it's typedef'd to a Vector3F. Also, there are some pieces that are missing if you're wanting a full-featured implementation. And I haven't looked at implementing operators through script - so %vec1 = %vec2 might take some work.

I like your idea because it does save some time passing stuff back and forth to the console.
#5
05/20/2013 (1:49 pm)
Oh, sorry for the confusion, thanks Richard!

[Edit] And thank you again, you ninja'd my post with your edit :P
#6
05/20/2013 (2:50 pm)
How many vector operations are you doing every second? Unless you're doing some hardcore number crunching, I wouldn't worry too much about the performance.

The Point classes are not derived from ConObject, which would need to be the case in order to have them appear in scripts. Or, more sensibly - create a subclass of ConObject that just acts as interface/storage for a Point.

I haven't looked at this in detail, but be aware - you'll be trading the performance hit of converting to/from strings for the hit of creating and removing ConObjects every time you want to work with vectors. I don't know how heavy or light that process is, but it's not a free lunch!
#7
05/20/2013 (3:21 pm)
Everyone knows TANSTAAFL, but it would be a good exercise to familiarize yourself with the console system....
#8
05/20/2013 (4:12 pm)
Oh yeah, definitely!
#9
05/20/2013 (5:20 pm)
@Daniel yeah that is going to be a problem, having to constantly instantiate a new object for every vector created. I am not doing heavy vector calculations so it may not even be worth it :P

I've been messing around with the console system and how it works with the scripting layer.