Collision for 2d voxel game

I’m making a terraria-style 2d voxel game and I’m wanting to use nphysics/ncollide. I’m attempting to add collision for the terrain, which right now is made up of 512×512 square chunks, each stored in its own contiguous 2d array.

I could use a bunch of Cuboids in a Compound shape, but I’m thinking it could be more performant to implement a custom shape by implementing PointQuery (I’m looking at HeightField as a model).

Can I get some pointers on the traits/structs/algorithms I will need to implement to get this working? Or perhaps I’m off-base and there’s a better way to go about this.


Yes, you are on the right track. You will need to implement the PointQuery and the Shape trait. Though keep in mind this is not enough for your shape to work with nphysics. You will also need to provide your own ContactDispatcher that will be able to return an algorithm (which you need to write too) for the computation of contacts between your custom shape and the others shapes.

Looking at how all this is done for HeightField is a very good idea and should allow you to find what you need to do.


Thanks so much! I’ll get to reading the HeightField code soon and I’ll come back with any questions.

Ok, looking more closely at the HeightField code, I can’t really understand
why its PointQuery implementation is necessary.

Looking at
it appears that it just finds the relevant segments in the given AABB and
checks each of them for collision individually.

I could do something similar for my voxel terrain: I could find voxels (as
smaller AABBs) within the given AABB that are present in the chunk, and
delegate to those. Can you explain why a PointQuery implementation is

Sorry, I said you need to implement the PointQuery because I assumed that was something you needed considering you mentioned it in your question.

If all you need is collision-detection so you can use your shape in a collider for nphysics, you don’t need PointQuery. You just need the manifold generator, custom contact dispatcher, and an implementation of the Shape trait.