2012-12-22 13:56:17 +00:00
|
|
|
/*
|
|
|
|
* World.cpp
|
|
|
|
*
|
|
|
|
* Created on: 29.08.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "World.h"
|
|
|
|
|
2013-03-27 13:38:34 +00:00
|
|
|
#include <unordered_set>
|
|
|
|
#include <unordered_map>
|
2012-12-22 13:56:17 +00:00
|
|
|
|
2012-12-22 15:48:48 +00:00
|
|
|
#include <Thor/Vectors.hpp>
|
|
|
|
|
2013-04-04 20:44:15 +00:00
|
|
|
#include "util/Interval.h"
|
|
|
|
|
2013-04-05 14:58:37 +00:00
|
|
|
const float World::WALL_DISTANCE_MULTIPLIER = 1.5f;
|
2012-12-22 13:56:17 +00:00
|
|
|
/**
|
|
|
|
* Insert a drawable into the group. Drawables should only be handled with shared_ptr.
|
|
|
|
* An object can't be inserted more than once at the same level.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
World::insert(std::shared_ptr<Sprite> drawable) {
|
2013-03-03 20:55:15 +00:00
|
|
|
#ifndef NDEBUG
|
2012-12-23 14:50:49 +00:00
|
|
|
Sprite::Category cat = drawable->getCategory();
|
2012-12-22 13:56:17 +00:00
|
|
|
auto item = std::find(mDrawables[cat].begin(), mDrawables[cat].end(), drawable);
|
2013-03-03 20:55:15 +00:00
|
|
|
assert(item == mDrawables[cat].end());
|
|
|
|
#endif
|
|
|
|
mDrawables[drawable->getCategory()].push_back(drawable);
|
2012-12-22 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a drawable from the group.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
World::remove(std::shared_ptr<Sprite> drawable) {
|
|
|
|
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
|
|
|
|
auto item = std::find(v->second.begin(), v->second.end(), drawable);
|
2013-04-05 14:54:51 +00:00
|
|
|
if (item != v->second.end()) {
|
2012-12-22 13:56:17 +00:00
|
|
|
v->second.erase(item);
|
2013-04-05 14:54:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-12-22 13:56:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-29 16:59:35 +00:00
|
|
|
/**
|
|
|
|
* Inserts a character into the world. A character can only be inserted once.
|
|
|
|
* Also calls insert(character);
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
World::insertCharacter(std::shared_ptr<Character> character) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
auto item = std::find(mCharacters.begin(), mCharacters.end(), character);
|
|
|
|
assert(item == mCharacters.end());
|
|
|
|
#endif
|
|
|
|
mCharacters.push_back(character);
|
|
|
|
insert(character);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a character from the world.
|
|
|
|
* Also calls remove(character);
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
World::removeCharacter(std::shared_ptr<Character> character) {
|
|
|
|
auto item = std::find(mCharacters.begin(), mCharacters.end(), character);
|
2013-04-04 21:13:08 +00:00
|
|
|
if (item != mCharacters.end())
|
2013-03-29 16:59:35 +00:00
|
|
|
mCharacters.erase(item);
|
|
|
|
remove(character);
|
|
|
|
}
|
|
|
|
|
2013-03-27 13:38:34 +00:00
|
|
|
/**
|
|
|
|
* Generate path finding base data.
|
|
|
|
*
|
|
|
|
* Hardcoded as heuristic may be unnecessary with proper map generation.
|
|
|
|
*
|
|
|
|
* @warning Must not be run while getPath() is running (raw pointers).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
World::generateAreas() {
|
|
|
|
Area a;
|
|
|
|
|
|
|
|
a.area = sf::FloatRect(50, 50, 900, 300);
|
|
|
|
a.center = sf::Vector2f(500, 200);
|
|
|
|
mAreas.push_back(a);
|
|
|
|
a.area = sf::FloatRect(450, 350, 450, 100);
|
|
|
|
a.center = sf::Vector2f(675, 400);
|
|
|
|
mAreas.push_back(a);
|
|
|
|
a.area = sf::FloatRect(50, 450, 900, 500);
|
|
|
|
a.center = sf::Vector2f(500, 700);
|
|
|
|
mAreas.push_back(a);
|
|
|
|
|
|
|
|
Portal p1;
|
|
|
|
Portal p2;
|
|
|
|
std::vector<Portal> vp;
|
|
|
|
|
|
|
|
p1.start = sf::Vector2f(450, 350);
|
|
|
|
p1.end = sf::Vector2f(950, 350);
|
|
|
|
p1.area = &mAreas[1];
|
|
|
|
vp.push_back(p1);
|
|
|
|
mAreas[0].portals = vp;
|
|
|
|
|
|
|
|
vp.clear();
|
|
|
|
p2.start = sf::Vector2f(450, 450);
|
|
|
|
p2.end = sf::Vector2f(950, 450);
|
|
|
|
p2.area = &mAreas[1];
|
|
|
|
vp.push_back(p2);
|
|
|
|
mAreas[2].portals = vp;
|
|
|
|
|
|
|
|
vp.clear();
|
|
|
|
p1.area = &mAreas[0];
|
|
|
|
vp.push_back(p1);
|
|
|
|
p2.area = &mAreas[2];
|
|
|
|
vp.push_back(p2);
|
|
|
|
mAreas[1].portals = vp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Runs the A* path finding algorithm with areas as nodes and portals as edges.
|
|
|
|
*
|
|
|
|
* @warning Areas and portals must not be changed while this is running.
|
|
|
|
*
|
|
|
|
* @param start The area to start the path finding from. Must not be null.
|
|
|
|
* @param end The goal to reach. May be null.
|
|
|
|
* @return Path in reverse order (start being the last item and end the first).
|
|
|
|
*/
|
|
|
|
std::vector<World::Portal*>
|
|
|
|
World::astarArea(Area* start, Area* end) const {
|
|
|
|
assert(start);
|
2013-04-04 21:13:08 +00:00
|
|
|
if (!end)
|
2013-03-27 13:38:34 +00:00
|
|
|
return std::vector<World::Portal*>();
|
2013-04-04 21:13:08 +00:00
|
|
|
|
2013-04-04 21:00:52 +00:00
|
|
|
std::unordered_set<Area*> closed;
|
|
|
|
std::unordered_map<Area*, float> openAreasEstimatedCost;
|
|
|
|
// Navigated areas with previous area/portal.
|
|
|
|
std::unordered_map<Area*, std::pair<Area*, Portal*>> previousAreaAndPortal;
|
|
|
|
std::unordered_map<Area*, float> bestPathCost;
|
|
|
|
|
|
|
|
openAreasEstimatedCost[start] = heuristic_cost_estimate(start, end);
|
|
|
|
bestPathCost[start] = 0;
|
|
|
|
|
|
|
|
while (!openAreasEstimatedCost.empty()) {
|
|
|
|
Area* current = std::min_element(openAreasEstimatedCost.begin(),
|
|
|
|
openAreasEstimatedCost.end())->first;
|
2013-03-27 13:38:34 +00:00
|
|
|
if (current == end) {
|
|
|
|
std::vector<Portal*> path;
|
|
|
|
auto previous = current;
|
|
|
|
while (previous != start) {
|
2013-04-04 21:00:52 +00:00
|
|
|
path.push_back(previousAreaAndPortal[previous].second);
|
|
|
|
previous = previousAreaAndPortal[previous].first;
|
2013-03-27 13:38:34 +00:00
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2013-04-04 21:00:52 +00:00
|
|
|
openAreasEstimatedCost.erase(current);
|
|
|
|
closed.insert(current);
|
2013-03-27 13:38:34 +00:00
|
|
|
for (Portal& portal : current->portals) {
|
|
|
|
Area* neighbor = portal.area;
|
2013-04-04 21:00:52 +00:00
|
|
|
float tentative_g_score = bestPathCost[current] +
|
|
|
|
heuristic_cost_estimate(current,neighbor);
|
|
|
|
if (closed.find(neighbor) != closed.end()) {
|
2013-04-04 21:13:08 +00:00
|
|
|
if (tentative_g_score >= bestPathCost[neighbor])
|
2013-03-27 13:38:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-04-04 21:00:52 +00:00
|
|
|
if ((openAreasEstimatedCost.find(neighbor) ==
|
|
|
|
openAreasEstimatedCost.end()) ||
|
|
|
|
(tentative_g_score < bestPathCost[neighbor])) {
|
|
|
|
previousAreaAndPortal[neighbor] = std::make_pair(current,
|
|
|
|
&portal);
|
|
|
|
bestPathCost[neighbor] = tentative_g_score;
|
|
|
|
openAreasEstimatedCost[neighbor] = bestPathCost[neighbor] +
|
|
|
|
heuristic_cost_estimate(neighbor, end);
|
2013-03-27 13:38:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::vector<Portal*>();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns path in reverse order.
|
|
|
|
*
|
|
|
|
* @warning Areas and portals must not be changed while this running.
|
|
|
|
*
|
|
|
|
* @param start Position to start the path from.
|
|
|
|
* @param end Position to move to.
|
2013-03-27 15:09:01 +00:00
|
|
|
* @param radius Radius of the moving object.
|
2013-03-27 13:38:34 +00:00
|
|
|
* @return Path from end to start (path from start to end in reverse order).
|
|
|
|
*/
|
|
|
|
std::vector<sf::Vector2f>
|
|
|
|
World::getPath(const sf::Vector2f& start, const sf::Vector2f& end,
|
2013-03-27 15:09:01 +00:00
|
|
|
float radius) const {
|
2013-03-27 13:38:34 +00:00
|
|
|
std::vector<Portal*> portals = astarArea(getArea(start), getArea(end));
|
|
|
|
std::vector<sf::Vector2f> path;
|
|
|
|
|
|
|
|
path.push_back(end);
|
|
|
|
for (auto p : portals) {
|
|
|
|
// Find the point on the line of the portal closest to the previous point.
|
|
|
|
sf::Vector2f startToEnd = p->end - p->start;
|
|
|
|
float percentage = thor::dotProduct(startToEnd, path.back() - p->start) /
|
|
|
|
thor::squaredLength(startToEnd);
|
2013-03-27 15:06:57 +00:00
|
|
|
sf::Vector2f point;
|
|
|
|
|
2013-03-27 13:38:34 +00:00
|
|
|
if (percentage < 0 || percentage > 1.0f) {
|
2013-03-27 15:06:57 +00:00
|
|
|
if (thor::squaredLength(p->start - path.back()) <
|
|
|
|
thor::squaredLength(p->end - path.back())) {
|
2013-04-05 14:58:37 +00:00
|
|
|
thor::setLength(startToEnd, WALL_DISTANCE_MULTIPLIER * radius);
|
2013-03-27 15:06:57 +00:00
|
|
|
point = p->start + startToEnd;
|
2013-03-27 13:38:34 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-04-05 14:58:37 +00:00
|
|
|
thor::setLength(startToEnd, WALL_DISTANCE_MULTIPLIER * radius);
|
2013-03-27 15:06:57 +00:00
|
|
|
point = p->end - startToEnd;
|
2013-03-27 13:38:34 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-04 21:13:08 +00:00
|
|
|
else
|
2013-03-27 15:06:57 +00:00
|
|
|
point = p->start + startToEnd * percentage;
|
|
|
|
|
|
|
|
// Take two points on a line orthogonal to the portal.
|
2013-03-27 15:09:01 +00:00
|
|
|
thor::setLength(startToEnd, radius);
|
2013-03-27 15:06:57 +00:00
|
|
|
startToEnd = thor::perpendicularVector(startToEnd);
|
|
|
|
path.push_back(point + startToEnd);
|
|
|
|
path.push_back(point - startToEnd);
|
|
|
|
// Make sure the points are in the right order.
|
|
|
|
if (thor::squaredLength(*(path.end() - 1) - *(path.end() - 3) ) <
|
2013-04-04 21:13:08 +00:00
|
|
|
thor::squaredLength(*(path.end() - 2) - *(path.end() - 3) ))
|
2013-03-27 15:06:57 +00:00
|
|
|
std::swap(*(path.end() - 1), *(path.end() - 2));
|
2013-03-27 13:38:34 +00:00
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2013-03-29 17:48:49 +00:00
|
|
|
/**
|
|
|
|
* Returns all characters that are within maxDistance from position.
|
|
|
|
*/
|
|
|
|
std::vector<std::shared_ptr<Character> >
|
|
|
|
World::getCharacters(const sf::Vector2f& position, float maxDistance) const {
|
|
|
|
std::vector<std::shared_ptr<Character> > visible;
|
|
|
|
for (auto it : mCharacters) {
|
2013-04-05 14:23:09 +00:00
|
|
|
if (position == it->getPosition())
|
|
|
|
continue;
|
2013-03-29 17:48:49 +00:00
|
|
|
if (thor::squaredLength(position - it->getPosition()) <=
|
2013-04-04 21:13:08 +00:00
|
|
|
maxDistance * maxDistance)
|
2013-03-29 17:48:49 +00:00
|
|
|
visible.push_back(it);
|
|
|
|
}
|
|
|
|
return visible;
|
|
|
|
}
|
2013-03-27 13:38:34 +00:00
|
|
|
/**
|
|
|
|
* Returns the linear distance between two areas (using their center).
|
|
|
|
*/
|
|
|
|
float
|
|
|
|
World::heuristic_cost_estimate(Area* start, Area* end) const {
|
|
|
|
return thor::length(end->center - start->center);
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:55:15 +00:00
|
|
|
/**
|
|
|
|
* Checks for collisions and applies movement, also removes sprites if
|
|
|
|
* Sprite::getDelete returns true.
|
|
|
|
*
|
|
|
|
* This method can be improved by only testing each pair of sprites once,
|
|
|
|
* and using the result for both. Applying movement should be done in
|
|
|
|
* testCollision, always applying the part that causes no collision.
|
|
|
|
*/
|
2012-12-22 13:56:17 +00:00
|
|
|
void
|
|
|
|
World::step(int elapsed) {
|
2012-12-22 15:48:48 +00:00
|
|
|
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
|
2013-04-12 18:31:43 +00:00
|
|
|
for (auto it = v->second.begin(); it != v->second.end(); it++) {
|
2013-03-03 20:55:15 +00:00
|
|
|
auto spriteA = *it;
|
2013-04-12 18:31:43 +00:00
|
|
|
if (spriteA->getDelete()) {
|
2013-03-03 20:55:15 +00:00
|
|
|
remove(spriteA);
|
2013-04-12 18:31:43 +00:00
|
|
|
it--;
|
|
|
|
}
|
|
|
|
// Apply movement for movable sprites.
|
|
|
|
else if ((*it)->isMovable()) {
|
2013-03-03 20:55:15 +00:00
|
|
|
sf::Vector2f speed = spriteA->getSpeed();
|
|
|
|
speed *= elapsed / 1000.0f;
|
|
|
|
bool overlap = false;
|
|
|
|
for (auto w = mDrawables.begin(); w != mDrawables.end(); w++) {
|
|
|
|
for (auto spriteB : w->second) {
|
2013-04-04 21:13:08 +00:00
|
|
|
if (spriteA == spriteB)
|
2013-03-03 20:55:15 +00:00
|
|
|
continue;
|
|
|
|
// Ignore anything that is filtered by masks.
|
|
|
|
if (!spriteA->collisionEnabled(spriteB->getCategory()) ||
|
2013-04-04 21:13:08 +00:00
|
|
|
!spriteB->collisionEnabled(spriteA->getCategory()))
|
2013-03-03 20:55:15 +00:00
|
|
|
continue;
|
|
|
|
if (testCollision(spriteA, spriteB, elapsed)) {
|
|
|
|
spriteA->onCollide(spriteB);
|
|
|
|
overlap = true;
|
|
|
|
}
|
2013-03-03 20:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-04 21:13:08 +00:00
|
|
|
if (!overlap)
|
2013-03-03 20:55:15 +00:00
|
|
|
spriteA->setPosition(spriteA->getPosition() + speed);
|
2013-03-03 20:28:33 +00:00
|
|
|
}
|
2012-12-22 15:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-22 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2013-03-29 16:59:35 +00:00
|
|
|
/**
|
2013-04-05 14:23:09 +00:00
|
|
|
* Calls Character::onThink for each character. Must be called
|
|
|
|
* before step (due to character removal).
|
2013-03-29 16:59:35 +00:00
|
|
|
*
|
|
|
|
* @param elapsed Time since last call.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
World::think(int elapsed) {
|
2013-04-05 14:54:51 +00:00
|
|
|
for (auto it = mCharacters.begin(); it != mCharacters.end(); ) {
|
|
|
|
if ((*it)->getDelete())
|
|
|
|
removeCharacter(*it);
|
|
|
|
else {
|
|
|
|
(*it)->onThink(elapsed);
|
|
|
|
it++;
|
|
|
|
}
|
2013-03-29 16:59:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-03 20:28:33 +00:00
|
|
|
/**
|
|
|
|
* Tests for collisions using Seperating Axis Theorem (SAT).
|
|
|
|
*
|
|
|
|
* http://www.metanetsoftware.com/technique/tutorialA.html
|
|
|
|
*
|
|
|
|
* @param spriteA, spriteB Pair of sprites which to test for collision/overlapping.
|
|
|
|
* @param elapsed Time elapsed in this step.
|
|
|
|
* @return True if both sprites will be overlapping after their current movement.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
World::testCollision(std::shared_ptr<Sprite> spriteA,
|
|
|
|
std::shared_ptr<Sprite> spriteB, int elapsed) const {
|
|
|
|
// circle-circle collision
|
|
|
|
if ((spriteA->mShape.type == Sprite::Shape::Type::CIRCLE) &&
|
|
|
|
(spriteB->mShape.type == Sprite::Shape::Type::CIRCLE)) {
|
|
|
|
|
|
|
|
sf::Vector2f axis = spriteA->getPosition() - spriteB->getPosition();
|
|
|
|
// If both objects are at the exact same position, allow any movement for unstucking.
|
2013-04-04 21:13:08 +00:00
|
|
|
if (axis == sf::Vector2f())
|
2013-03-03 20:28:33 +00:00
|
|
|
return true;
|
|
|
|
axis = thor::unitVector(axis);
|
|
|
|
float centerA = thor::dotProduct(axis, spriteA->getPosition());
|
2013-03-19 19:51:56 +00:00
|
|
|
float radiusA = spriteA->getRadius();
|
2013-03-03 20:28:33 +00:00
|
|
|
float movementA = thor::dotProduct(axis, spriteA->getSpeed() * (elapsed / 1000.0f));
|
|
|
|
float centerB = thor::dotProduct(axis, spriteB->getPosition());
|
2013-03-19 19:51:56 +00:00
|
|
|
float radiusB = spriteB->getRadius();
|
2013-03-03 20:28:33 +00:00
|
|
|
float movementB = thor::dotProduct(axis, spriteB->getSpeed() * (elapsed / 1000.0f));
|
|
|
|
|
|
|
|
// Allow movement if sprites are moving apart.
|
2013-04-04 20:44:15 +00:00
|
|
|
return Interval::IntervalFromRadius(centerA, radiusA).getOverlap(
|
|
|
|
Interval::IntervalFromRadius(centerB, radiusB)).getLength() <
|
|
|
|
Interval::IntervalFromRadius(centerA + movementA, radiusA).getOverlap(
|
|
|
|
Interval::IntervalFromRadius(centerB + movementB, radiusB)).getLength();
|
2013-03-03 20:28:33 +00:00
|
|
|
}
|
|
|
|
// circle-rect collision
|
|
|
|
if (((spriteA->mShape.type == Sprite::Shape::Type::CIRCLE) &&
|
|
|
|
(spriteB->mShape.type == Sprite::Shape::Type::RECTANGLE)) ||
|
|
|
|
((spriteA->mShape.type == Sprite::Shape::Type::RECTANGLE) &&
|
|
|
|
(spriteB->mShape.type == Sprite::Shape::Type::CIRCLE))) {
|
|
|
|
std::shared_ptr<Sprite> circle = spriteA;
|
|
|
|
std::shared_ptr<Sprite> rect = spriteB;
|
2013-04-04 21:13:08 +00:00
|
|
|
if (circle->mShape.type != Sprite::Shape::Type::CIRCLE)
|
2013-03-03 20:28:33 +00:00
|
|
|
std::swap(circle, rect);
|
2013-03-19 19:51:56 +00:00
|
|
|
float radius = circle->getRadius();
|
2013-03-04 23:36:22 +00:00
|
|
|
sf::Vector2f halfsize = rect->getSize() / 2.0f;
|
2013-03-03 20:28:33 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
// We assume that rectangles are always axis aligned.
|
2013-04-04 20:44:15 +00:00
|
|
|
float overlapNoMovementX = Interval::IntervalFromRadius(circlePos.x, radius)
|
|
|
|
.getOverlap(Interval::IntervalFromRadius(rectPos.x, halfsize.x)).getLength();
|
|
|
|
float overlapMovementX = Interval::IntervalFromRadius(circlePos.x + circleMovement.x, radius)
|
|
|
|
.getOverlap(Interval::IntervalFromRadius(rectPos.x, halfsize.x)).getLength();
|
|
|
|
float overlapNoMovementY = Interval::IntervalFromRadius(circlePos.y, radius)
|
|
|
|
.getOverlap(Interval::IntervalFromRadius(rectPos.y, halfsize.y)).getLength();
|
|
|
|
float overlapMovementY = Interval::IntervalFromRadius(circlePos.y + circleMovement.y, radius)
|
|
|
|
.getOverlap(Interval::IntervalFromRadius(rectPos.y, halfsize.y)).getLength();
|
2013-03-04 23:36:22 +00:00
|
|
|
|
|
|
|
bool xyCollisionResult = (((overlapNoMovementX < overlapMovementX) &&
|
|
|
|
(overlapNoMovementY > 0)) ||
|
2013-03-03 20:28:33 +00:00
|
|
|
((overlapNoMovementY < overlapMovementY) && (overlapNoMovementX > 0)));
|
2013-03-04 23:36:22 +00:00
|
|
|
// If circle center is overlapping rectangle on x or y axis, we can take xyCollisionResult.
|
2013-04-04 20:44:15 +00:00
|
|
|
if (Interval::IntervalFromRadius(rectPos.x, halfsize.x).isInside(circlePos.x) ||
|
2013-04-04 21:13:08 +00:00
|
|
|
Interval::IntervalFromRadius(rectPos.y, halfsize.y).isInside(circlePos.y))
|
2013-03-04 23:36:22 +00:00
|
|
|
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 = circle->getPosition() - rect->getPosition();
|
|
|
|
// If both objects are at the exact same position, allow any
|
|
|
|
// movement for unstucking.
|
2013-04-04 21:13:08 +00:00
|
|
|
if (axis == sf::Vector2f())
|
2013-03-04 23:36:22 +00:00
|
|
|
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.
|
2013-04-04 20:44:15 +00:00
|
|
|
return Interval::IntervalFromRadius(circlePosProjected, radius)
|
|
|
|
.getOverlap(Interval::IntervalFromRadius(rectPosProjected,
|
|
|
|
rectHalfWidthProjected))
|
2013-03-04 23:36:22 +00:00
|
|
|
.getLength() <
|
2013-04-04 20:44:15 +00:00
|
|
|
Interval::IntervalFromRadius(circlePosProjected + movementProjected, radius)
|
|
|
|
.getOverlap(Interval::IntervalFromRadius(rectPosProjected,
|
|
|
|
rectHalfWidthProjected))
|
2013-03-04 23:36:22 +00:00
|
|
|
.getLength();
|
|
|
|
}
|
|
|
|
// If there is no collision on x and y axis, there can't be one at all.
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
2013-03-03 20:28:33 +00:00
|
|
|
}
|
|
|
|
// Rectangles can't move and thus not collide.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-27 13:38:34 +00:00
|
|
|
/**
|
|
|
|
* Returns the area where point is in.
|
|
|
|
* Just iterates through all areas and tests each.
|
|
|
|
*/
|
|
|
|
World::Area*
|
|
|
|
World::getArea(const sf::Vector2f& point) const {
|
|
|
|
for (auto area = mAreas.begin(); area != mAreas.end(); area++) {
|
2013-04-04 21:13:08 +00:00
|
|
|
if (area->area.contains(point))
|
2013-03-27 13:38:34 +00:00
|
|
|
// Make the return value non-const for convenience.
|
|
|
|
return &const_cast<Area&>(*area);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-12-22 13:56:17 +00:00
|
|
|
/**
|
|
|
|
* Draws all elements in the group.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
World::draw(sf::RenderTarget& target, sf::RenderStates states) const {
|
|
|
|
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
|
|
|
|
for (auto item : v->second) {
|
|
|
|
target.draw(static_cast<sf::Drawable&>(*item), states);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|