- ‘We need some way to let our engine know which objects we want to collide”. We accomplished this by creating the C_BoxCollider component, which we can attach to any object we want to collide.
- “The objects will be passed to a structure called a quadtree”. Yep done that as well.
- “Each frame our collision system will check if any objects with a collider component are colliding. If two are found to be colliding then the collision will need to be resolved so that they are no longer colliding”. Our collision system takes care of that.
- “We should have the ability to mark an object as static, which means that we will not resolve its collisions”. This has also been implemented. We use it to reduce the number of collision checks.
- “Every collidable will be on a specific layer. We will be able to set which layers collide”. We have our layer system setup. Another useful tool used to reduce the number of collision checks we need to perform.
- “When two objects collide we want to be able to inform any interested components”. This is yet to be completed and is what I want to work on this week.
So how are we going to accomplish this? As with any programming task, there are loads of ways we can go about it. However, I want to take advantage of our component system. First, we’ll create a new component that will be the base class for any component that wants to be notified of a collision change. This component will have three virtual functions: OnCollisionEnter, OnCollisionStay, and OnCollisionExit. The names of the functions may give you a clue on when the functions will be called:
- OnCollisionEnter will be called when two objects first collide.
- OnCollisionStay will be called every frame that the objects stay colliding.
- OnCollisionExit is called when two objects stop colliding by moving apart or when either of the objects has been queued for removal.
When a new component is added to an object, the object will check if the component derives from this base class and if it does it stores it in a separate collection. The object class will also have an OnCollisionEnter, Stay, and Exit functions. These will call the corresponding functions in any collidable components added to the object. We will call these functions on the objects rather than interacting with the components directly.