Wanting to make a collider system for player built ships.

First test whether having 1000 colliders (or whatever the maximum is) causes a problem. If it does, you can do some simple optimizations – loop through the blocks, and if two are perfectly (or almost perfectly) aligned, expand one box’s collider to encompass the other block, and delete the other block’s collider. Or measure the angle between the boxes’ centers, and make a rotated collider that roughly encapsulates the two (or more) blocks.

Optimizations get more complex after that, but if you can knock 1000 colliders down to 100, that might be good enough.

It might help to put all colliders on one object, so you only have one rigidbody, and Unity can do whatever optimizations it does for compound colliders. You’ll have to test whether 1000 colliders on one object is better than 1000 colliders on 1000 objects. I suspect it is.

1 Like

Yes that is easiest first step for optimization of such mechanics.

Only

Instead of deleting, there is disable Boolean in collider.

Ok I have looked up greedy meshing but found no tutorials on how to implement it without using a mesh collider.

Also when I say to combine all colliders, I mean merge them like you would meshes.

How would I code the part that allows colliders to stretch and take over other colliders?

This is part, which you should be confident, if you venture that direction. If you need, take piece of paper and start drawing. Draw what you got and draw what you need. Then think, how you get from A to B solution. It is part of game designing.

Thing I can point at, is scaling and position offset of colliders and meshes. Also enabling and disabling objects accordingly.

It won’t be easy. One way would be to sort all of the boxes by x-coordinate, and look for matches where the entire surface lines up, plus or minus some small threshold. If you find such a match, measure the two colliders, and resize one to encompass the other. Then disable the other. Keep doing this until you run out of matches on the x-coordinate, then do the same thing for y and z.

Another thing you could do is add a resize tool to your game. The way you have it right now, you encourage users to build ships using a ton of little, stacked blocks. If you let them resize the blocks, there will be fewer colliders. Limit each ship to, say, 50 or 100 blocks, and you might be okay without any optimizations.

And here’s one more thing that just occurred to me: There are two problems you’re trying to solve. You want ships to be able to collide with each other, and you want the player to be able to walk around inside the ship. You can solve those two separately:

  • Walk inside: Instead of giving every block a collider, figure out the outline of the floor, and create colliders based on that. Alternatively, use a NavMesh. You’ll lose the ability to have stairs or complex shapes, but you’ll also have a lot fewer colliders. If you use NavMesh, you might be able to do stairs and multiple levels using multiple meshes. I’ve never tried anything like this myself – it’s just an avenue worth exploring.
  • Outside: Disable all colliders to start, and wrap the ship in one giant box collider trigger. As soon as another ship passes through the trigger, enable all the colliders to allow complex collisions. That way, if you have 100 ships floating around at once, only a few of them will have more than one collider at any given time. This may or may not help performance, depending on Unity’s existing optimizations.

As far I am concerned, NavMesh can not be generated at run time. Tell me if I am wrong tho.

Is better look at neighbors blocks, than using any form of sorting. Then combine that, with your initial solution, from earlier post.

You’d miss a lot of new systems and updates. ECS, JobSystem, PhysX upgrades, … list continues and the short answer is: You shouldn’t go back for the reasons you mentioned.

No matter if you use any of the features , if you ever needed only one of them, you’re going to miss it and your whole project would be stuck at 4.x because you would have entirely built it on a feature that’s only supported in that old version.

It seems that you can.

1 Like

Thx, certainly something I didn’t know.

Life short anecdote from developer Nick Smart (Mostly solo dev) for most of time, of From The Depth.

As far I remember correctly, he started his game dev early, probably well before 4.x version.
But many fans base players, was pushing him, to improve game performance. Hence Nick was attempting convert into Unity 5. I remember there were multiple attempts and multiple drop offs with such effort, as there was so many changes and bugs propagating through whole project, that was extremely difficult for such upgrade. Eventually was done. But not without a whole lot of a pain. And probably took over year, back and forward, just doing so. Plus still game evolving in meantime.

1 Like

I came up with two ideas but I do not know if they would work in unity:

If possible, have all the cubes used to make the ship transfer a copy of their mesh verticies and triangles to the ship’s mesh collider verticies and triangles and build the mesh collider with that. I do not know if this can be done with convex colliders tho.

The other idea is to have the verticies and triangles that form the colliders on the cubes copied onto the ship’s mesh collider and build the collider from that. I also don’t know if this can be done with convex colliders.

Would those be a good idea?

Edit: The convex mesh collider is limited to 255 triangles so I might have to devide the ship’s blocks into small groups and have each use their own mesh collider.

Over complicating.
You need also consider situation, where you ship structure will change when damaged, or in build. So you need recreating such meshes every time, such changes happens.
Why you don’t try simple solutions first?

That’s the thing, I don’t know any simple solutions.

@zioth suggested in his first post.

I would have no idea how to put that into code.

You need to figure that one. We can give you suggestions, but is least likely, that we are going write code for you.

Is it possible to merge all the box colliders in a similar way to Mesh. CombineMeshes while keeping the collider convex?

That’s what I was describing. Sorting by coordinates will help you figure out which blocks are neighbors. For example:

  • Sort all blocks by the Y coordinates of their top and bottom surfaces.

  • If block 1’s Y-top is the same as block 2’s Y-bottom:

  • Compare X and Z to see whether they line up perfectly.

  • Once you’ve done all you can with Y comparisons, sort by X and start over. Then sort by Z.

This should have a complexity of O(n log n), I think. Is there some other Unity way to find neighbors more efficiently?

Yep, that is possible way of doing it. Easiest way is by applying searching / flood algorithm principles. Having a 3D grid, and simply check indexes, of neighbours.
This solution is perfectly fine, so long, as grid is not too big. Let say you have ship 100x00x100 blocks, Tha is 1 000 000 cells in grid, per ship. Multiplied by number of data in cell. That is of course without any optimizations. Then you need resize grid, when building ships,( potential performance hit). Also, since blocks may not be 1x1x1, this introduces significant complexity and additional checks are required.

I do use neighbour information storing, per each blocks. So I don’t need 3d grid itself. But that is another can of worms by itself.

Alternatively, batch of raycasts could be used. But neither most optimal solution.

Looked up flood algorithims but only found stuff for 2d. Any other ideas?

How would I do the thing where I can overlap box colliders if I just used basic cubes to construct the ship? Where can I find some documentation on this?

I doubt there are any specific documentations. These often are custom solutions.
Probably there are some examples on web, but I would assume, these would need to be modified accordingly again, to fit your solution. Hence most likely, is better write your own.

For example I do in ECS, and there is no out of box solution yet for that.

3D flood is no much different than 2D. If you understand 2D, you can modify into 3D.