The discussion in short: Can we have a set of Vector2 math functions in Construct 2 available for any plugin to use?
For certain plugins and behaviors, having a vector math library is extremely useful as it can often cut down the amount of challenging, error-prone calculations by at least half. We see this in the Physics behavior, where Box2D uses b2Vec2 extensively and has a thorough selection of vector math functions. I found it surprising that there is really no useful set of 2D Vector operations provided as part of the Construct 2 framework for all plugins to use. It seems that currently, the only option as a plugin developer is to include your own Vector2 definition and set of methods, which is obviously inefficient and wasteful if more than one plugin needs vector support.
I understand Scirra's reasoning for avoiding pervasive use of a Vector2 object (primarily because it lends itself easily to careless object creation and GC problems). I have read these articles concerning Vector2's and garbage collection (the last two are written by Ashley):
Efficient JavaScript Vector Math - media.tojicode.com/sfjs-vectors
scirra.com/blog/76/how-to-write-low-garbage-real-time-javascript
netmagazine.com/tutorials/make-your-javascript-apps-smoother
The key takeaway I get from these articles is that careless instantiation of objects is the main problem. It sounds like the overhead of accessing properties or indexing arrays (ie. v.x or v[0] as opposed to vx) or using methods instead of inlining calculations is insignificant and generally acceptable for the increased code readability.
To address the risk of garbage, I think a simple comment on the Vector2 object linking to one of Ashley's above articles would help a lot. Also, the methods should all be designed with out parameters so that the user needs to instantiate the result objects ahead of time, and will hopefully do so smartly. An example of what some of these functions might look like:
Vec2.addV = function (a, b, out)
{
out.x = a.x + b.x;
out.y = a.y + b.y;
return out;
};
//Linearly interpolate between a (when s=0) and b (when s=1)
Vec2.lerp = function (a, b, s, out)
{
out.x = a.x + s * (b.x - a.x);
out.y = a.y + s * (b.y - a.y);
return out;
};
The return out; is optional (and I don't know if it affects performance over returning nothing), but it allows you to nest operations instead of being limited to one operation per line. Ex:
//This code only does one math operation per line
Vec2.addV(a, obj, originToA);
Vec2.subV(originToA, origin, originToA);
//But you have the option of compacting into a single line for the same result
Vec2.subV(Vec2.addV(a, obj, originToA), origin, originToA);
Regarding the option of using typed arrays over objects (see 1st article), I think it's still best to use plain objects with x and y properties. Using objects and static methods is actually nice; in the code above, obj is a Sprite object, but it can still be used as if it were a Vec2 because it has numeric x and y properties.