Most methods have support for self modification (postfix with Self, e.g. addSelf).

Most methods have support for component wise operations (postfix with XY, e.g. addXY and addXYSelf).

Over the years I’ve written a couple of vector classes for different languages this time I ported an old C++ class to AS3. Well I couldn’t exactly port it, I more or less had to rewrite it due to differences in the two languages. What I really like about C++ is it’s strict typing and how it allows (good) developers to create code that is more or less impossible to misuse by for example using ‘const’ keywords.

As I mentioned earlier in my post AS3 and constants AS3 is missing such a feature. Another feature it’s missing is operator overloading (ability to create your own += operators). Both of those features are highly desirable for vector implementations.

Operator overloading and naming convention

It’s not hard to see why we want operator overloading considering this c++ example

Vec2 v1(1, 2);
Vec2 v2(3, 4);
v1 += v2; // Operator overloading of +=

However, in AS3 we have to settle with something less nice -methods that do the same work. Many implementations (even Adobes Vector3d among others) name the += ‘operator’ methods to ‘incrementBy’ or ‘decrementBy’ while ‘add’ and ‘sub’ returns a copy (v1 + v2). As an API user I find this rather confusing – how do I know which is modifying itself and which is returning a copy per intuition?

I decided to postfix the name all methods that modify itself with ‘Self’, so for example ‘v1.addSelf(v2);’ means v1 adds v2 to itself. This can easily be applied on any method without having to break your brain to figure out a good descriptive name.

For example what would you name a normalize() method if you wanted one that modified itself and another that returned a normalized vector? This naming convention makes it easy to keep things consequent and pretty self explanatory.

Const, or the absense of const

Just to sketch out why we want const consider the following scenario. You have a bot class, the bot class has a position. Only the bot is allowed to modify it position. Everything outside the bot is only allowed to read the position, how do we solve that? There are several ways mentioned here. For this Vector2d class I decided to go with inheritance for performance reasons. Since the vector objects often are involved in many computations, performance is relevant.

In short, using an interface for read only methods would force us to always access the components (x and y) via an intermediate get/set accessor (much slower than direct access). By doing this by inheritance internal operations will always work directly on the components and it’s easy to convert (use precompile options) to make direct access externally.

The class that contains read only methods is called Vec2Const and read and write operations are in Vec2.

12 Comments

Hi! I was looking on the internet for a long time when I stumbled upon this website. I am working on making a vector class from the http://www.tonypa.pri.ee/vectors/tut06.html because I think the code is not that good. The website shows some balls moving and bouncing of vector line, like walls. Could you point me in the direction of how to do that with our class. I am stuck with the tutorials and your class seems to have that and more!

Best regards,

Vince

Jon

Mar 3, 2013, 9:56

Sorry I don’t have any good tutorials on that stuff, I would just start playing with it, often you come along way with Vec2 newPos=oldPos.add(delta); Good luck!

azrafe7

Oct 12, 2013, 3:08

Hey, nice package you have here! 😉

Seriously, Point is disappoint for multiple reasons.
Aaand I really like the way you thought it out!

You’ve made the whole thing chainable (NICE copy().mul().normalize()), able to act on the same instance (…Self() methods), and exposing a bunch of useful functions (rads vs degs), along with keeping a Vec2Const class?! Nice job dude!

Don’t know if you’re still improving this, maybe you’ve added to it and throwed in some funky stuff as quaternions or some other useful features? I could use those too, so let us know.

Mind if I refactor it a bit and add some other methods (with credits pointing at you in the source code obviously)?

Jon

Oct 12, 2013, 7:34

Thanks! I’ve updated the code above with the latest version, not sure what has changed though.

Sure, you can do whatever you want with the code, I am happy you like it! Also let me know if you find any bugs.

It looks really good, well done. Ever since I created the Vec2Const class I have been wondering if it would have been better to just name it Vec2C. I can’t seem to decide whats nicer, what do you think?

azrafe7

Oct 17, 2013, 18:02

Nah… I think I’m fine with Vec2Const (less cryptic than Vec2C).

Also: added clamping and refactored things a bit.

azrafe7

Oct 14, 2013, 0:04

Mmmhh… here’s a little problem: I have a Vec2 called A, and I wish to calculate an orthogonal offset from it. I know I can easily do it by calculating normal vectors (left and right) and add them to A, but isn’t there an easier way to do it with spinors? Or am I misunderstanding what they should be used for?

PS: I’m planning to implement some object-pooling into the class to contain memory usage, refactor it a bit to make it more similar to the AS3 Point class, and take inspiration from Nape’s Vec2 class for further additions/changes. I’ll probably upload it up on github as soon as I’ve some relevant code.

I simplified it, dropped self functions. I will use static functions instead, or just clone original vector.

I also dropped static x and y, to make it perform better(getting and setting it is about 4 times faster this way).
But I agree with you wanting to have static properties, I use different approach – I use mvcExpress live testing feature to ensure that const vectors are never changed.
My approach does not support constant vector.

Thanks for your work again.

PS : where mul() function is used? I know only scalar multiplication use in practice..