Tips for collision detection (Terrain/Entities/Particles)

Hello,

I am trying to determine the best way to compute collisions in Cesium. As far as I can tell, only Camera collision detection is built in. I need to determine when two entities are colliding (seems easy), when an particle system is colliding with an entity (seems okay) and when a particle system is colliding with a terrain (no idea).

One basic thing that I can't figure out is how to alter the material on a chunk of terrain. Assuming I have a simple radial particle system, what is the best way to determine which terrain chunks it intersects and how do I then swap the material on that chunk of terrain?

Right now my particle system is being drawn as a custom primitive but I would be open to using any part of the API to get this working.

Thanks muchly,
Nick

Probably the easiest way to check for collisions with terrain is to use globe.getHeight(). You can look at the code in there to get an idea of how to extract the closest tile, or look at the code below which is pretty similar (adapted from CesiumInspectorViewModel).

function selectTile(cartesian) {

var selectedTile;

if (Cesium.defined(cartesian)) {

    var cartographic = ellipsoid.cartesianToCartographic(cartesian);

    var tilesRendered = globe._surface.tileProvider._tilesToRenderByTextureCount;

    for (var textureCount = 0; !selectedTile && textureCount < tilesRendered.length; ++textureCount) {

        var tilesRenderedByTextureCount = tilesRendered[textureCount];

        if (!Cesium.defined(tilesRenderedByTextureCount)) {

            continue;

        }

        for (var tileIndex = 0; !selectedTile && tileIndex < tilesRenderedByTextureCount.length; ++tileIndex) {

            var tile = tilesRenderedByTextureCount[tileIndex];

            if (Cesium.Rectangle.contains(tile.rectangle, cartographic)) {

                selectedTile = tile;

            }

        }

    }

}

return selectedTile;

}

``

For the second part, altering the material for a single terrain tile, I’m not completely sure. An easier solution that might also work better is to use a GroundPrimitive to drape a material onto the terrain surface.

Also for entity collisions if you don’t need anything too precise checking the distance between the two bounding spheres would work. Same thing will particles vs. entities.