void TrackingBruteForce::pairAndPopulate(std::list<Object> & candidatePrev, std::list<Object> & candidateCurr, std::vector<Object> & destination){
     // Pair closest points and remove from temporary lists.
     double error = 0;
     while(error <= maximumDistance && !candidatePrev.empty() && !candidateCurr.empty()) {
         double smallestError = 1e99;
         std::list<Object>::iterator bestPrev, bestCurr;
         for(std::list<Object>::iterator previous = candidatePrev.begin(); previous != candidatePrev.end(); previous++) {
             for(std::list<Object>::iterator current = candidateCurr.begin(); current != candidateCurr.end(); current++) {
                 if(squaredDistance(*previous, *current) < smallestError) {
                     smallestError = squaredDistance(*previous, *current);
                     bestPrev = previous;
                     bestCurr = current;
                 }
             }
         }
         if(smallestError <= maximumDistance) {
             bestCurr->merge(&*bestPrev);
             bestCurr->lost = false;
             destination.push_back(*bestCurr);
             candidatePrev.erase(bestPrev);
             candidateCurr.erase(bestCurr);
         }
         error = smallestError;
     }
 } //End of pairAndPopulate
示例#2
0
int getSnapRotation(const QPointF &scenePos)
{
    qreal halfItemSize = ITEM_SIZE / 2.0;
    QPointF gridPos = ArenaScene::nearestGridPoint(scenePos);
    QPointF top    = gridPos - QPointF(0, halfItemSize);
    QPointF bottom = gridPos + QPointF(0, halfItemSize);
    QPointF left  = gridPos - QPointF(halfItemSize, 0);
    QPointF right = gridPos + QPointF(halfItemSize, 0);

    qreal distTop = squaredDistance(scenePos, top);
    qreal distBottom = squaredDistance(scenePos, bottom);
    qreal distLeft = squaredDistance(scenePos, left);
    qreal distRight = squaredDistance(scenePos, right);

    qreal minDist = qMin(distTop, qMin(distBottom, qMin(distLeft, distRight)));

    if (minDist == distTop)
        return 0;
    if (minDist == distRight)
        return 90;
    if (minDist == distBottom)
        return 180;
    //if (minDist == distLeft)
        return 270;
}
	void WobbleNodeAnimator::buildQuadVertex(MyGUI::VectorQuadData& _data)
	{
		int count_w = getCountX();
		int count_h = getCountY();

		for (int rx=0; rx<count_w+1; rx++)
		{
			for (int ry=0; ry<count_h+1; ry++)
			{
				MyGUI::FloatPoint point((float)rx / (float)count_w, (float)ry / (float)count_h);

				float drageffect = 0;
				if (mInertiaMode)
				{
					float drageffect1 = squaredDistance(point, MyGUI::FloatPoint(0, 0)) * mResizeStrength;
					float drageffect2 = squaredDistance(point, MyGUI::FloatPoint(1, 1)) * mResizeStrength;

					drageffect = std::min(drageffect1, drageffect2);
				}
				else
				{
					drageffect = squaredDistance(mInertiaPoint, point) * mDragStrength;
				}

				float fx = getLeft() + getWidth() * point.left;
				float fy = getTop() + getHeight() * point.top;

				MyGUI::FloatPoint vert(fx + (-mDragOffset.left) * drageffect, fy + mDragOffset.top * drageffect);

				if (rx < count_w && ry < count_h)
				{
					_data[rx + ry*count_w].vertex[MyGUI::QuadData::CornerLT].x = vert.left;
					_data[rx + ry*count_w].vertex[MyGUI::QuadData::CornerLT].y = vert.top;
				}

				if (rx > 0 && ry > 0)
				{
					_data[(rx-1) + (ry-1)*count_w].vertex[MyGUI::QuadData::CornerRB].x = vert.left;
					_data[(rx-1) + (ry-1)*count_w].vertex[MyGUI::QuadData::CornerRB].y = vert.top;
				}

				if (rx > 0 && ry < count_h)
				{
					_data[(rx-1) + ry*count_w].vertex[MyGUI::QuadData::CornerRT].x = vert.left;
					_data[(rx-1) + ry*count_w].vertex[MyGUI::QuadData::CornerRT].y = vert.top;
				}

				if (rx < count_w && ry > 0)
				{
					_data[rx + (ry-1)*count_w].vertex[MyGUI::QuadData::CornerLB].x = vert.left;
					_data[rx + (ry-1)*count_w].vertex[MyGUI::QuadData::CornerLB].y = vert.top;
				}
			}
		}
	}
void CWarZombieAttackState::Execute(CWarZombie * pMonster, float fFrameTime)
{
	const Real targetDisSq = pPlayer->getPosition().squaredDistance(pMonster->getPosition());
	if (targetDisSq > attackRangeSq)
	{
		pMonster->getStateMachine()->ChangeState(&CWarZombieIdleState::getInstance());
	}
	else
	{
		auto timePos = pMonster->getAnimState()->getTimePosition();
		if (0.7f < timePos && timePos < 0.8f)
		{
			if (pMonster->isAttackDelay()) return;

			auto player = InGameState::getInstance()->getPlayer();
			auto pos = pMonster->getPosition();
			auto targetPos = player->getPosition();
			if (targetPos.squaredDistance(pos) < attackRangeSq)
			{
				player->damaged(8);
				pMonster->setAttackDelay(true);
			}
		}
		else
			pMonster->setAttackDelay(false);
	}
}
示例#5
0
bool CombatSystem::enemy_in_range(std::size_t id)
{
	FACTION friendly_faction = FactionHelper::get_faction(entities_, id);
	FACTION enemy_faction{};

	switch(friendly_faction)
	{
		case FACTION::FRIENDLY:
			enemy_faction = FACTION::ENEMY;
			break;
		case FACTION::ENEMY:
			enemy_faction = FACTION::FRIENDLY;
			break;
		case FACTION::NEUTRAL:
			return false;
	}

	auto range = CombatHelper::get_range(entities_, id);
	range *= range;
	auto position = PhysicsHelper::get_2d_position(entities_, id);
	for(const auto& ent : entities_.get_component_container<FactionComponent>())
	{
		if(ent.second.faction == enemy_faction &&
		   position.squaredDistance(PhysicsHelper::get_2d_position(entities_, ent.first)) <= range)
		{
			return true;
		}
	}
	return false;
}
示例#6
0
glm::vec3 radiance (const Ray & r, int countdown = 6)
{
    float t;
    Object* o = intersect(r, t);
    if(t == noIntersect)
        return glm::vec3{0,0,0};

    // Le point et sa normale
    glm::vec3 p = r.origin+t*r.direction;
    glm::vec3 normale = o->getNormale(p);

    // Choix d'un point aléatoire sur une sphère de lumière
    glm::vec3 lightPToSphere = scene::light-p;
    glm::vec3 dirToSphere = glm::normalize(lightPToSphere);
    float pdf;
    glm::vec3 lightOnSphere = random_light(dirToSphere, pdf);


    //std::cout << scene::light.x << "," << scene::light.y << "," << scene::light.z << " / " << lightOnSphere.x << "," << lightOnSphere.y << "," << lightOnSphere.z << std::endl;
    // Intersection entre le point et la lumière
    glm::vec3 lightP = (scene::light+lightOnSphere)-p;
    glm::vec3 dir = glm::normalize(lightP);
    glm::vec3 p2 = p+dir*0.1f;
    Ray ray{p2,dir};
    intersect(ray, t);

    // Rayon réfracté par la surface
    glm::vec3 ex = reflect(-r.direction, normale);
    glm::vec3 pEx = p + ex*0.1f;
    Ray rayEx{pEx, ex};
    float light = 1.0f;

    // Si intersection avant lumière alors pas d'éclairage direct
    if(t*t < squaredDistance(lightP))
        light = 0;

    double angle = glm::dot(normale,dir);

    //return o->albedo()*energie;

    if(countdown > 0)
        return o->albedo()*o->bsdf(std::abs(angle))*light*scene::lightColor/squaredDistance(lightP)/pdf + o->albedo()*o->indirect(r, rayEx, p, normale, --countdown);
    else
        return o->albedo()*o->bsdf(std::abs(angle))*light*scene::lightColor/squaredDistance(lightP)/pdf;
}
示例#7
0
tdt::real PhysicsHelper::get_distance(EntitySystem& ents, tdt::uint id1, tdt::uint id2)
{
	PhysicsComponent* comp1{nullptr};
	GET_COMPONENT(id1, ents, comp1, PhysicsComponent);
	auto comp2 = ents.get_component<PhysicsComponent>(id2);
	if(comp1 && comp2)
	{
		auto pos1 = comp1->position;
		auto pos2 = comp2->position;
		pos1.y = pos2.y = 0;
		return pos1.squaredDistance(pos2);
	}
	else
		return std::numeric_limits<tdt::real>::max();
}
示例#8
0
QPointF getRotationSnapPoint(const QPointF &scenePos)
{
    QPointF gridPos = ArenaScene::nearestGridPoint(scenePos);
    QPointF top = gridPos + QPointF(ITEM_SIZE / 2.0, 0.0);
    QPointF bottom = gridPos + QPointF(ITEM_SIZE / 2.0, ITEM_SIZE);
    QPointF left = gridPos + QPointF(0.0, ITEM_SIZE / 2.0);
    QPointF right = gridPos + QPointF(ITEM_SIZE, ITEM_SIZE / 2.0);

    qreal distTop = squaredDistance(scenePos, top);
    qreal distBottom = squaredDistance(scenePos, bottom);
    qreal distLeft = squaredDistance(scenePos, left);
    qreal distRight = squaredDistance(scenePos, right);

    qreal minDist = qMin(distTop, qMin(distBottom, qMin(distLeft, distRight)));

    if (minDist == distTop)
        return top;
    if (minDist == distRight)
        return right;
    if (minDist == distBottom)
        return bottom;
    //if (minDist == distLeft)
        return left;
}
示例#9
0
index_t findNearestSite(const Vector2 p[], index_t l)
{
	//とりあえず素朴な実装
	if (l == 0)
	{
		assert(!"input number larger than 0");
		return 0;
	}
	const Vector2& q = p[l];
	index_t result;
	float* d2 = new float[l];
	//float* d2 = static_cast<float*>(_alloca(l * sizeof (float)));
	{
		std::transform(p, p + l, d2, [&q](const Vector2& p_i)
		{
			return squaredDistance(p_i, q);
		});
		result = std::distance(d2, std::min_element(d2, d2 + l));
	}
	//_freea(d2);
	delete[] d2;
	return result;
}
示例#10
0
T distance(const vec3<T>& a, const vec3<T>& b){
    return sqrt(squaredDistance(a,b));
}
示例#11
0
文件: Vector3.cpp 项目: luk2010/aproe
 Real Vector3::distance(const Vector3& v) const
 {
     return Math::Sqrt(squaredDistance(v));
 }
示例#12
0
	float Distance(PointT a, PointT b){
		return std::sqrt(squaredDistance(a,b));
	}
示例#13
0
int PersistentMoveFSM::update() {
  int moveStatus;
  switch (state) {
    case IDLE:
      tempParams.clear();
      tempParams.push_back(targetLoc.x);
      tempParams.push_back(targetLoc.y);
      moveFSM->init(tempParams);
      moveStatus = moveFSM->update();
      state = MOVE;
      break;
    case MOVE:
      moveStatus = moveFSM->update();
      if (moveStatus == FSM_RUNNING) {
        // do nothing
      }
      else if (moveStatus == FSM_FAILURE) {
        if (squaredDistance(*gob->sod.x, *gob->sod.y, targetLoc.x, targetLoc.y)
            <= tolerance) {
          return FSM_SUCCESS;
        }
        repathCount++;
        if (repathCount >= MAX_REPATHS) {
          msg << "repath count exceeded " << repathCount << endl;
          return FSM_FAILURE;
        }
        msg << "repathing, count " << repathCount << endl;
        tempParams.push_back(targetLoc.x);
        tempParams.push_back(targetLoc.y);
        moveFSM->init(tempParams);
      }
      else if (moveStatus == FSM_STUCK) {
     //   unreachableCount++;
     //   msg << "unreachable count" << unreachableCount << endl;
     //   if (unreachableCount >= MAX_UNREACHABLE) {
     //     return FSM_FAILURE;
     //   }
        moveFSM->panic();
        moveFSM->update();
        state = PANIC;
        msg << "panic starting..\n";
        panicUpdateCount = 0;
      }
      else if (moveStatus == FSM_UNREACHABLE) {
        return FSM_FAILURE;
      }
      else if (moveStatus == FSM_SUCCESS) {
        return FSM_SUCCESS;
      }
      else {
        ASSERT(false);
      }
      break;
    case PANIC:
      moveStatus = moveFSM->update();
      if (panicUpdateCount++ < 4 and
          moveStatus == FSM_RUNNING || moveStatus == FSM_SUCCESS) {
        msg << "panic succeeded.\n";
        // moving! reinit to the right coords
        state = IDLE;
      }
      else if (moveStatus == FSM_RUNNING) {
        msg << "panic in progress.\n";
      }
      else {
        moveFSM->panic();
        moveFSM->update();
        msg << "panic again..\n";
      }
      break;
      
      
  }
  
  return FSM_RUNNING;
}
示例#14
0
//Determines collision. 
//Uses squared values to avoid sqrt function.
bool cModel::SphereSphereCollision(glm::vec3 otherPosition, float otherRadius)
{
	const float squaredSumRadius = pow((m_mdlRadius + otherRadius), 2);
	return (squaredSumRadius > squaredDistance(otherPosition));
}
示例#15
0
	float Distance(PointT a){
		return std::sqrt(squaredDistance(a));
	}
示例#16
0
 inline float LineSegment2::squaredLength() const
 {
     return squaredDistance(p1, p2);
 }
示例#17
0
 inline bool intersects(Circle2 const &c1, Circle2 const &c2)
 {
     return (squaredDistance(c1.center, c2.center) <=
             square(c1.radius + c2.radius));
 }
示例#18
0
 inline bool intersects(Circle2 const &c, Vector2 const &p)
 {
     return squaredDistance(c.center, p) <= square(c.radius);
 }