Changed collision resolving so sliding along walls is possible.

This commit is contained in:
Felix Ableitner 2013-05-26 20:16:36 +02:00
parent 27fd692471
commit 0f5d0597fc
10 changed files with 122 additions and 111 deletions

View File

@ -12,6 +12,7 @@
#include "generator/Generator.h" #include "generator/Generator.h"
#include "sprites/Enemy.h" #include "sprites/Enemy.h"
#include "sprites/Player.h" #include "sprites/Player.h"
#include "util/Yaml.h"
const int Game::FPS_GOAL = 60; const int Game::FPS_GOAL = 60;

View File

@ -69,16 +69,13 @@ void
World::step(int elapsed) { World::step(int elapsed) {
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) { for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
for (auto it = v->second.begin(); it != v->second.end(); it++) { for (auto it = v->second.begin(); it != v->second.end(); it++) {
auto& spriteA = *it; if ((*it)->getDelete()) {
if (spriteA->getDelete()) {
v->second.erase(it); v->second.erase(it);
it--; it--;
} }
else if (spriteA->getSpeed() != sf::Vector2f()) { // Don't run collision tests if sprite is not moving.
sf::Vector2f speed = spriteA->getSpeed() * (elapsed / 1000.0f); else if ((*it)->getSpeed() != sf::Vector2f())
if (!doesOverlap(spriteA, elapsed)) applyMovement(*it, elapsed);
spriteA->setPosition(spriteA->getPosition() + speed);
}
} }
} }
} }
@ -87,24 +84,25 @@ World::step(int elapsed) {
* Tests spriteA for overlap with every other sprite (considering collision * Tests spriteA for overlap with every other sprite (considering collision
* masks). * masks).
*/ */
bool void
World::doesOverlap(std::shared_ptr<Sprite> spriteA, int elapsed) { World::applyMovement(std::shared_ptr<Sprite> sprite, int elapsed) {
sf::Vector2f offset = sprite->getSpeed() * (elapsed / 1000.0f);
for (auto w = mDrawables.begin(); w != mDrawables.end(); w++) { for (auto w = mDrawables.begin(); w != mDrawables.end(); w++) {
for (auto& spriteB : w->second) { for (auto& other : w->second) {
if (spriteA == spriteB) if (sprite == other)
continue; continue;
// Ignore anything that is filtered by masks. // Ignore anything that is filtered by masks.
if (!spriteA->collisionEnabled(spriteB->getCategory()) || if (!sprite->collisionEnabled(other->getCategory()) ||
!spriteB->collisionEnabled(spriteA->getCategory())) !other->collisionEnabled(sprite->getCategory()))
continue; continue;
if (spriteA->testCollision(spriteB, elapsed)) { if (sprite->testCollision(other, offset,
spriteA->onCollide(spriteB); other->getSpeed() * (elapsed / 1000.0f))) {
spriteB->onCollide(spriteA); sprite->onCollide(other);
return true; other->onCollide(sprite);
} }
} }
} }
return false; sprite->setPosition(sprite->getPosition() + offset);
} }
/** /**

View File

@ -35,9 +35,7 @@ private:
private: private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const; void draw(sf::RenderTarget& target, sf::RenderStates states) const;
bool testCollision(std::shared_ptr<Sprite> spriteA, void applyMovement(std::shared_ptr<Sprite> sprite, int elapsed);
std::shared_ptr<Sprite> spriteB, int elapsed) const;
bool doesOverlap(std::shared_ptr<Sprite> spriteA, int elapsed);
private: private:
std::map<Sprite::Category, std::vector<std::shared_ptr<Sprite> > > mDrawables; std::map<Sprite::Category, std::vector<std::shared_ptr<Sprite> > > mDrawables;

View File

@ -24,13 +24,16 @@ Circle::Circle(const sf::Vector2f& position, Category category,
* matter which object is this or other. * matter which object is this or other.
*/ */
bool bool
Circle::testCollision(std::shared_ptr<Sprite> other, int elapsed) { Circle::testCollision(std::shared_ptr<Sprite> other,
sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond) {
Rectangle* rect = dynamic_cast<Rectangle*>(other.get()); Rectangle* rect = dynamic_cast<Rectangle*>(other.get());
Circle* circle = dynamic_cast<Circle*>(other.get()); Circle* circle = dynamic_cast<Circle*>(other.get());
if (circle != nullptr) if (circle != nullptr)
return CollisionModel::testCollision(*this, *circle, elapsed); return CollisionModel::testCollision(*this, *circle,
offsetFirst, offsetSecond);
else if (rect != nullptr) else if (rect != nullptr)
return CollisionModel::testCollision(*this, *rect, elapsed); return CollisionModel::testCollision(*this, *rect,
offsetFirst, offsetSecond);
else { else {
assert(false); assert(false);
return false; return false;

View File

@ -23,7 +23,8 @@ public:
const sf::Vector2f& direction = sf::Vector2f(0, 0)); const sf::Vector2f& direction = sf::Vector2f(0, 0));
virtual ~Circle() = default; virtual ~Circle() = default;
bool testCollision(std::shared_ptr<Sprite> other, int elapsed); bool testCollision(std::shared_ptr<Sprite> other,
sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond);
float getRadius() const; float getRadius() const;
}; };

View File

@ -17,111 +17,112 @@
CollisionModel::~CollisionModel() { CollisionModel::~CollisionModel() {
} }
/** /**
* Tests for collision between rectangle and circle. * Tests for collision between a circle and a rectangle. Offset is the maximum
* value between zero and the original value of previous, for which the
* objects do not collide. Rectangles are assumed to be axis aligned.
* *
* @param [in,out] offset The movement offset of the circle.
* @param offsetSecond Movement offset of the rectangle.
* @return True if a collision occured. * @return True if a collision occured.
*/ */
bool bool
CollisionModel::testCollision(const Circle& circle, const Rectangle& rect, CollisionModel::testCollision(const Circle& circle, const Rectangle& rect,
int elapsed) { sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond) {
sf::Vector2f halfsize = rect.getSize() / 2.0f; sf::Vector2f halfSize = rect.getSize() / 2.0f;
sf::Vector2f circlePos = circle.getPosition(); sf::Vector2f circleNewPos = circle.getPosition() + offsetFirst;
sf::Vector2f rectPos = rect.getPosition(); sf::Vector2f rectNewPos = rect.getPosition() + offsetSecond;
// Only circle movement as rectangles don't move.
sf::Vector2f circleMovement = circle.getSpeed() * (elapsed / 1000.0f);
// We assume that rectangles are always axis aligned. // If circle center is inside rect on x plane, we just take y direction result.
float overlapNoMovementX = Interval::IntervalFromRadius(circlePos.x, circle.getRadius()) if (Interval::IntervalFromRadius(rectNewPos.x, halfSize.x)
.getOverlap(Interval::IntervalFromRadius(rectPos.x, halfsize.x)).getLength(); .isInside(circleNewPos.x)) {
float overlapMovementX = Interval::IntervalFromRadius(circlePos.x + circleMovement.x, circle.getRadius()) float overlapY =
.getOverlap(Interval::IntervalFromRadius(rectPos.x, halfsize.x)).getLength(); Interval::IntervalFromRadius(circleNewPos.y, circle.getRadius())
float overlapNoMovementY = Interval::IntervalFromRadius(circlePos.y, circle.getRadius()) .getOverlap(Interval::IntervalFromRadius(rectNewPos.y, halfSize.y))
.getOverlap(Interval::IntervalFromRadius(rectPos.y, halfsize.y)).getLength();
float overlapMovementY = Interval::IntervalFromRadius(circlePos.y + circleMovement.y, circle.getRadius())
.getOverlap(Interval::IntervalFromRadius(rectPos.y, halfsize.y)).getLength();
bool xyCollisionResult = (((overlapNoMovementX < overlapMovementX) &&
(overlapNoMovementY > 0)) ||
((overlapNoMovementY < overlapMovementY) && (overlapNoMovementX > 0)));
// If circle center is overlapping rectangle on x or y axis, we can take xyCollisionResult.
if (Interval::IntervalFromRadius(rectPos.x, halfsize.x).isInside(circlePos.x) ||
Interval::IntervalFromRadius(rectPos.y, halfsize.y).isInside(circlePos.y))
return xyCollisionResult;
// Test if the circle is colliding with a corner of the rectangle.
else if (xyCollisionResult) {
// This is the same as circle-circle collision.
sf::Vector2f axis = circlePos - rectPos;
// If both objects are at the exact same position, allow any
// movement for unstucking.
if (axis == sf::Vector2f())
return true;
axis = thor::unitVector(axis);
float circlePosProjected = thor::dotProduct(axis, circlePos);
float movementProjected = thor::dotProduct(axis, circleMovement);
float rectPosProjected = thor::dotProduct(axis, rectPos);
// For corner projections, those on the same line with the rect
// center are equal by value, so we only need one on each axis
// and take the maximum.
float rectHalfWidthProjected = std::max(
abs(thor::dotProduct(axis, halfsize)),
abs(thor::dotProduct(axis,
sf::Vector2f(halfsize.x, -halfsize.y))));
// Allow movement if sprites are moving apart.
return Interval::IntervalFromRadius(circlePosProjected, circle.getRadius())
.getOverlap(Interval::IntervalFromRadius(rectPosProjected,
rectHalfWidthProjected))
.getLength() <
Interval::IntervalFromRadius(circlePosProjected + movementProjected, circle.getRadius())
.getOverlap(Interval::IntervalFromRadius(rectPosProjected,
rectHalfWidthProjected))
.getLength(); .getLength();
offsetFirst.y += (circleNewPos.y > rectNewPos.y)
? overlapY : - overlapY;
return overlapY > 0;
} }
// If there is no collision on x and y axis, there can't be one at all. // Same here (just switched x/y).
else if (Interval::IntervalFromRadius(rectNewPos.y, halfSize.y)
.isInside(circleNewPos.y)) {
float overlapX =
Interval::IntervalFromRadius(circleNewPos.x, circle.getRadius())
.getOverlap(Interval::IntervalFromRadius(rectNewPos.x, halfSize.x))
.getLength();
offsetFirst.x += (circleNewPos.x > rectNewPos.x)
? overlapX : - overlapX;
return overlapX > 0;
}
// Test if the circle is colliding with a corner of the rectangle, using
// the same method as circle-circle collision (distance to corner instead
// of radius.
else { else {
return false; sf::Vector2f axis(thor::unitVector(rectNewPos - circleNewPos));
}
// Use correct vector for corner projections (positive/negative
// direction does not matter).
float rectHalfSizeProjected;
if ((circleNewPos.x > rectNewPos.x && circleNewPos.y > rectNewPos.y) ||
(circleNewPos.x < rectNewPos.x && circleNewPos.y < rectNewPos.y))
rectHalfSizeProjected = thor::dotProduct(axis, halfSize);
else
rectHalfSizeProjected = thor::dotProduct(axis,
sf::Vector2f(halfSize.x, -halfSize.y));
Interval projectedCircle = Interval::IntervalFromRadius(
thor::dotProduct(axis, circleNewPos),
circle.getRadius());
Interval projectedRect = Interval::IntervalFromRadius(
thor::dotProduct(axis, rectNewPos),
rectHalfSizeProjected);
// using -5: works perfectly going between corner/side
// without -5 works perfectly on corner, but skips when going in between
float overlap = projectedCircle.getOverlap(projectedRect).getLength() - 5;
if (overlap > 0)
offsetFirst -= overlap * axis;
return overlap > 0;
}
} }
/** /**
* Tests for collision between two circles. * Tests for collision between two circles. Offset is the maximum value between
* zero and the original value of previous, for which the objects do
* not collide.
* *
* @param [in,out] offset The movement offset of the first circle.
* @param offsetSecond Movement offset of the second circle.
* @return True if a collision occured. * @return True if a collision occured.
*/ */
bool bool
CollisionModel::testCollision(const Circle& first, const Circle& second, CollisionModel::testCollision(const Circle& first, const Circle& second,
int elapsed) { sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond) {
sf::Vector2f axis = first.getPosition() - second.getPosition(); sf::Vector2f axis(thor::unitVector(second.getPosition() + offsetFirst -
// If both objects are at the exact same position, allow any movement for unstucking. (first.getPosition() + offsetSecond)));
if (axis == sf::Vector2f()) Interval projectedFirst = Interval::IntervalFromRadius(
return true; thor::dotProduct(axis, first.getPosition() + offsetFirst),
axis = thor::unitVector(axis); first.getRadius());
float centerA = thor::dotProduct(axis, first.getPosition()); Interval projectedSecond = Interval::IntervalFromRadius(
float radiusA = first.getRadius(); thor::dotProduct(axis, second.getPosition() + offsetSecond),
float movementA = thor::dotProduct(axis, first.getSpeed() * (elapsed / 1000.0f)); second.getRadius());
float centerB = thor::dotProduct(axis, second.getPosition());
float radiusB = second.getRadius();
float movementB = thor::dotProduct(axis, second.getSpeed() * (elapsed / 1000.0f));
// Allow movement if sprites are moving apart. float overlap = projectedFirst.getOverlap(projectedSecond).getLength();
return Interval::IntervalFromRadius(centerA, radiusA).getOverlap( if (overlap > 0)
Interval::IntervalFromRadius(centerB, radiusB)).getLength() < offsetFirst -= overlap * axis;
Interval::IntervalFromRadius(centerA + movementA, radiusA).getOverlap( return overlap > 0;
Interval::IntervalFromRadius(centerB + movementB, radiusB)).getLength();
} }
/** /**
* Tests for collision between two rectangles. Not implemented as these can't * Tests for collision between two rectangles. Always returns false as
* occur (rectangles can't move). * these can't occur (rectangles can't move).
* *
* @return True if a collision occured. * @return True if a collision occured.
*/ */
bool bool
CollisionModel::testCollision(const Rectangle& first, const Rectangle& second, CollisionModel::testCollision(const Rectangle& first, const Rectangle& second,
int elapsed) { sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond) {
return false; return false;
} }

View File

@ -11,8 +11,12 @@
class Circle; class Circle;
class Rectangle; class Rectangle;
#include <SFML/System.hpp>
/** /**
* Abstract class providing helper functions to test for collisions between shapes. * Abstract class providing helper functions to test for collisions between shapes.
*
* http://www.metanetsoftware.com/technique/tutorialA.html
*/ */
class CollisionModel { class CollisionModel {
public: public:
@ -20,11 +24,11 @@ public:
protected: protected:
static bool testCollision(const Circle& circle, const Rectangle& rect, static bool testCollision(const Circle& circle, const Rectangle& rect,
int elapsed); sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond);
static bool testCollision(const Circle& first, const Circle& second, static bool testCollision(const Circle& first, const Circle& second,
int elapsed); sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond);
static bool testCollision(const Rectangle& first, const Rectangle& second, static bool testCollision(const Rectangle& first, const Rectangle& second,
int elapsed); sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond);
}; };
#endif /* DG_COLLISIONMODEL_H_ */ #endif /* DG_COLLISIONMODEL_H_ */

View File

@ -22,13 +22,16 @@ Rectangle::Rectangle(const sf::Vector2f& position, Category category,
* matter which object is this or other. * matter which object is this or other.
*/ */
bool bool
Rectangle::testCollision(std::shared_ptr<Sprite> other, int elapsed) { Rectangle::testCollision(std::shared_ptr<Sprite> other,
sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond) {
Rectangle* rect = dynamic_cast<Rectangle*>(other.get()); Rectangle* rect = dynamic_cast<Rectangle*>(other.get());
Circle* circle = dynamic_cast<Circle*>(other.get()); Circle* circle = dynamic_cast<Circle*>(other.get());
if (circle != nullptr) if (circle != nullptr)
return CollisionModel::testCollision(*circle, *this, elapsed); return CollisionModel::testCollision(*circle, *this,
offsetFirst, offsetSecond);
else if (rect != nullptr) else if (rect != nullptr)
return CollisionModel::testCollision(*rect, *this, elapsed); return CollisionModel::testCollision(*rect, *this,
offsetFirst, offsetSecond);
else { else {
assert(false); assert(false);
return false; return false;

View File

@ -23,7 +23,8 @@ public:
const sf::Vector2f& direction = sf::Vector2f(0, 0)); const sf::Vector2f& direction = sf::Vector2f(0, 0));
virtual ~Rectangle() = default; virtual ~Rectangle() = default;
bool testCollision(std::shared_ptr<Sprite> other, int elapsed); bool testCollision(std::shared_ptr<Sprite> other,
sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond);
}; };
#endif /* DG_RECTANGLE_H_ */ #endif /* DG_RECTANGLE_H_ */

View File

@ -53,7 +53,8 @@ public:
bool collisionEnabled(Category category) const; bool collisionEnabled(Category category) const;
bool isInside(const sf::FloatRect& rect) const; bool isInside(const sf::FloatRect& rect) const;
virtual bool testCollision(std::shared_ptr<Sprite> other, int elapsed) = 0; virtual bool testCollision(std::shared_ptr<Sprite> other,
sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond) = 0;
virtual void onCollide(std::shared_ptr<Sprite> other); virtual void onCollide(std::shared_ptr<Sprite> other);
protected: protected: