[5SD033] Introduction & Collision System
|
Hey, my name is Alex and in the next couple of weeks I will post once a week about the progress on my team’s project. The project is a game called Datepocalypse, it’s a ”space” shooter game with a romantic and apocalyptic twist, a couple who got superpowers from their first kiss while a power plant explodes. You play as two avatars, first one is controlled with W, A, S & D and the second one is controlled with the arrow keys on your keyboard. This week I will talk about the Collision System I wrote. The system can handle collisions between circles, none rotated rectangles and circles against rectangles. The system consists of several datatypes, classes to process the data, structs to fold up related data and enumerations to be used as flags.
First is the CollisionManager, this class is used as a gate from the public API to the internal system, with this class you can add and remove so called ”Body” objects. This class also has a BroadPhase instance and CollisionCore instance (both are classes). The BroadPhase is responsible for filtering possible collisions from impossible using a simple AABB check, this is used to speed up the collision process and reduce CPU heavy tasks. This process generates a list of Pair’s, Pair is a struct containing a pointer to each Body and their respectively computed AABB’s. Secondly is CollisionCore, this class is responsible for checking if a collision actually happend. The process starts with a call from CollisionCore to BroadPhase, requesting a list containing all possible canidates (see above, AABB check). Using this data the CollisionCore can start using the more CPU heavy algorithms. First I check what kind of shape types the bodies have. This check is used so I know which function to use next. Right now there are three functions, one for circle against circle, another for rectangle against rectangle, and the last one is for rectangle against circle. All three takes a CollisionInfo struct and a reference to a CollisionReport struct and returns a boolean if the bodies actually collided or not. Inside the CollisionReport struct there are a Vector2f array called overlap. This array will be filled by how many pixels it’s overlapping the other shape (the left body’s overlap in index 0 and the right body’s overlap in index 1). When the collision check is done the collision system will automatically reset the bodies positions so there won’t be an overlap. I have to agree this system could have been made simpler and it might be a bit overkill for the scope of our project, but now we have a system that can easily be expanded if we later on decide that we need to support other shapes. I decided to work on this task early on since it’s a core mechanic and other systems will depend on this later on in the development, such as movement, shooting and other weapons. |
