Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mono] Plane intersect methods returning Vector3(0, 0, 0) #32614

Closed
wfowler1 opened this issue Oct 7, 2019 · 7 comments · Fixed by #32670
Closed

[Mono] Plane intersect methods returning Vector3(0, 0, 0) #32614

wfowler1 opened this issue Oct 7, 2019 · 7 comments · Fixed by #32670

Comments

@wfowler1
Copy link

wfowler1 commented Oct 7, 2019

Godot version:
3.1.1
OS/device including version:
Windows 10
Issue description:
Per the documentation at https://docs.godotengine.org/en/3.1/classes/class_plane.html Plane.intersect_3, Plane.intersect_ray and Plane.intersect_segment will return null if no intersection is found. However, in C#/Mono, value types cannot be null, so instead these methods return Vector3(0, 0, 0). This isn't good, since (0, 0, 0) could be a valid intersection point but we have no way to know if it was the actual intersection or an invalid result. A better result might be to return Vector3(float.NaN, float.NaN, float.NaN).
Steps to reproduce:
Try to use any of these three methods from C# using data which won't actually intersect. You can use Intersect3 with planes:
(0, 0, 1), 0
(0, 0, 1), 1
[any other plane]

@Zylann
Copy link
Contributor

Zylann commented Oct 7, 2019

Could we make it return Nullable<Vector3>?
Or return a bool with an out parameter, although that signature would need to be C# only.
Returning a class should be avoided because it would allocate on the heap and that's no good for realtime math.

@Chaosus Chaosus changed the title [GodotSharp] Plane intersect methods returning Vector3(0, 0, 0) [Mono] Plane intersect methods returning Vector3(0, 0, 0) Oct 7, 2019
@wfowler1
Copy link
Author

wfowler1 commented Oct 8, 2019

Nullable<Vector3> would probably work as well.
As far as returning a bool with an out parameter, this is the approach Unity3D took for its raycasts and it works fairly well, though I'm not sure it's the cleanest implementation.
Really the main issue is knowing whether there actually was an intersection or not. However this is done, it shouldn't yield a result which could potentially be valid.

@aaronfranke
Copy link
Member

aaronfranke commented Oct 8, 2019

Another option, if we want to use purely value types, is to return Vector3.Inf since infinity is easier to compare than NaN (NaN == NaN is always false).

I prefer nullable types to bool with out.

@NathanWarden
Copy link
Contributor

I agree with Zylann about either for Nullable or returning bool with an out parameter since these seem to be basically self documenting. Nullable seems a little better since it's essentially a 1 to 1 equivalent with GDScript.

@neikeq
Copy link
Contributor

neikeq commented Oct 9, 2019

I would prefer the bool and out way too.

@mwerezak
Copy link

Nullable or bool/out are the most idiosyncratic ways to do this in C# so it should probably be one of these two.

I can't think of any real pro/cons between the two so it seems like a purely stylistic difference. Returning a Vector3? (i.e. the nullable type) would more closely match the gdscript API.

The other ideas involving INF or NaN just seem awkward and weird as a longtime C# dev.

@aaronfranke
Copy link
Member

@mwerezak Agreed, the PR I opened uses Vector3?, the suggestion of INF was just an idea. I prefer Vector3? over bool with out parameters personally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants