Search Unity

Raycast misses custom mesh collider

Discussion in 'Physics for ECS' started by jonwah00, Jan 16, 2020.

  1. jonwah00

    jonwah00

    Joined:
    Jul 21, 2019
    Posts:
    36
    Hi,

    Firstly; I am new to game development, and DOTS/Physics, so I'm not sure if I'm even taking the right approach here, but I would appreciate any help anyone can give.

    I'm attempting to procedurally generate a 3D world which consists of hexagons wrapped into a cylinder. I've tried a few different approaches, but at the moment I'm doing it by generating a unique mesh for each hexagon cell, with the vertices at world positions, and 0 translation. Note, I did try the same with a 'single' mesh and translating/rotating each one correctly, but it gave me small render bugs where each hex met.

    Anyway; that's working, and now I'm attempting to raycast to touch each hex. I have created a mesh collider for each mesh, then do a Unity.Physics collision world CastRay to find the hit entity.

    It does work - but is inaccurate. I've verified the ray direction/length is correct (with Debug.DrawRay), so I suspect the collider mesh might be out of sync with the real mesh?



    If that image doesn't work, link: https://imgur.com/a/xSufyDc

    Here's the creation of the mesh collider (vertices, triangles are the verts/indices from each mesh created):

    Code (CSharp):
    1.  
    2.                     using (var positions = new NativeArray<float3>(vertices, Allocator.Temp)) {
    3.                         using (var indices = new NativeArray<int>(triangles, Allocator.Temp)) {
    4.  
    5.                             var collider = Unity.Physics.MeshCollider.Create(positions, indices);
    6.  
    7.                             entityManager.SetComponentData(cell, new PhysicsCollider {
    8.                                 Value = collider
    9.                             });
    10.                         }
    11.                     }

    So; a couple of questions:

    What could be causing this?

    Is this even a remotely sane idea? I would like to be able to support a large map size; so there maybe hundreds of thousands of these meshes in the game - will this just be too much even with ECS/DOTS?

    I'm using a mesh collider as I don't think any of the built in colliders would work with hexagons (for detecting when the mouse is over that hex/click etc) - but am I wrong here? Could a sphere approximate it, and be a lot faster?
     
  2. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    If each hexagon is the same size, then I would certainly only want to create a single shared MeshCollider that is shared across all the bodies. I even recommend using the CylinderCollider rather than the MeshCollider and setting the side count to 6.
    upload_2020-1-17_10-22-39.png

    What size are the colliders and the world in general? Having the vertices of the mesh centered around the local origin and translating the PhysicsCollider would be preferable. If the colliders and world space gets big you could well be hitting floating point accuracy issues.

    Can you add the Physics Debug Display component and render the Collider Edges and maybe the Broadphase?

    upload_2020-1-17_10-26-27.png

    You should see some red boxes representing the broadphase and green edge lines representing the objects physically representation. If these are drawn along with the green raycast it might help visualize the problem.
     
  3. jonwah00

    jonwah00

    Joined:
    Jul 21, 2019
    Posts:
    36
    Thanks very much for your reply!

    The colliders are each about 10 units radius - the world size can change as it's generated programmatically. Ideally I wanted it to be able to be quite large - thousands/tens of thousands of cells wide/high.

    I did try (and got working) the approach of having a single mesh used for each entity (I hadn't tried colliders at this point in time), and then translating and rotating each cell appropriately to form the world. This did work, but I ended up with small errors where the cells wouldn't quite align. I put it down to floating point accuracy and so abandoned this approach; but perhaps I should try again; with colliders this time.

    Thanks heaps for the pointer about Physics Debug Display component; I was trying to use the collider debugger but couldn't see anything coming up; I think that will help! I'll post again when I've had time to try it out!

    Also thanks for the tip about using a cylinder collider with 6 sides, that sounds like it would be a lot more performant than a mesh collider!
     
  4. jonwah00

    jonwah00

    Joined:
    Jul 21, 2019
    Posts:
    36
    Ok so I can't quite figure out how to get the physics debug display attached to the entity.

    I've tried instantiating it for each entity which has a mesh + collider as below:

    Code (CSharp):
    1. entityManager.SetComponentData(cell, new PhysicsDebugDisplayData {
    2.     DrawColliderEdges = 1
    3. });
    I've tried with just DrawColliders, DrawColliderEdges, and both; but nothing shows up in-game :(

    I've got the Physics Debugger active through Window -> Analysers -> Physics Debugger; everything looks correct; but nothing shows up on the scene/game views..
     
  5. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    A few notes when playing with dots:
    • Your Raycast will not hit faces from behind. Mind triangle orientation.
    • Your Raycast will not hit anything before the Physics World has been updated (dots specific)
     
  6. jonwah00

    jonwah00

    Joined:
    Jul 21, 2019
    Posts:
    36
    Thanks for the reply; the faces should be correct as they're set to the same rotation as the actual mesh; which is rendered correctly. I'm still trying to get the physics debugger working to show me the colliders so that I can verify they are in the right world positions, but it still doesn't seem to be working :(
     
  7. jonwah00

    jonwah00

    Joined:
    Jul 21, 2019
    Posts:
    36
    Ok; finally got physics debug working properly. Didn't realise the debug authoring component - Unity.Physics.Authoring.PhysicsDebugDisplayData - was supposed to be added to some kind of global step - I had been adding it to each of my entities I wanted to debug..

    Full code below for adding physics debug (taken from physics sample)

    Code (CSharp):
    1. ComponentType[] componentTypes = {
    2.             typeof(Unity.Physics.PhysicsStep),
    3.             typeof(Unity.Physics.Authoring.PhysicsDebugDisplayData)
    4.         };
    5.         var stepper = entityManager.CreateEntity(componentTypes);
    6.         entityManager.SetComponentData(stepper, new Unity.Physics.PhysicsStep {
    7.             SimulationType = SimulationType.UnityPhysics,
    8.             Gravity = new float3(0, -9.81f, 0),
    9.             SolverIterationCount = Unity.Physics.PhysicsStep.Default.SolverIterationCount,
    10.             ThreadCountHint = Unity.Physics.PhysicsStep.Default.ThreadCountHint
    11.         });
    12.         // Add options for visually debugging physics information
    13.         entityManager.SetComponentData(stepper, new Unity.Physics.Authoring.PhysicsDebugDisplayData {
    14.             DrawColliderEdges = 1,
    15.         });
     
    Pargeno likes this.
  8. jonwah00

    jonwah00

    Joined:
    Jul 21, 2019
    Posts:
    36
    And to add to the "here's how I solved my own stupid problem" - I had erroneously set the colliders' center and orientation to be the same as each cell I was adding to the world; so it was adding the world position as a local offset. Removed that and all is well in the world...
     
    Emxm3 and steveeHavok like this.
  9. andywatts

    andywatts

    Joined:
    Sep 19, 2015
    Posts:
    112
    The Physics samples contain a QueryTester prefab that's worth a look.
    Raycasts and collidercasts with debug gizmos.
     
  10. Emxm3

    Emxm3

    Joined:
    Aug 10, 2016
    Posts:
    1
    Thank you. After looking over many tutorials and being 100% positive I had everything right, I wish I read this 5 hours ago...