Vector2:Cross(), Vector2:Dot()

With the rising need for ever more immersive UI and elements of menus I think expanding the Vector2 class to include :Dot() and :Cross() to perform basic operations C-Side would help create some of the effects we look for in dynamically drawing UI.
This would be useful for us now to (as said above) create UI that constantly changes, and it would be useful for ROBLOX so that if/when the time comes we decide to actively develop a CanvasFrame class (or something of the sort, with vector-graphics support) we can use this for drawing things with that class.

For me personally my application is to make a .Obj drawer with GUIs, I can do it without this being C-Side but I just like optimization.

4 Likes

It’s actually much faster to do this math 100% in lua, without involving Vector2’s in any way

2 Likes

Uh, cross product isn’t defined for 2D vectors. It’s exclusively 3D.

Crossing a Vector2 makes as much sense as taking the dot product of a basepart.

1 Like

Cross product is commonly defined in 2D space as u.xv.y-u.yv.x, which is equivalent to a signed parallelogram area defined by the two vectors. It’s close in spirit to 3D cross-product (e.g. assume that you have 2 vectors in a plane in 3D space - cross product will have the same length as this, and the sign can be thought of as whether the resulting vector is in the positive or negative half-space). Being able to compute signed areas in 2D is important for many algorithms, of course.

But yeah, the fastest way to do stuff like this in Lua is to just do component-wise math yourself in local variables.

6 Likes

Huh, I’ve only seen that called a perp dot product. Thanks for the clarification.

This is an inconsistency between Vector3 and Vector2 and should be resolved.

Performance shouldnt be a priority when talking about writing GUI code in a slow scripting language. Providing these operations for Vector2 promotes readable (and standardized) code and minimizes unnecessary up-front development effort as you dont have to write your own functions every time.

Plus right now, what happens is that you use Vector3 for 2D coordinates because it actually has those operations, which probably isnt good for performance either.

local function Cross(a,b) --Vector2,Vector2 returns Number
	return a.x*b.y-a.y*b.x
end
local v2n = Vector2.new
local function InvCross(a,b) --Number,Vector2 returns Vector2
	return v2n(-a*b.y,a*b.x)
end

Here are the functions that you need.

First function is basically:

Vector3(0, 0, a.x*b.y-a.y*b.x)

and the second one is:

Vector3(-a*b.y, a*b.x, 0)

The math is actually in 3 dimensions.

Better shown in wolfram mathematica:
blob.png

2 Likes