What the system does
This system is a collision system, it provides two different collision components, SphereCollisionComponent and BoxCollisionComponent. You can use this system to generate OnComponentHit event, OnComponentBeginOverlap event and OnComponentEndOverlap event.

This system uses the BVH structure and some other methods to reduce the performance cost. It also uses linear interpolation and binary search to avoid tunneling happen when the actor moves too fast.
How to use the system
Set Up
The set up is very easy:
- Download the Zip file I provided and unzip
- Put it to your Engine folder
- Set the correct props for Collision system just like what you did for Graphics system
- Add Math system as the reference of Collision system
- Add Collision system as the reference of MyGame
Collision Manager
Collision manager handles collision detection from collision component set and broadcast certain events when those events happened, it also builds BVH for those components. The way it works is pretty complicated but users only need to know how to use the interfaces provided.
Collision manager uses the singleton pattern, if you want to use collision manager, Call the function CollisionManager::GetCollisionManager() first to get the variable.
|
|
-
You mush call AddCollisionComponent() to add the component to the component set, if it is not set to the collision set, it would not do the collision detection;
-
You mush call RemoveCollisionComponent() before you destroy the component;
-
You should call Begin() in the beginning of the game after you add all the components to the component set;
-
You should call Update() in each update and call Destroy() when you finish the game;
Collision Component
SphereCollisionComponent and BoxCollisionComponent are the child classes of BaseCollisionComponent
Set Size
SphereCollisionComponent:
|
|
BoxCollisionComponent:
|
|
Set Initial Position
|
|
Notice: This function is only used to set the initial position. If you want to change the component’s position when it moves, please see 2.3.5 Movement
Collision Event
You can set the collision event of collision component to NoCollision, Overlap, or Hit.
|
|
The function you need to set the collision event is SetCollisionEvent(). By default, it would be set to Hit.
|
|
Rules:
Actor A | Actor B | Collision Event Generated |
---|---|---|
Hit | Hit | Hit Event |
Hit | Overlap | Overlap Event |
Overlap | Hit | Overlap Event |
Overlap | Overlap | Overlap Event |
NoCollision | Any | None |
Any | NoCollision | None |
Collision Component Type
You can set the component type to Static or Dynamic.
|
|
The function you need to set the component type is SetCollisionComponentType(). By default, it would be set to Dynamic.
|
|
Because the StaticBVH would be only built once when CollisionManager::GetCollisionManager()->Begin() is called, while the DynamicBVH would be built every time CollisionManager::GetCollisionManager()->Update() is called. So it is good for performance if the component type is set to static.
“You must set the component type to Dynamic if you want the component to move."
Movement
If you want to move the component to a new position, there are some steps you need to follow.
-
Again, please make sure the component type has set to Dynamic
-
Must call TryMoveTo() first.
|
|
- Notify Collision manager to do the collision detection by calling Update(). Collision manager would decide whether components can move to the new position and set them to the correct position.
|
|
Bind Event
There are four events provided by collision component
|
|
- OnComponentHit event would broadcast to the functions binded to it the info of the component it hits.
- OnComponentBeginOverlap event would broadcast to the functions binded to it the info of the component it just overlaps.
- OnComponentEndOverlap event would broadcast to the functions binded to it the info of the component it just finishes overlapping with.
- UpdatePositionAfterCollision event would broadcast to the functions binded to it the position of itself after collision detection.
Example of binding event
|
|
|
|
What you need to notice
When doing the final project, I found that deleting the pointer variable of the collision component added to the collision manager in the middle of the game would crash the game (even if you have already called RemoveCollisionComponent() to remove it). So if you want to use this system, please do not delete the pointer variables of the collision component in the middle of the game. Instead, delete them at the end of the game, which means you should delete all those variables at your CleanUp() function. I may fix this issue if I have enough time but for now, please follow it.
How I used what I have learned this semester
- The way to set up an engine system and make it work with other system;
- The way to make graphics debugging, thanks for that I did not stuck on any issue for a long time;
- Taking the advantage of logging system and use it to test whether the demo works as I expected;
Anything learned or struggled
- How to build BVH;
|
|
- The way to use custom hash rule to store pairs in hash set;
|
|
-
How the delegate system works;
-
The optimization methods for collision system;
-
The way to handle tunneling problem;
|
|
Collision System Download
Download and have a try: CollisionSystem