Raycast misses custom mesh collider

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: unity raycast wrong - Album on Imgur

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

                    using (var positions = new NativeArray<float3>(vertices, Allocator.Temp)) {
                        using (var indices = new NativeArray<int>(triangles, Allocator.Temp)) {

                            var collider = Unity.Physics.MeshCollider.Create(positions, indices);

                            entityManager.SetComponentData(cell, new PhysicsCollider {
                                Value = collider
                            });
                        }
                    }

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?

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.

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?

5380869--545130--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.

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!

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:

entityManager.SetComponentData(cell, new PhysicsDebugDisplayData {
    DrawColliderEdges = 1
});

I’ve tried with just DrawColliders, DrawColliderEdges, and both; but nothing shows up in-game :frowning:

I’ve got the Physics Debugger active through Window → Analysers → Physics Debugger; everything looks correct; but nothing shows up on the scene/game views…

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)

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 :frowning:

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)

ComponentType[] componentTypes = {
            typeof(Unity.Physics.PhysicsStep),
            typeof(Unity.Physics.Authoring.PhysicsDebugDisplayData)
        };
        var stepper = entityManager.CreateEntity(componentTypes);
        entityManager.SetComponentData(stepper, new Unity.Physics.PhysicsStep {
            SimulationType = SimulationType.UnityPhysics,
            Gravity = new float3(0, -9.81f, 0),
            SolverIterationCount = Unity.Physics.PhysicsStep.Default.SolverIterationCount,
            ThreadCountHint = Unity.Physics.PhysicsStep.Default.ThreadCountHint
        });
        // Add options for visually debugging physics information
        entityManager.SetComponentData(stepper, new Unity.Physics.Authoring.PhysicsDebugDisplayData {
            DrawColliderEdges = 1,
        });
1 Like

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…

2 Likes

The Physics samples contain a QueryTester prefab that’s worth a look.
Raycasts and collidercasts with debug gizmos.

Thank you. After looking over many tutorials and being 100% positive I had everything right, I wish I read this 5 hours ago…