3D Pool Dev Blog: Passive Cue Collision

The Problem

As you might have gathered from this blog, I’m currently part way through learning Unity SDK in order to code my own games. I decided to program a 3D Pool game in order to learn the system. I have decided to keep a bit of a Dev Blog here, as it may be of use to other people. It’s worth noting that there are 1000 ways to skin a cat in Unity – and I’m sure an experienced programmer would likely take a different approach to something like this. But this is my ‘nuts and bolts’ solution to a problem I had.

I needed to implement a system that would prevent the pool cue from intersecting other objects on the table, anywhere along the length of the cue (which was also at an angle). I wasn’t really sure what the best method for this would be. My initial idea, was to add collision to the cue object and have it constantly monitor for collisions with other objects. The moment the cue hit something, it would add some top-spin to the ball – which would have the effect of raising it off the table.

Well, I wasn’t really expecting it to work smoothly – and it didn’t! The moment the cue encountered an object on the table, it would move upwards to clear the obstruction. However, the moment it was clear, it would detect the space under the cue and drop back down to occupy it. Which would trigger it to go back up again! The system would basically just bounce between these two states. Even with a damping function applied, the cue was in constant motion. So that didn’t work.

The Solution – Multiple Colliders

The second approach I had, was to use multiple collision meshes to provide a stable means of detecting obstructions by height. These collision meshes remain at a fixed height, tracking the cue object as it rotates around the cue ball. The collision meshes are monitored by a dedicated script. The overall height of the cue would be set depending on how many of the colliders are currently being hit. In the image below, you can see that three of the colliders are hit – enough to raise the cue above the cushion:

 

cueCollision

Here’s a quick video showing the basic system in operation.  You can just make out the collision meshes, tracking the cue object as it moves. The more collisions hit, the higher the cue is offset.

This is the basic system:

And this is the final result, dampened using Mathf.SmoothDamp:

This solution seems to work quite well. It caters for the tapered shape of the cue and also its angle in relation to the table.

Comments are closed.