bool CircleCollider::IntersectsLine(const Vector2& start, const Vector2& end, float lineRadius, CollisionData *collisionData) { //Algorithm stolen from: http://www.gamedev.net/topic/304578-finite-line-circle-intersection/page__view__findpost__p__2938618 Vector2 ePos = GetEntityPosition(); Vector2 dir = end - start; Vector2 diff = (ePos + offset) - start; float t = diff.Dot(dir) / dir.GetSquaredMagnitude(); if (t < 0.0f) t = 0.0f; if (t > 1.0f) t = 1.0f; Vector2 closest = (t * dir) + start; Vector2 d = (ePos + offset) - closest; if (collisionData) { collisionData->hitPoint = closest; } return d.GetSquaredMagnitude() <= (radius + lineRadius) * (radius + lineRadius); }
Entity* Scene::GetNearestEntityWithTag(const Vector2 &position, const std::string &tag) { float smallestSqrMag = -1.0f; Entity *nearestEntity = NULL; for (std::list<Entity*>::iterator i = tagMap[tag].begin(); i != tagMap[tag].end(); ++i) { Vector2 diff = (*i)->position - position; float sqrMag = diff.GetSquaredMagnitude(); if (smallestSqrMag <= -1 || sqrMag < smallestSqrMag) { nearestEntity = (*i); } } return nearestEntity; }
Entity* Scene::GetNearestEntityByControlPoint(const Vector2 &position, const std::string &tag, Entity *ignoreEntity) { float smallestSqrMag = -1.0f; Entity *nearestEntity = NULL; std::list<Entity*> *entities = &this->entities; if (tag != "") { entities = GetAllTag(tag); } if (entities != NULL) { for (std::list<Entity*>::iterator i = entities->begin(); i != entities->end(); ++i) { if ((*i) != ignoreEntity) { Vector2 diff = (*i)->GetWorldPosition() - position; if (diff.IsInRange(ENTITY_CONTROLPOINT_SIZE)) { float sqrMag = diff.GetSquaredMagnitude(); if (smallestSqrMag == -1 || sqrMag < smallestSqrMag) { smallestSqrMag = sqrMag; nearestEntity = (*i); } } } //Entity *nearestChild = (*i)->GetNearestEntityByControlPoint(position, tag, ignoreEntity, smallestSqrMag); //if (nearestChild) //{ // nearestEntity = nearestChild; //} } } return nearestEntity; }
Entity* Scene::GetNearestEntity(const Vector2 &position, Entity *ignoreEntity) { float smallestSqrMag = -1.0f; Entity *nearestEntity = NULL; for (std::list<Entity*>::iterator i = entities.begin(); i != entities.end(); ++i) { if ((*i) != ignoreEntity) { Vector2 diff = (*i)->position - position; float sqrMag = diff.GetSquaredMagnitude(); if (smallestSqrMag <= -1 || sqrMag < smallestSqrMag) { nearestEntity = (*i); } } } return nearestEntity; }