2013-05-07 22:00:05 +00:00
|
|
|
/*
|
|
|
|
* CollisionModel.cpp
|
|
|
|
*
|
|
|
|
* Created on: 07.05.2013
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CollisionModel.h"
|
|
|
|
|
|
|
|
#include <SFML/System.hpp>
|
|
|
|
|
|
|
|
#include <Thor/Vectors.hpp>
|
|
|
|
|
|
|
|
#include "Circle.h"
|
|
|
|
#include "Rectangle.h"
|
|
|
|
#include "../util/Interval.h"
|
|
|
|
|
|
|
|
CollisionModel::~CollisionModel() {
|
|
|
|
}
|
2013-05-26 18:16:36 +00:00
|
|
|
|
2013-05-07 22:00:05 +00:00
|
|
|
/**
|
2013-05-26 18:16:36 +00:00
|
|
|
* 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.
|
2013-05-07 22:00:05 +00:00
|
|
|
*
|
2013-05-26 18:16:36 +00:00
|
|
|
* @param [in,out] offset The movement offset of the circle.
|
|
|
|
* @param offsetSecond Movement offset of the rectangle.
|
2013-05-07 22:00:05 +00:00
|
|
|
* @return True if a collision occured.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
CollisionModel::testCollision(const Circle& circle, const Rectangle& rect,
|
2013-05-26 18:16:36 +00:00
|
|
|
sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond) {
|
|
|
|
sf::Vector2f halfSize = rect.getSize() / 2.0f;
|
|
|
|
sf::Vector2f rectNewPos = rect.getPosition() + offsetSecond;
|
2013-07-09 20:29:56 +00:00
|
|
|
sf::Vector2f circleRotatedPos = circle.getPosition() + offsetFirst - rectNewPos;
|
|
|
|
circleRotatedPos = thor::rotatedVector(circleRotatedPos, -rect.mShape.getRotation());
|
|
|
|
circleRotatedPos += rectNewPos;
|
2013-05-07 22:00:05 +00:00
|
|
|
|
2013-05-26 18:16:36 +00:00
|
|
|
// If circle center is inside rect on x plane, we just take y direction result.
|
|
|
|
if (Interval::IntervalFromRadius(rectNewPos.x, halfSize.x)
|
2013-07-09 20:29:56 +00:00
|
|
|
.isInside(circleRotatedPos.x)) {
|
2013-05-26 18:16:36 +00:00
|
|
|
float overlapY =
|
2013-07-09 20:29:56 +00:00
|
|
|
Interval::IntervalFromRadius(circleRotatedPos.y, circle.getRadius())
|
2013-05-26 18:16:36 +00:00
|
|
|
.getOverlap(Interval::IntervalFromRadius(rectNewPos.y, halfSize.y))
|
2013-05-07 22:00:05 +00:00
|
|
|
.getLength();
|
2013-07-09 20:29:56 +00:00
|
|
|
offsetFirst += ((circleRotatedPos.y > rectNewPos.y) ? 1.0f : - 1.0f) *
|
|
|
|
thor::rotatedVector(sf::Vector2f(0, overlapY), rect.mShape.getRotation());
|
2013-05-26 18:16:36 +00:00
|
|
|
return overlapY > 0;
|
2013-05-07 22:00:05 +00:00
|
|
|
}
|
2013-05-26 18:16:36 +00:00
|
|
|
// Same here (just switched x/y).
|
|
|
|
else if (Interval::IntervalFromRadius(rectNewPos.y, halfSize.y)
|
2013-07-09 20:29:56 +00:00
|
|
|
.isInside(circleRotatedPos.y)) {
|
2013-05-26 18:16:36 +00:00
|
|
|
float overlapX =
|
2013-07-09 20:29:56 +00:00
|
|
|
Interval::IntervalFromRadius(circleRotatedPos.x, circle.getRadius())
|
2013-05-26 18:16:36 +00:00
|
|
|
.getOverlap(Interval::IntervalFromRadius(rectNewPos.x, halfSize.x))
|
|
|
|
.getLength();
|
2013-07-09 20:29:56 +00:00
|
|
|
offsetFirst += ((circleRotatedPos.x > rectNewPos.x) ? 1.0f : - 1.0f) *
|
|
|
|
thor::rotatedVector(sf::Vector2f(overlapX, 0), rect.mShape.getRotation());
|
2013-05-26 18:16:36 +00:00
|
|
|
return overlapX > 0;
|
2013-05-07 22:00:05 +00:00
|
|
|
}
|
2013-05-26 18:16:36 +00:00
|
|
|
// 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 {
|
2013-07-09 20:29:56 +00:00
|
|
|
sf::Vector2f axis(thor::unitVector(rectNewPos - circleRotatedPos));
|
2013-05-07 22:00:05 +00:00
|
|
|
|
2013-05-26 18:16:36 +00:00
|
|
|
// Use correct vector for corner projections (positive/negative
|
|
|
|
// direction does not matter).
|
|
|
|
float rectHalfSizeProjected;
|
2013-07-09 20:29:56 +00:00
|
|
|
if ((circleRotatedPos.x > rectNewPos.x && circleRotatedPos.y > rectNewPos.y) ||
|
|
|
|
(circleRotatedPos.x < rectNewPos.x && circleRotatedPos.y < rectNewPos.y))
|
2013-05-26 18:16:36 +00:00
|
|
|
rectHalfSizeProjected = thor::dotProduct(axis, halfSize);
|
|
|
|
else
|
|
|
|
rectHalfSizeProjected = thor::dotProduct(axis,
|
|
|
|
sf::Vector2f(halfSize.x, -halfSize.y));
|
|
|
|
|
|
|
|
Interval projectedCircle = Interval::IntervalFromRadius(
|
2013-07-09 20:29:56 +00:00
|
|
|
thor::dotProduct(axis, circleRotatedPos),
|
2013-05-26 18:16:36 +00:00
|
|
|
circle.getRadius());
|
|
|
|
Interval projectedRect = Interval::IntervalFromRadius(
|
|
|
|
thor::dotProduct(axis, rectNewPos),
|
|
|
|
rectHalfSizeProjected);
|
|
|
|
// using -5: works perfectly going between corner/side
|
2013-07-09 20:29:56 +00:00
|
|
|
// without -5 works perfectly on corner, but skips when going in between tiles
|
2013-05-26 18:16:36 +00:00
|
|
|
float overlap = projectedCircle.getOverlap(projectedRect).getLength() - 5;
|
|
|
|
if (overlap > 0)
|
2013-07-09 20:29:56 +00:00
|
|
|
offsetFirst -= thor::rotatedVector(overlap * axis, rect.mShape.getRotation());
|
2013-05-26 18:16:36 +00:00
|
|
|
return overlap > 0;
|
|
|
|
}
|
2013-05-07 22:00:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-26 18:16:36 +00:00
|
|
|
* 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.
|
2013-05-07 22:00:05 +00:00
|
|
|
*
|
2013-05-26 18:16:36 +00:00
|
|
|
* @param [in,out] offset The movement offset of the first circle.
|
|
|
|
* @param offsetSecond Movement offset of the second circle.
|
2013-05-07 22:00:05 +00:00
|
|
|
* @return True if a collision occured.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
CollisionModel::testCollision(const Circle& first, const Circle& second,
|
2013-05-26 18:16:36 +00:00
|
|
|
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());
|
2013-05-07 22:00:05 +00:00
|
|
|
|
2013-05-26 18:16:36 +00:00
|
|
|
float overlap = projectedFirst.getOverlap(projectedSecond).getLength();
|
|
|
|
if (overlap > 0)
|
|
|
|
offsetFirst -= overlap * axis;
|
|
|
|
return overlap > 0;
|
2013-05-07 22:00:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-26 18:16:36 +00:00
|
|
|
* Tests for collision between two rectangles. Always returns false as
|
|
|
|
* these can't occur (rectangles can't move).
|
2013-05-07 22:00:05 +00:00
|
|
|
*
|
|
|
|
* @return True if a collision occured.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
CollisionModel::testCollision(const Rectangle& first, const Rectangle& second,
|
2013-05-26 18:16:36 +00:00
|
|
|
sf::Vector2f& offsetFirst, const sf::Vector2f& offsetSecond) {
|
2013-05-07 22:00:05 +00:00
|
|
|
return false;
|
|
|
|
}
|