Open kenny-designs opened 8 years ago
I'm going to go ahead and tackle this. For each individual unit, I shall use a capsule shaped bounding box. Originally I was thinking of going with something more cube shaped but, after looking online a bit, it seems something more capsule-like would be a better fit.
A key thing I'm going to achieve here is allowing the units to spread out if they are stacked. I noticed in StarCraft II that units will stack from time to time but immediately spread out when they stop moving or run into obstacles. This should come in handy for when I move on to path finding.
I've been working on this issue for a bit. I found that the simplest way to check for collisions between units is by using a sphere. With the model open in blender, I place a sphere around its feet until it looks about right. I then take the x-value of the sphere and half it for the radius. Inside the engine itself, I am working on a new function called "collisionCheck." It finds the distance between two units and adds their radii together and squares the result. If the final result for the radii equals or exceeds the distance between the two units then we know we have a collision! From my time with the Unity game engine I was expecting to have a separate invisible mesh to test for collisions.
The main issue I'm having now is finding a clean way to have the units unstack and move around each other.
I made a new branch called 'units/BoundingCapsules' with my current progress working on collision between units and having them move around each other. Collision is spot on but movement can be funky. Here's a list of glitches I've observed thus far:
There's probably a few more where those came from too.
Last but not least, I've added several new functions to help out. Several for collision checking, one for the distance formula, and another for helping a unit decide where to go. Of all of these, the last is the backbone of what I'm doing now. It's in the Mech.cpp files and it is called eightWayCheck().
When a unit collides with another, it calls the eightWayCheck() function. It finds a temporary position to move to based on eight directions around it. It could go up, down, left, right, upper-right, upper-left, lower-right, and lower-left. It even takes into account the direction other units it collided with are going in so as to avoid following them and walking off forever. Luckily, they don't do that anymore.
There's plenty of room for improvement and roughing out the edges. Really can't wait to get this done!
Make it so units can collide with each other and the environment.