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

Consider improving raytrace return values #1770

Open
sprunk opened this issue Nov 14, 2024 · 0 comments
Open

Consider improving raytrace return values #1770

sprunk opened this issue Nov 14, 2024 · 0 comments
Labels
refactor Internal code cleanup; paying off technical debt; janitorial work.

Comments

@sprunk
Copy link
Collaborator

sprunk commented Nov 14, 2024

Trace rays (GuiTraceRay, various CGround methods etc) return a float, which is the distance to ray intersection point.

  • the ray can fail to hit anything, in which case the distance is -1.0f. The type doesn't make this magic value obvious and it can ruin math if you forget to take it into account.
  • if the ray hits something you have to recalculate the intersection point from scratch via distance * normDir + origin.
  • hit unit/feature is "returned" by the caller passing a reference, which is confusing and has to be passed even if you're not interested in the values. The types involved also don't make it obvious that they are exclusive since you always pass both.

In that vein how about making it more obvious? Something like (named definitions just for exposition):

using GroundOrWater = bool; // or maybe even std::monostate for a general "some surface"
using WhatWasHit = std::variant <CUnit*, CFeature*, GroundOrWater>;
using Distance = float;
using HitLocation = float3;
using Hit = std::tuple <Distance, HitLocation, WhatWasHit>;
using TraceRayResult = std::optional <Hit>; // empty if didnt hit anything
@sprunk sprunk added the refactor Internal code cleanup; paying off technical debt; janitorial work. label Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Internal code cleanup; paying off technical debt; janitorial work.
Projects
None yet
Development

No branches or pull requests

1 participant