This is an AI dodgeball project I created during grad school that demonstrates physics and state machine logic.
Throwing Physics
The smallest building block of this project is the physics behind throwing each ball. The minion character throws a ball at a set speed each time, but the angle at which it throws can be adjusted. So, throwing a ball to a specific point requires calculating an angle that creates an arc to a specific point in 3d space. This endpoint is not always on the same horizontal plane as the starting point, making the calculations a little more complicated. After these calculations were completed, the ball could be thrown to a specific point.
Throwing Intercepts and Decisions
The next step required hitting a moving target. This target moved at a variable rate and could accelerate, decelerate, and change directions without warning. To hit this target with a thrown ball, a future position had to be calculated.
Based on the target’s current velocity and acceleration, a future position could be calculated a certain number of seconds into the future. Using the physics calculations of the thrown ball, it could be calculated how long it would take the thrown ball to arrive at a given location. The ball could then be thrown to the point where the ball and the target would arrive simultaneously.
However, with the target potentially changing direction or acceleration without warning, additional decision making had to be added on whether or not to throw a ball. This first involved determining how likely the target was to change velocity or direction before the ball would arrive.
If an obstacle existed between the target and future intersect point or if the target would hit the boundary before the intersect point, it was determined that a change in direction was certain before the ball would arrive. Additionally, if the target was accelerating, actively turning outside an acceptable range, or too far away, the ball wouldn’t be thrown. Lastly, if an obstacle blocked the path of the throw, the ball wouldn’t be thrown. This involved checking the trajectory of the edges of the ball to see if it would clip an obstacle because just checking the center point would potentially miss certain obstacles.
Dodgeball State Machine
All of the above building blocks were then applied to a game of dodgeball. Team strategy was determined using a state machine. On my team, each player could be in one of the following states.
StrategicDefense
– A global catch-all state that directs players to other states as needed. When in this state (and no other state is appropriate) the player runs in a narrow figure-8 pattern towards the back of their side to make them difficult to hit.CollectBall
– A state where a ball is available and that player is the closest one to it, so they go to retrieve it.GoToThrowBall
– A state where the player already has a ball and has an assigned opponent to throw at. They then proceed towards their opponent until they are close enough to throw (and proceed to ThrowBall).ThrowBall
– A state where the player already has a ball, has an assigned opponent to throw at, are close enough to throw at them, and actually throws the ball.GoToPrison
– A state where a player has been hit by a ball and proceeds to prisonLeavePrison
– After catching a ball thrown by a teammate, a state where the player leaves prison and returns to the field of play.Rescue
– A state where a player chooses to rescue a player from prison by throwing the ball for them to catch.PrisonShield
– A state where a player sacrifices themselves to block a ball being thrown to rescue an opponent.Rest
– A game-ending state where players all stop.