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

2D physical space for Objects. More...

#include <collider.hpp>

Public Member Functions

void Simple (UPoint size, uint8_t type, Point relativePosition=Point(0, 0))
 Construct a simple Collider (filled rectangle).
 
void Write (const std::vector< std::string > &stringVector, uint8_t type, Point relativePosition=Point(0, 0))
 Construct a complex Collider (2D bitmap).
 
void ByTextureCharacter (const Texture &texture, uint8_t type, char excludedCharacter=' ')
 Construct a Collider based on a Texture's character values.
 
void ByTextureBackground (const Texture &texture, uint8_t type, uint8_t alphaThreshold=0)
 Construct a Collider based on a Texture's background values.
 
void ByTextureForeground (const Texture &texture, uint8_t type, uint8_t alphaThreshold=0)
 Construct a Collider based on a Texture's foreground values.
 
auto operator() (size_t x, size_t y) const -> bool
 Get a value from the 2D bitmap by-value (complex Colliders only).
 

Public Attributes

bool m_active = true
 Activation status: true means enabled. false means disabled, and will be ignored in Object movement processing.
 
bool m_simple
 true means simple, false means complex.
 
uint8_t m_type
 Collider type, which determines collision results based on Collision::colliderTypes.
 
Point m_rPos
 Relative position to the parent Object.
 
UPoint m_size
 Rectangle size (used in both simple and complex forms).
 
std::vector< bool > m_c
 1D vector of the 2D bitmap (used only in complex form).
 

Detailed Description

2D physical space for Objects.

Objects have a vector of these Colliders (Object::m_colliders), which represents their physical space. Colliders are used to process Object movement in Collision::MoveObject().

Similarly to Texture, there are 2 forms of Colliders: "simple" and "complex".

  • Simple colliders are just efficient filled rectangles, which take the least amount of processing and memory. So, you should prefer using simple Colliders.
  • Complex colliders are 2D bitmaps of bools which allow them to hold a detailed shape (in contrary to a filled rectangle). This makes them the least efficient in terms of processing and memory. So, minimize your use of complex Colliders to when you need such detailed shapes.

Colliders also have a "type", which you can define yourself (or first learn about) in Collision::colliderTypes.

See also
Collision::colliderTypes

Member Function Documentation

◆ ByTextureBackground()

void KTech::Collider::ByTextureBackground ( const Texture & texture,
uint8_t type,
uint8_t alphaThreshold = 0 )

Construct a Collider based on a Texture's background values.

Parameters
textureThe base Texture.
typeCollider type, as defiend in Collision::colliderTypes.
alphaThresholdCells with background alpha => this value will be turned on.

◆ ByTextureCharacter()

void KTech::Collider::ByTextureCharacter ( const Texture & texture,
uint8_t type,
char excludedCharacter = ' ' )

Construct a Collider based on a Texture's character values.

Parameters
textureThe base Texture.
typeCollider type, as defiend in Collision::colliderTypes.
excludedCharacterCells with this character will not be turned on.

◆ ByTextureForeground()

void KTech::Collider::ByTextureForeground ( const Texture & texture,
uint8_t type,
uint8_t alphaThreshold = 0 )

Construct a Collider based on a Texture's foreground values.

Parameters
textureThe base Texture.
typeCollider type, as defiend in Collision::colliderTypes.
alphaThresholdCells with foreground alpha => this value will be turned on.

◆ operator()()

auto KTech::Collider::operator() ( size_t x,
size_t y ) const -> bool
nodiscard

Get a value from the 2D bitmap by-value (complex Colliders only).

Useful if you don't want to convert the 2D position to the corresponding index in the bitmap vector (Collider::m_c).

Parameters
xX axis.
yY axis.
Returns
The value of the 2D bitmap at the given location, by-value (not by-reference!)

◆ Simple()

void KTech::Collider::Simple ( UPoint size,
uint8_t type,
Point relativePosition = Point(0, 0) )

Construct a simple Collider (filled rectangle).

Parameters
sizeRectangle size.
typeCollider type, as defiend in Collision::colliderTypes.
relativePositionRelative position to the parent Object.

◆ Write()

void KTech::Collider::Write ( const std::vector< std::string > & stringVector,
uint8_t type,
Point relativePosition = Point(0, 0) )

Construct a complex Collider (2D bitmap).

Parameters
stringVectorThe 2D bitmap representation in a vector of std::strings. If a character is a space (' '), the collider will not exist in the corresponding part of the resulting bitmap. Otherwise, the collider will exist there.
typeCollider type, as defiend in Collision::colliderTypes.
relativePositionRelative position to the parent Object.

Example:

{
" # ",
"###",
"# #"
}, 1, Point(0, 0)
);
void Write(const std::vector< std::string > &stringVector, uint8_t type, Point relativePosition=Point(0, 0))
Construct a complex Collider (2D bitmap).
Definition collider.cpp:75
2D vector, mostly used to store positions and directions.
Definition point.hpp:30

Will construct a Collider that corresponds to the Texture:

{
" O ",
"/|\\",
"/ \\"
}, RGBA( 255, 255, 0, 255 ), RGBAColors::transparent, Point(0, 0)
);
Like RGB, but also has an alpha channel representing transparency.
Definition rgba.hpp:30
auto Write(const std::vector< std::string > &stringVector, RGBA foreground, RGBA background) -> Texture &
Write a complex Texture from a vector of strings.
Definition texture.cpp:172

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