Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Object Depth From Angle

Discussion in 'Scripting' started by Nigey, Dec 30, 2017.

  1. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Hi Guys,

    I want to get the depth of an object from a direction.

    Example: A cube's depth when facing the intended direction will be just it's bounds.size.x. When the cube is at a 45 degree angle on the x axis in relation to the direction in question, it will be the distance between the bounds.ClosestPointOnBounds(originOfDirection) and it's point on the opposite side.

    The issue I'm having is the examples I gave above will only work on primitive shapes that are either directly facing an object, or at quarter angles to it. When a cube is at 20 degrees along the x axis to the direction, the closest and furthest points will no longer equal it's depth relative to the direction. I suppose I COULD raycast from one side of the object and then the other, then measure the distance between the two collision points. Is there a better solution? I want to be measuring distances between edges and faces, not just vertices.

    Thanks!
     
    Last edited: Dec 30, 2017
  2. MickM

    MickM

    Joined:
    Nov 19, 2012
    Posts:
    166
    Interesting - what is the scope? Is it just primitives or meshes? Are there colliders?

    Simple hack as a first instinct is to use the ClosestPointOnBounds again from the opposide side (eg. add X distance along the direction past the object position)

    There is a thread with relevant discussion here
    https://forum.unity.com/threads/how...osition-reversed-closestpointonbounds.344122/


    edit:
    I assume you are discounting vertexes as you are defining the depth as the distance from the closest point on the object (From a point in space) to the opposite side? If it is just the depth along this one direction then you can look at a raycast back along the same direction from the other side of the object to identify the point on the far side....
    Implementation is heavily tied to how you define 'depth' though.
     
    Nigey likes this.
  3. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    I have radiation attempting to penetrate objects and carry on through the other side. Whether the radiation is able to get through the object depends on it's depth based on the direction the radiation is travelling. It's going to be using a mix of primitive and mesh colliders. Here's a 2D example:



    In the first example you can use the closest and furthest points (vertices), as the travelling path is parallel to the line between those two vertices. In the second example the two vertices would not equal the depth of the object from the entry to exit points. As it's penetrating at an angle on an object's face, not directly between two vertice.

    I'm thinking I may have to just make another raycast from the opposite direction. I'm just loath to doing raycasts unless I have too :p.
     
  4. MickM

    MickM

    Joined:
    Nov 19, 2012
    Posts:
    166
    Ha well getting it working before optimising it is always good too.
    I guess it depends on what kind of radiation source you are modelling and the fidelity required; is it modelled as a 'close' 'point' source? If so, raycasts and trajectories could work. If it is a continuous source or point far away (eg. sun) then it may need a different approach. (Is it modelled for an object overall or various points along the object? In the second example, the middle may be too thick but the edges may allow it to pass?)

    Regardless, sounds interesting!
     
    Nigey likes this.
  5. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Hah you're really getting into my project :D. This is a close range one. There will be background radiation emitters that'll have a nice simple system. This ones close ranged and more accurate, so probably a few raycasts from some selected vertices within a trigger area :).

    Thanks for help!