Yet Another Vector class

Monkey Forums/Monkey Code/Yet Another Vector class

V. Lehtinen(Posted 2014) [#1]
Hi!

Here's my vector class. Almost every method returns a new vector.

Live demo




Nobuyuki(Posted 2014) [#2]
it's cool, though it looks like many of the operation methods return new Vectors and there doesn't appear to be mutators. For my vector class, I used (static) functions for operations that output vector objects and kept instance methods to mutate the instance only. Why, you may ask? It's to reduce the number of new Vector objects being created, which can get important especially on mobile devices, like older Android targets which don't have concurrent GC. Significant object de/allocation can lead to significant thrash, which you'll start running into if you use this class as the foundation for a heavy-use physics engine.

You can get around this problem somewhat if you use a resource pool for your vectors -- including a reference to a resource pool in one of the instance constructors, for example, or allowing one to be specified as an optional argument in methods which return Vector.


ImmutableOctet(SKNG)(Posted 2014) [#3]
It's not really compatible, but here's my own 'vector' module. There are a number of requirements, though. All you should technically need to use it is my 'util' module.


V. Lehtinen(Posted 2014) [#4]
@Nobuyuki: Thanks for the thoughts! At first I had it returning 'Self's, but how I use them didn't work, since I didn't want to change the values of 'that' vector. Also, I don't have the Pro license, so I'm not even able to play around with mobile platforms... :P But I'll take a note on what you said.


@ImmutableOctet: Nice. O_O Hope I could understand more in your class tho :D Also, you could say that my class is named wrong, haha... Should be just a Vector2D... :)