Example #1
0
void MovingObjectModel::RecalculatePath()
{
	//Only do stuff if the path contains elements.
	if(m_Path != NULL && !m_Path->empty())
	{
		auto next = m_Path->at(0);

		Point tempDirection = next - GetPosition();
		float distance = tempDirection.GetLength();
		tempDirection = tempDirection.GetNormalizedPoint();

		Point oppositDirection = m_Direction.GetNegatedPoint();
		oppositDirection = oppositDirection.GetNormalizedPoint();

		//If direction got negated, we overshot our target or if we have reached our next goal
		if(tempDirection == &oppositDirection || distance < 0.01f)
		{
			m_Path->erase(m_Path->begin());

			if(m_Path->empty())
			{
				m_MovementSpeed = 0;
			}
			else
			{
				next = m_Path->at(0);
				auto pos = GetPosition();

				Point newDirection = next - pos;
				newDirection = newDirection.GetNormalizedPoint();

				if(m_Owner != NULL)
				{
					m_Owner->setLookAt2D(newDirection.X, newDirection.Y);
				}
				else
				{
					SetDirection(newDirection.X, newDirection.Y);
				}
			}

		}
	}
}
void Voronoi::Thread :: _calculate( double x, double y, double z, double da[4], double pa[12])
{
	int xi = Floor(x); // Standard floor flushes the processor pipeline. This is from Global.h included in g3.h
	int yi = Floor(y);
	int zi = Floor(z);

	da[0] = da[1] = da[2] = da[3] = DBL_MAX;
	Point<double, 3> V;
	for(int xx=xi-1; xx<=xi+1; ++xx)
	{
		for(int yy=yi-1; yy<=yi+1; ++yy)
		{
			for(int zz=zi-1; zz<=zi+1; ++zz)
			{ // Things in this inner loop will happen 27 times:
				const float * p = _getPoint( xx, yy, zz);
				V = Point<double, 3>( x - (p[0] + xx), y - (p[1] + yy), z - (p[2] + zz));
				double d;
				switch (m_eDistance)
				{
				default:
				case eLength:// Euclidean (shortest line).
					d = V.GetLength();
					break;

				case eLength2:// The length squared. Saves the slow Square Root for some calculations.
					d = V.GetSquaredLength();
					break;
/*
				case eManhattan:// The length of the distance in axial directions (as if travelling around a city). Saves the slow Square Root for some calculations.
					d = V.GetManhattan();
					break;

				case eChebychev:// The length of the longest Axial journey. Saves the slow Square Root for some calculations.
					d = V.GetChebychev();
					break;

				case eQuadratic:// The sum of everything multiplied by everything else!
					d = V.GetQuadratic();
					break;
				case eMinkowski4:// Minkowski(4); pow(x*x*x*x+y*y*y*y+z*z*z*z,0.25);   p=4 in: pow(pow(x.abs(), p) + pow(y.abs(), p) + pow(z.abs(), M_E), 1.0/p);
					d = V.GetMinkowski( 4.0f);
					break;
				case eMinkowski5:// Same as Minkowski(0.5);
					d = V.GetMinkowski( 0.5f);
					break;
*/
				}

				if (d < da[0])
				{
					da[3] = da[2];
					da[2] = da[1];
					da[1] = da[0];

					da[0] = d; // Insert at 0

					pa[9] = pa[6];
					pa[10] = pa[7];
					pa[11] = pa[8];

					pa[6] = pa[3];
					pa[7] = pa[4];
					pa[8] = pa[5];

					pa[3] = pa[0];
					pa[4] = pa[1];
					pa[5] = pa[2];

					pa[0] = p[0] + xx;
					pa[1] = p[1] + yy;
					pa[2] = p[2]+zz;
				}
				else if (d < da[1])
				{
					da[3] = da[2];
					da[2] = da[1];

					da[1] = d;            // Insert at 1

					pa[9] = pa[6];
					pa[10] = pa[7];
					pa[11] = pa[8];

					pa[6] = pa[3];
					pa[7] = pa[4];
					pa[8] = pa[5];

					pa[3] = p[0] + xx;
					pa[4] = p[1] + yy;
					pa[5] = p[2] + zz;
				}
				else if (d < da[2])
				{
					da[3] = da[2];
					da[2] = d;                             // Insert at 2

					pa[9] = pa[6];
					pa[10] = pa[7];
					pa[11] = pa[8];

					pa[6] = p[0] + xx;
					pa[7] = p[1] + yy;
					pa[8] = p[2] + zz;
				}
				else if (d<da[3])
				{                                   // Insert at 3
					da[3] = d;
					pa[9] = p[0] + xx;
					pa[10] = p[1] + yy;
					pa[11] = p[2] + zz;
				}
			}
		}
	}
}