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

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

View File

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

View File

@ -35,9 +35,7 @@ private:
private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
bool testCollision(std::shared_ptr<Sprite> spriteA,
std::shared_ptr<Sprite> spriteB, int elapsed) const;
bool doesOverlap(std::shared_ptr<Sprite> spriteA, int elapsed);
void applyMovement(std::shared_ptr<Sprite> sprite, int elapsed);
private:
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.
*/
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());
Circle* circle = dynamic_cast<Circle*>(other.get());
if (circle != nullptr)
return CollisionModel::testCollision(*this, *circle, elapsed);
return CollisionModel::testCollision(*this, *circle,
offsetFirst, offsetSecond);
else if (rect != nullptr)
return CollisionModel::testCollision(*this, *rect, elapsed);
return CollisionModel::testCollision(*this, *rect,
offsetFirst, offsetSecond);
else {
assert(false);
return false;

View File

@ -23,7 +23,8 @@ public:
const sf::Vector2f& direction = sf::Vector2f(0, 0));
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;
};

View File

@ -17,111 +17,112 @@
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.
*/
bool
CollisionModel::testCollision(const Circle& circle, const Rectangle& rect,
int elapsed) {
sf::Vector2f halfsize = rect.getSize() / 2.0f;
sf::Vector2f circlePos = circle.getPosition();
sf::Vector2f rectPos = rect.getPosition();
// Only circle movement as rectangles don't move.
sf::Vector2f circleMovement = circle.getSpeed() * (elapsed / 1000.0f);
sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond) {
sf::Vector2f halfSize = rect.getSize() / 2.0f;
sf::Vector2f circleNewPos = circle.getPosition() + offsetFirst;
sf::Vector2f rectNewPos = rect.getPosition() + offsetSecond;
// We assume that rectangles are always axis aligned.
float overlapNoMovementX = Interval::IntervalFromRadius(circlePos.x, circle.getRadius())
.getOverlap(Interval::IntervalFromRadius(rectPos.x, halfsize.x)).getLength();
float overlapMovementX = Interval::IntervalFromRadius(circlePos.x + circleMovement.x, circle.getRadius())
.getOverlap(Interval::IntervalFromRadius(rectPos.x, halfsize.x)).getLength();
float overlapNoMovementY = Interval::IntervalFromRadius(circlePos.y, circle.getRadius())
.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))
// If circle center is inside rect on x plane, we just take y direction result.
if (Interval::IntervalFromRadius(rectNewPos.x, halfSize.x)
.isInside(circleNewPos.x)) {
float overlapY =
Interval::IntervalFromRadius(circleNewPos.y, circle.getRadius())
.getOverlap(Interval::IntervalFromRadius(rectNewPos.y, halfSize.y))
.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 {
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.
*/
bool
CollisionModel::testCollision(const Circle& first, const Circle& second,
int elapsed) {
sf::Vector2f axis = first.getPosition() - second.getPosition();
// 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 centerA = thor::dotProduct(axis, first.getPosition());
float radiusA = first.getRadius();
float movementA = thor::dotProduct(axis, first.getSpeed() * (elapsed / 1000.0f));
float centerB = thor::dotProduct(axis, second.getPosition());
float radiusB = second.getRadius();
float movementB = thor::dotProduct(axis, second.getSpeed() * (elapsed / 1000.0f));
sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond) {
sf::Vector2f axis(thor::unitVector(second.getPosition() + offsetFirst -
(first.getPosition() + offsetSecond)));
Interval projectedFirst = Interval::IntervalFromRadius(
thor::dotProduct(axis, first.getPosition() + offsetFirst),
first.getRadius());
Interval projectedSecond = Interval::IntervalFromRadius(
thor::dotProduct(axis, second.getPosition() + offsetSecond),
second.getRadius());
// Allow movement if sprites are moving apart.
return Interval::IntervalFromRadius(centerA, radiusA).getOverlap(
Interval::IntervalFromRadius(centerB, radiusB)).getLength() <
Interval::IntervalFromRadius(centerA + movementA, radiusA).getOverlap(
Interval::IntervalFromRadius(centerB + movementB, radiusB)).getLength();
float overlap = projectedFirst.getOverlap(projectedSecond).getLength();
if (overlap > 0)
offsetFirst -= overlap * axis;
return overlap > 0;
}
/**
* Tests for collision between two rectangles. Not implemented as these can't
* occur (rectangles can't move).
* Tests for collision between two rectangles. Always returns false as
* these can't occur (rectangles can't move).
*
* @return True if a collision occured.
*/
bool
CollisionModel::testCollision(const Rectangle& first, const Rectangle& second,
int elapsed) {
sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond) {
return false;
}

View File

@ -11,8 +11,12 @@
class Circle;
class Rectangle;
#include <SFML/System.hpp>
/**
* Abstract class providing helper functions to test for collisions between shapes.
*
* http://www.metanetsoftware.com/technique/tutorialA.html
*/
class CollisionModel {
public:
@ -20,11 +24,11 @@ public:
protected:
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,
int elapsed);
sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond);
static bool testCollision(const Rectangle& first, const Rectangle& second,
int elapsed);
sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond);
};
#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.
*/
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());
Circle* circle = dynamic_cast<Circle*>(other.get());
if (circle != nullptr)
return CollisionModel::testCollision(*circle, *this, elapsed);
return CollisionModel::testCollision(*circle, *this,
offsetFirst, offsetSecond);
else if (rect != nullptr)
return CollisionModel::testCollision(*rect, *this, elapsed);
return CollisionModel::testCollision(*rect, *this,
offsetFirst, offsetSecond);
else {
assert(false);
return false;

View File

@ -23,7 +23,8 @@ public:
const sf::Vector2f& direction = sf::Vector2f(0, 0));
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_ */

View File

@ -53,7 +53,8 @@ public:
bool collisionEnabled(Category category) 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);
protected: