Closed Joy-less closed 3 weeks ago
FYI, I usually define the following extension methods to get vector2 from vector3:
public static Vector2 XY(this Vector3 self) => new(self.X, self.Y);
public static Vector2 XZ(this Vector3 self) => new(self.X, self.Z);
public static Vector2 YX(this Vector3 self) => new(self.Y, self.X);
public static Vector2 YZ(this Vector3 self) => new(self.Y, self.Z);
public static Vector2 ZX(this Vector3 self) => new(self.Z, self.X);
public static Vector2 ZY(this Vector3 self) => new(self.Z, self.Y);
public static Vector2i XY(this Vector3i self) => new(self.X, self.Y);
public static Vector2i XZ(this Vector3i self) => new(self.X, self.Z);
public static Vector2i YX(this Vector3i self) => new(self.Y, self.X);
public static Vector2i YZ(this Vector3i self) => new(self.Y, self.Z);
public static Vector2i ZX(this Vector3i self) => new(self.Z, self.X);
public static Vector2i ZY(this Vector3i self) => new(self.Z, self.Y);
@beicause I do actually like that, even though it's a lot of methods. Would be extra nice as properties:
if (player.Position.XZ.DistanceTo(enemy.Position.XZ) <= 2.0f) {
GD.Print("near");
}
if player.position.xz.distance_to(enemy.position.xz) <= 2.0:
print("near")
In fact, that's how Godot's shader language does it:
vec4 a = vec4(1.0).zxyw;
But, to be fully completionist about this, you would need to add 144 new properties:
I'm going to suggest that these properties are added:
Vector3.xy # Justification: For side-view platformers and maps directly to Vector2
Vector3.xz # Justification: For top-down games or comparing horizontally
Vector3.yz # Justification: For forward-platformers
Vector3I.xy
Vector3I.xz
Vector3I.yz
Implementations as extension methods:
public static Vector2 XY(this Vector3 self) => new(self.X, self.Y);
public static Vector2 XZ(this Vector3 self) => new(self.X, self.Z);
public static Vector2 YZ(this Vector3 self) => new(self.Y, self.Z);
public static Vector2I XY(this Vector3I self) => new(self.X, self.Y);
public static Vector2I XZ(this Vector3I self) => new(self.X, self.Z);
public static Vector2I YZ(this Vector3I self) => new(self.Y, self.Z);
Related: #727
Thank you for your proposal, this is already tracked in:
Please continue the discussion there and add your support!
Describe the project you are working on
A game with player characters and non-player characters.
Describe the problem or limitation you are having in your project
There is not a clean way to compare two objects horizontally.
Describe the feature / enhancement and how it helps to overcome the problem or limitation
Add a
Flatten
/flatten
method toVector3
that allows you to convert it to aVector2
by removing a certain axis.Before:
After:
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
This could also be implemented by returning a
Vector3
where the given axis is zero, but I think it's more useful and performant to return aVector2
.You could also implement
Flatten
/flatten
forVector4
->Vector3
, but I don't see a use case for it.Possible alternative names for the method:
horizontal()
as_horizontal()
to_flat()
to_vector2()
drop_axis()
without_axis()
project_2d()
drop_y()
without_y()
If this enhancement will not be used often, can it be worked around with a few lines of script?
It can be worked around several ways, but deciding which way wastes time and most aren't pretty.
Is there a reason why this should be core and not an add-on in the asset library?
This could benefit a lot of games (since most games don't have free vertical movement) and is very simple.