Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

PhysicsWorld.CastRay does not hit if distance is large

Discussion in 'Physics for ECS' started by MNNoxMortem, Nov 2, 2019.

  1. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    I am doing some tests with PhysicsWorld.CastRay and it looks like the rays consistently do not hit something when I the End point is far away.

    For example RayCastInput

    Code (CSharp):
    1. End = origin + direction*1e10f // does not hit
    2. End = origin + direction*1e6f // does hit
    3.  
    4. // just  for sake of completeness - not really usable without attaching an example project
    5. Start = (0.1, 49.7, 0.0)
    6. End = float3(437697,6f, -896598,6f, 66654,33f) // 1e6f
    7.  
    8. // Result:
    9. Fraction = 0.000139309908 // 1e6f
    10.  
    This might be related to float having only about 6-9 significant digits, which are already reached when calculating the direction over 10 digits (end*1e10-start*1e0).

    I assume there is no way to force double precision for Unity.Physics?

    Scenario: Data from a scientific area, which easily spans orders larger than that.
     
    Last edited: Nov 4, 2019
  2. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    Physics uses the general Translation component so, if possible, forcing double precision won't just be a Unity Physics thing.
    Is the usecase focused on querying a static world or trying to simulate a massive world as well?
    To somewhat mitigate the precision issues, you'll need some custom query code. For example, you could break the really long raycast into a series of AABB overlap queries. Then each partial ray segment can be converted to local space of any overlapping collider, and a raycast performed against the collider directly.
     
    MNNoxMortem likes this.