SFGF
Public Member Functions | Static Public Member Functions | List of all members
sfgf::Collider Class Reference

Handles collisions. More...

#include <Collider.hpp>

Public Member Functions

void applyTransform (const sf::Transform &t)
 Applies transform. More...
 
void pushBack (sf::Vector2f pt)
 Adds point to internal array. More...
 
void clear ()
 Clears points. More...
 
sf::FloatRect getGlobalBounds () const
 Returns global bounds. More...
 
bool intersects (const Collider &poly) const
 Check for intersection. More...
 
bool contains (sf::Vector2f point) const
 Check if point is inside of collider. More...
 
bool collides (const Collider &poly) const
 Check if two colliders collide. More...
 

Static Public Member Functions

static Collider circle (float radius, size_t cnt=128)
 Returns collider in shape of circle. More...
 
static Collider rectangle (sf::Vector2f size)
 Returns collider in shape of rectangle. More...
 
static bool lineIntersection (sf::Vector2f p1, sf::Vector2f q1, sf::Vector2f p2, sf::Vector2f q2)
 Check two lines for intersection. More...
 

Detailed Description

Handles collisions.

Contains basic set of points which can be later transformed to represent bounds of an object. Also supports globalBounds to make collision detection more efficient.
Sample code:

auto circle = sfgf::Collider::circle(5); // Make circle, r = 5
auto rect = sfgf::Collider::rectangle({10, 10}); // Make rectangle (10, 10)
auto t = sf::Transform().translate({9, 0}); // Make transform translating by 9 to the right
circle.applyTransform(t); // Apply that transform to circle
std::cout << std::boolalpha << circle.collides(rect) << std::endl; //True
circle.applyTransform(t); // Apply transform again
std::cout << std::boolalpha << circle.collides(rect) << std::endl; //False

Member Function Documentation

void sfgf::Collider::applyTransform ( const sf::Transform &  t)

Applies transform.

Parameters
[in]t– transform to use

Used to adjust position/rotation/scale of collider to actual object.
Sample code:

this->collider.applyTransform(this->polygon.getTransform());
Collider sfgf::Collider::circle ( float  radius,
size_t  cnt = 128 
)
static

Returns collider in shape of circle.

Parameters
[in]radius– radius of circle
[in]cnt– count of points to make
Returns
new collider
somePolygon.setSampleCollider(sfgf::Collider::circle(7));
void sfgf::Collider::clear ( )

Clears points.

Simply clears internal array (std::vector), empty collider never detects any collision.

bool sfgf::Collider::collides ( const Collider poly) const

Check if two colliders collide.

Parameters
[in]poly– collider to check against

Tests whether colliders collide or not. They do, if any of theirs side intersect or if no point of one collider is outside of another.

Returns
True if they collide, False otherwise.
bool sfgf::Collider::contains ( sf::Vector2f  point) const

Check if point is inside of collider.

Parameters
[in]point– point to check

Tests if given point is inside of (*this).

Returns
True if point is in collider, false otherwise
sf::FloatRect sfgf::Collider::getGlobalBounds ( ) const
inline

Returns global bounds.

Get the global bounding rectangle of the entity.
The returned rectangle is in global coordinates, which means that it takes in account the transformations (translation, rotation, scale, …) that are applied to the entity. In other words, this function returns the bounds of the collider in the global 2D world's coordinate system.

Returns
Global bounding rectangle of the entity
bool sfgf::Collider::intersects ( const Collider poly) const

Check for intersection.

Parameters
[in]poly– collider to check

Tests if any side of (*this) intersects with any side of poly.

Returns
True if colliders intersect, False otherwise
bool sfgf::Collider::lineIntersection ( sf::Vector2f  p1,
sf::Vector2f  q1,
sf::Vector2f  p2,
sf::Vector2f  q2 
)
static

Check two lines for intersection.

Parameters
[in]p1– beginning of line I
[in]q1– end of line I
[in]p2– beginning of line II
[in]q2– end of line II
Returns
True if lines intersect, False otherwise
std::cout << std::boolapha << sfgf::Collider::lineIntersection({0, 0}, {50, 20}, {25, 0}, {25 50}) << std::endl;
void sfgf::Collider::pushBack ( sf::Vector2f  pt)

Adds point to internal array.

Parameters
[in]pt– coordinates in local system

Can be used to create collider in non–standard shape, aka custom polygon.
Sample code:

while(inputFile >> x >> y) {
collider.pushBack({x, y});
}
Collider sfgf::Collider::rectangle ( sf::Vector2f  size)
static

Returns collider in shape of rectangle.

Parameters
[in]size– dimensions of rectangle
Returns
new collider
somePolygon.setSampleCollider(sfgf::Collider::rectangle({20, 10}));

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