Tech Research: Grabbing Box Shapes (Patreon)
Content
Today's devpost is going to be a tech research post which are typically lengthy technical posts about implementing certain features. Search the "tech research" tag for other older ones.
At this point in the game I've been using cylinders to allow your model to interact with objects in a realistic fashion. This works perfectly for things like poles and small capsule like items. However, many objects are not of a cylindrical nature and thus there was a need to expand the types of shapes you can grab onto. In comes the new box shape for grabbing.
First I needed to figure out exactly how things would be held with a box. There are so many ways to hold a box unlike a pole. It can be complicated. Eventually I settled with a simple corner grab:
So now comes the problem of how to represent being able to grab a box's corner mathematically. There are 12 edges on a box and when you press grab, your hand should snap to the nearest corner.
However, of those 12 corners you can break it down to 4,4,4 edges each applicable for a certain hand direction. So the first step in filtering out the edges is to do some dot product with vectors to make sure you only grab those that are oriented with your hand.
As you can see in the image above, the green lines switch to represent which edges are appropriate to grab depending on my hand's orientation.
From 12 we are now down to 4 corners. Now we have to filter 3 corners so we end up only with 1 applicable one. This is done by checking which one you are the closest to. The function would be point to 3D segment distance.
There were a few caveats to filtering out corners on a box. It's not only about finding the closest corner to grab but also about making sure that your hand is not too far away. Here's a visual example:
If your hand was somewhere along the red points, your hand would be the farthest away from the edges. You'd ideally want to add a minimum corner distance so you only grab within a sensible range. Anyways the last thing I did to this algorithm was to solve the orientation in which your hand should go. This can be solved by rounding the corner normals to the nearest axis of the hand. The result:
At this point it's complete! The finger positioning was a bit trickier and frankly I'm not quite done with that part so I just added an approximation. Now we can make props and items that have grabbable square shapes. Like a wood board:
I'll explain what this building mechanic is for in the next devpost. It is related to being able to reach high up places in trees for more fruit.
Stay tuned!