FabGL
ESP32 VGA Controller and Graphics Library
fabgl::CollisionDetector Class Reference

A class to detect sprites collisions. More...

#include <collisiondetector.h>

Public Member Functions

 CollisionDetector (int maxObjectsCount, int width, int height)
 Creates an instance of CollisionDetector. More...
 
void addSprite (Sprite *sprite)
 Adds the specified sprite to collision detector. More...
 
SpritedetectCollision (Sprite *sprite, bool removeCollidingSprites=true)
 Detects first collision with the specified sprite. More...
 
void detectCollision (Sprite *sprite, CollisionDetectionCallback callbackFunc, void *callbackObj)
 Detects multiple collisions with the specified sprite. More...
 
void removeSprite (Sprite *sprite)
 Removes the specified sprite from collision detector. More...
 
void update (Sprite *sprite)
 Updates collision detector. More...
 
SpriteupdateAndDetectCollision (Sprite *sprite, bool removeCollidingSprites=true)
 Updates collision detector and detect collision with the specified sprite. More...
 
void updateAndDetectCollision (Sprite *sprite, CollisionDetectionCallback callbackFunc, void *callbackObj)
 Updates collision detector and detect multiple collisions with the specified sprite. More...
 

Detailed Description

A class to detect sprites collisions.

CollisionDetector uses a Quad-tree data structure to efficiently store sprites size and position and quick detect collisions.
CollisionDetector is embedded in Scene class, so usually you don't need to instantiate it.

Constructor & Destructor Documentation

◆ CollisionDetector()

fabgl::CollisionDetector::CollisionDetector ( int  maxObjectsCount,
int  width,
int  height 
)

Creates an instance of CollisionDetector.

CollisionDetector is embedded in Scene class, so usually you don't need to instantiate it.

Parameters
maxObjectsCountSpecifies maximum number of sprites. This is required to size the underlying quad-tree data structure.
widthThe scene width in pixels.
heightThe scene height in pixels.

Member Function Documentation

◆ addSprite()

void fabgl::CollisionDetector::addSprite ( Sprite sprite)

Adds the specified sprite to collision detector.

The collision detector is updated calling CollisionDetector.update() or CollisionDetector.updateAndDetectCollision().
The number of sprites cannot exceed the value specified in CollisionDetector constructor.

Parameters
spriteThe sprite to add.

◆ detectCollision() [1/2]

Sprite * fabgl::CollisionDetector::detectCollision ( Sprite sprite,
bool  removeCollidingSprites = true 
)

Detects first collision with the specified sprite.

It is necessary to call CollisionDetector.update() before detectCollision() so sprites position and size are correctly updated.

Parameters
spriteThe sprite to detect collision.
removeCollidingSpritesIf true the collided sprites are automatically removed from the collision detector.
Returns
The other sprite that collided with the specified sprite.

◆ detectCollision() [2/2]

void fabgl::CollisionDetector::detectCollision ( Sprite sprite,
CollisionDetectionCallback  callbackFunc,
void *  callbackObj 
)

Detects multiple collisions with the specified sprite.

It is necessary to call CollisionDetector.update() before detectCollision() so sprites position and size are correctly updated.

Parameters
spriteThe sprite to detect collision.
callbackFuncThe callback function called whenever a collision is detected.
callbackObjPointer passed as parameter to the callback function.

◆ removeSprite()

void fabgl::CollisionDetector::removeSprite ( Sprite sprite)

Removes the specified sprite from collision detector.

Parameters
spriteThe sprite to remove.

◆ update()

void fabgl::CollisionDetector::update ( Sprite sprite)

Updates collision detector.

When a sprite changes its position or size it is necessary to update the collision detector.
This method just updates the detector without generate collision events.

Parameters
spriteThe sprite to update.

◆ updateAndDetectCollision() [1/2]

Sprite * fabgl::CollisionDetector::updateAndDetectCollision ( Sprite sprite,
bool  removeCollidingSprites = true 
)

Updates collision detector and detect collision with the specified sprite.

When a sprite changes its position or size it is necessary to update the collision detector.
This method updates the detector and detects collisions.

Parameters
spriteThe sprite to update and to check for collisions.
removeCollidingSpritesIf true the collided sprites are automatically removed from the collision detector.
Returns
The other sprite that collided with the specified sprite.

◆ updateAndDetectCollision() [2/2]

void fabgl::CollisionDetector::updateAndDetectCollision ( Sprite sprite,
CollisionDetectionCallback  callbackFunc,
void *  callbackObj 
)

Updates collision detector and detect multiple collisions with the specified sprite.

When a sprite changes its position or size it is necessary to update the collision detector.
This method updates the detector and detects multiple collisions.

Parameters
spriteThe sprite to update and to check for collisions.
callbackFuncThe callback function called whenever a collision is detected.
callbackObjPointer passed as parameter to the callback function.

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