Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Formula used by SphereCast to iterpolate normals when hitting an edge?

Discussion in 'Scripting' started by Iron-Warrior, May 16, 2015.

  1. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    Hi,

    As some of you may or may not know, SphereCast doesn't always return complete normals data through the hit.normal. When the cast hits an edge of two polygons, the hit.normal that is returned seems to be an interpolation of the normals of the two faces that join that edge.



    My problem is that I really need the normals data of both faces contacted. Getting one of them is really simple, since the hit.triangleIndex returned can be used to find the triangle in the sharedMesh of the mesh collider (if it is one) and then calculate the normal from there. My issue is to reliably find the normal of the second face. For the life of me I cannot derive the formula that Unity uses to calculate this (I don't suppose the source for SphereCast is available anywhere?)

    I figure if I have the interpolated normal, the normal of one face and the hit.point, I should be able to find the normal of the second face. Has anyone out there tackled this issue yet? Having both normals would be really useful for many applications I'd figure.

    Thanks for the help,
    Erik

    EDIT: *Interpolate. Dammit thread title.
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Here's one implementation that I have tried myself:
    http://www.peroxide.dk/papers/collision/collision.pdf
    The thing with this implementation, and probably unitys/physx aswell is that there is only really one intersection stored. The interpolated normal you're getting is not because you're hitting several triangles, but because of the sphere to edge intersection test a spherecast does.
     
  3. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    Hey Thermal, thanks for the response. I'm guessing you're referencing 3.4 in your document, specifically the sphere-edge intersection sweep test. It looks like the equation you are solving only gives the hit distance and point. I looked over but I couldn't see if you described how the normal of the contacted element (edge in this case) is calculated. From tests in Unity it looks like it's an interpolation between the normals of the two faces joining a contacted edge, but it sounds like you're suggesting that this is not the case, and rather is calculated differently. Sorry if I missed something obvious in the document, either way since I'm currently working on a custom character controller for Unity it's a really fascinating read and I'll definitely have to dig deeper into it sometime.