Godot-proposals: Add Vector4 and other 4D math types

Created on 11 Aug 2018  路  11Comments  路  Source: godotengine/godot-proposals

Describe the project you are working on:

Many projects can benefit from this feature, this is mostly for discussion.

Describe the feature / enhancement and how it helps to overcome the problem or limitation:

A Vector4 class would be similar to Vector2 and Vector3, but with four components: X, Y, Z, and W.

Potential use cases for a Vector4 class that I can think of:

There's likely many more uses for Vector4 than I'm thinking of. I'm not desperate for it, but I would find a Vector4 class useful. What matters is what Godot users would need it to do. It would be engine bloat implementing Vector4 "just because". So I'm opening this discussion to ask around about if users would find Vector4 useful and what kind of things they would use it for.

If you can think of a use case for Vector4 that you'd use, feel free to post it here.

If this enhancement will not be used often, can it be worked around with a few lines of script?:

Yes-ish. I have a C# implementation here. It's not really feasible to make within GDScript, but the real solution there would be to allow custom structs in GDScript: #279

Is there a reason why this should be core and not an add-on in the asset library?:

Having Vector4 in core would allow engine functions to use it as a built-in type whenever it's necessary to work with four numbers. An analysis of how many engine functions could benefit from this would be a good idea first. Again, this proposal is mostly for discussion.

It might indeed be a better idea for this to be an add-on, but this isn't possible in GDScript.

core

Most helpful comment

You mentioned shaders, the shading language has vec4. Although it could be useful in GDScript as well.

All 11 comments

You mentioned shaders, the shading language has vec4. Although it could be useful in GDScript as well.

@AndreaCatania Bullet has btVector4. Are there physics use cases for Vector4 in core?

In bullet I didn't used it, but I used it for the Flex Engine implementation

I don't think this deserves to be in the core. The need for them is very limited, as even in the core we don't need them (we have the quaternion class for case 3)

I don't really see why this isn't core.

Just for linking with the shader language vec4s if nothing else.

Unless Quats already do this, and can be passed to shaders, etc... as arbitrary 4-tuples.

That would be really useful for many cases.
Besides, Vector4 is a core type in many engines and (could be) used directly for producing more effects with shaders, storing complex data and scripting fancy math stuff.

It won't hurt anyone or anything to have this implemented.

It won't hurt anyone or anything to have this implemented.

Reduz stated that submissions will be refused by default and that bloat accumulates quickly, and this doesn't seem like a feature that is needed (follow the diagram here).

While the debate for the usefulness of Vector4 continues, if anyone is interested in having Vector4 in their own projects, I created a C# math library which includes Vector4 among other things. You still won't be able to use it as a parameter for Godot's built-in methods, at least not directly, but it can be used for manual math or storing the values for later.

Currently Godot Engine uses plane or color for vec4.

When it comes to shoehorning existing types for usage as a Vector4, here's a quick overview:

  • Color: This type is not ideal because it uses float rather than real_t, which would mean it has less precision than vector types when real_t is double. Color also generally expects its values to be on a range of 0 to 1, but it can go above 1. There are also some issues with automatic color space conversions that can mess things up.

  • Plane: This type expects its x, y, and z values to form a normalized vector.

  • Quat: This type expects its values to be on a range of -1 to 1, and form a valid rotation.

  • Rect2: This type has no restrictions on its position, but expects the size to be non-negative. This might be the most annoying to use since it's built out of two Vector2 values, but it also has the least restrictions on what kind of values produce a "valid" Rect2.

A Vector4 type would have no expectations, and all numbers would make up valid Vector4s.

Rect2: [鈥 but expects the size to be non-negative.

I do not remember this being the case, especially since issues like godotengine/godot#23711 are reproduced when using rect2 with negative size.

Was this page helpful?
0 / 5 - 0 ratings