2D Vector, just for convenience

Ok, so this is going to be a really small feature request, shouldn’t take much time to implement, but sometimes when I am working with position and doing some 2D vector maths, it would be more convenient to have 2D vectors, not anything critical, but for convenience, or when dealing with things that only have two values, it just seems redundant to keep having to set the z value to 0 :slight_smile: Pretty self explanatory but I imagine somehting like this:

local vec = vmath.vector2(20, 20)
-- Conversion between vector2 and vector3:
local vec3 = vmath.vector3(vec)
local vec3_1 = vmath.vector3(vec, 10)
print(vec)
-- >> x: 20, y: 20
print(vec3)
-- >> x: 20, y: 20, z: 0
print(vec3_1)
-- >> x: 20, y: 20, z: 10

Hello @dawo2002

The Defold engine will render everything in a 3D world, so even when you’re building a 2D game there is a Z-axis you have to consider. If you would place both the ground and the character at a Z value of 0 the character wont be guaranteed to be rendered on top of the ground. Such an implementation could lead to Z-fighting, which would cause your graphics to glitch.

1 Like

As you say, the implementation is probably not very complex, although I believe something like convenience functions to work with only vector2s could be created using a Lua module as well. A Lua module could potentially also monkey-patch vmath to add the vector2() function which could return a vector3.

1 Like

Hi @jakob.pogulis

I see what you mean, and I suppose I did not make myself clear enough, I do understand the perks of rendrering everything in 3d space, the 2d vectors were just for for example if you are going to apply some 2D maths, or transfer other kinds of data with only 2 “axises”, of course when you render the objects you would need 3d vectors, but this is for other things, something that I just sometimes miss

Exactly something like that is what I was looking for :slight_smile:

These are just small convenience things, like it would be nice to have predefined normalized directions, like
north, west, east, south, up, and down, even though that is mostly for readbility

You can kind of make a vector2 right now but I am not sure any of the vmath functions for vectors would work with it. You could write your own versions of anything.

vect2 = vmath.vector({1,2})
vect3 = vmath.vector3(vect2[1],vect2[2],0)
print(vect2[1])
print(vect2[2])
pprint(vect3)

I’d personally stick to vect3 with the extra ,0

I have a big general math/tool module that I’ll release eventually which has everything vmath has and more such as vect2.

What’s the real intended use for vmath.vector(table)?

2 Likes

Ok thanks! Looking forward to it :slight_smile:

I believe it’s used for custom easing curves in go.animate(). Instead of specifying an easing constant you can provide a vector containing easing values.

2 Likes

That would be worthwhile to add to the API docs for it!

1 Like

True. @sicher, this might be worth pointing out!

2 Likes

A newbie here, is there a solution to make 2D vectors yet??

Don’t think so. The recommendation is to use a v3, and simply ignore the z-value.

3 Likes

Alrightly, I’m coming from Godot and I am very used to Vector2, so I asked just in case
Thank you for replying tho!

2 Likes