KTech 1.1.0
C++ 2D terminal game engine library
Loading...
Searching...
No Matches
KTech::Collision Class Reference

Engine component responsible for processing Object movement and collision. More...

#include <collision.hpp>

Public Types

enum class  CR : uint8_t { B , P , O }
 Collision result. More...
 

Public Member Functions

auto MoveObject (const ID< Object > &object, Point direction) -> bool
 Move an Object in relation to other Objects from the same Layer.
 

Public Attributes

std::vector< std::vector< CR > > colliderTypes
 Matrix representing collider types and their potential collision results.
 

Friends

class Engine
 

Detailed Description

Engine component responsible for processing Object movement and collision.

Member Enumeration Documentation

◆ CR

enum class KTech::Collision::CR : uint8_t
strong

Collision result.

There are 3 possible collision results: CR::B (block), CR::P (push), and CR::O (overlap).

See also
Collision::colliderTypes
Enumerator

Block; the moving collider can't move because the passive collider can't be moved.

Push; the moving collider can make way by moving the passive collider with it.

Overlap; the moving collider can move into the passive collider.

Member Function Documentation

◆ MoveObject()

auto KTech::Collision::MoveObject ( const ID< Object > & p_object,
Point p_direction ) -> bool

Move an Object in relation to other Objects from the same Layer.

Object::Move() calls this function on itself.

This function processes all Objects in the Layer to judge whether the moving Object can move, or there are blocking Colliders. It creates a "movement tree", allowing Objects to be pushed one after another in a sequence. This function tries to form an optimal movement tree: it won't stop looking for a way to push all discovered blocking Objects, until there are no more Objects left to process (or a way was found).

Based on the final result, it can call back the following virtual Object functions (with the appropriate parameters):

See also
Object

Member Data Documentation

◆ colliderTypes

std::vector<std::vector<CR> > KTech::Collision::colliderTypes
Initial value:
{
{ CR::B, CR::P, CR::O },
{ CR::B, CR::P, CR::O },
{ CR::O, CR::O, CR::O }
}
@ P
Push; the moving collider can make way by moving the passive collider with it.
@ B
Block; the moving collider can't move because the passive collider can't be moved.
@ O
Overlap; the moving collider can move into the passive collider.

Matrix representing collider types and their potential collision results.

An Object's Collider (each of Object::m_colliders) can have a distinct "collider type" (Collider::m_type). Collider types are defined in this 2D vector.

The index of each row or column is a collider type. The intersection between 2 collider types is their collision result (Collision::CR) if the row type was of the moving collider, and the column type was of the passive collider.

For example, here's how you should look at the default values:

What if (row ↓) collides with (column →) Type 0 Type 1 Type 2
Type 0 Block Push Overlap
Type 1 Block Push Overlap
Type 2 Overlap Overlap Overlap

Meaning, colliders of type 0 can't be pushed, colliders of type 1 can be pushed, and colliders of type 2 overlap with everything.

You can set this variable to fit your needs. This matrix should be a square so all available collider types have a defined collision result between them. If a row or a column is missing, the default collision result is CR::O (overlap).

See also
Collision::CR
Collider::m_type

The documentation for this class was generated from the following files: