bool S013010C_Aaron_Smith_Tank::RotateHeadingToTarget(Vector2D targetPos)
{
	Vector2D toTarget = Vec2DNormalize(GetCentralPosition() - targetPos);

	//Determine the angle between the heading vector and the target.

	double angle = atan2(toTarget.y - mHeading.y,toTarget.x - mHeading.x);

	
	//cout << angle << endl;
	angle *= (180 / Pi);
	//Return true if the player is facing the target.
	/*if (angle < -0.007)
		return true;*/

	if (angle < 0)
	{
		angle = 360 - (-angle);
	}

	RotateHeadingByRadian(angle, mHeading.Sign(toTarget));

	return true;
}
void S013010C_Aaron_Smith_Tank::MoveInHeadingDirection(float deltaTime)
{
	
	mSteeringForce = mSteering->Calculate(deltaTime);
	//Acceleration = Force/Mass
	//mSteeringForce.Truncate(GetMaxForce());
	Vector2D acceleration = mSteeringForce / GetMass();
	
	//Update velocity.
	mVelocity += acceleration * deltaTime;

	//Don't allow the tank does not go faster than max speed.
	mVelocity.Truncate(GetMaxSpeed());
	//cout << "mVelocity " << mVelocity.x << ":" << mVelocity.y << endl;
	//cout << "VELOCITY " << mVelocity.Length() << endl;

	
	if (mVelocity.Length() > 0.0001)
	{
		if (mVelocity.x != mVelocity.x || mVelocity.y != mVelocity.y) return;
		Vector2D newPosition = GetPosition();
		newPosition.x += mVelocity.x*deltaTime;
		newPosition.y += (mVelocity.y)*deltaTime;	//Y flipped as adding to Y moves down screen.
		SetPosition(newPosition);

		Vector2D ahead = Vec2DNormalize(mVelocity);
		if (ahead.Length() == 0)
			ahead = mHeading;

		Vector2D cenPos = GetCentrePosition();
		Vector2D aheadDistance = cenPos + ahead * 10 ;
		
		RotateHeadingToTarget(aheadDistance);

	//	double dynamicLength = mVelocity.Length() / GetMaxSpeed();
		

		Vector2D left = (mHeading - mSide) * 0.5;
		Vector2D right = (mHeading + mSide) * 0.5;

		mTopLeftCorner.x = left.x;
		mTopLeftCorner.y = left.y;
		mTopLeftCorner = Vec2DNormalize(mTopLeftCorner) * -10;
		mTopLeftCorner += GetCentralPosition();

		mTopRightCorner.x = right.x;
		mTopRightCorner.y = right.y;
		mTopRightCorner = Vec2DNormalize(mTopRightCorner) * -10;
		mTopRightCorner += GetCentralPosition();



		/*double angle = 70;
		int ahead2Amount = 40;
		int ahead1Amount = ahead2Amount / 2;
		int side2Amount = 30;
		int side1Amount = side2Amount / 2;

		Vector2D fannedLeftAhead;
		fannedLeftAhead.x = ahead.x * cos(DegsToRads(-angle)) - ahead.y * sin(DegsToRads(-angle));
		fannedLeftAhead.y = ahead.x * sin(DegsToRads(-angle)) + ahead.y * cos(DegsToRads(-angle));
		Vector2D fannedRightAhead;
		fannedRightAhead.x = ahead.x * cos(DegsToRads(angle)) - ahead.y * sin(DegsToRads(angle));
		fannedRightAhead.y = ahead.x * sin(DegsToRads(angle)) + ahead.y * cos(DegsToRads(angle));

		mLeftAhead2Distance = (mTopLeftCorner) + (fannedLeftAhead *  dynamicLength * side2Amount);
		mLeftAhead1Distance = (mTopLeftCorner) + (fannedLeftAhead *  dynamicLength * side1Amount);
		
		mRightAhead2Distance = (mTopRightCorner)+(fannedRightAhead  *  dynamicLength * side2Amount);
		mRightAhead1Distance = (mTopRightCorner)+(fannedRightAhead  *  dynamicLength * side1Amount);

		mAhead1Distance = GetCentrePosition() + (ahead * dynamicLength * ahead1Amount );
		mAhead2Distance = GetCentrePosition() + (ahead * dynamicLength * (ahead2Amount + 50));*/
		//cout << "AHEAD " << mLeftAhead2Distance.x << ":" << mLeftAhead2Distance.y << endl;
		//mAhead1Distance = mAhead2Distance * 0.5;
	}

	CreateFeelers(mRightSideFeelers, 90, 30, GetCentralPosition());
	CreateFeelers(mLeftSideFeelers, -90, 30, GetCentralPosition());
	CreateFeelers(mLeftFeelers, -40, 30, mTopLeftCorner);
	CreateFeelers(mRightFeelers, 40, 30, mTopRightCorner); // 70
	CreateFeelers(mStraightFeelers, 0, 40, mTopLeftCorner);
}
示例#3
0
void UI_ListView_Handler(void *pvParameters)
{
	ListView_Param_Struct* listView_Params = pvParameters;
	Key_Message_Struct key_Message;
	u16 pos[5];
	char tempString[29];
	int currentPos;
	int currentRelativePos;
	u16 itemStringLengths[4];
	u16 lastSelected = listView_Params->DefaultPos;
	u16 lengthTemp;
	u16 posTemp;
	u16 i, p, m;
	u8 displayLineNumInSinglePage = listView_Params->ListLength > 5 ? 5 : listView_Params->ListLength;
	//	u16* dataNumPointer;
	float * dataPointers[4];
	bool updateNumTab = true;
	bool firstDraw = true;
	bool currentPosChanged = false;
	xSemaphoreTake(OLEDRelatedMutex, portMAX_DELAY);
	OLED_Clear();
	OLED_DrawRect(0, 0, 127, 23 + 10 * (displayLineNumInSinglePage - 1), DRAW);
	OLED_DrawHorizonalLine(13, 0, 127, 1);
	xSemaphoreGive(OLEDRelatedMutex);
	SetKeyBeatRate(listView_Params->FastSpeed);
	/*Get the lengths of the itemNames*/
	for (i = 0; i < listView_Params->ItemNum; i++)
	{
		itemStringLengths[i] = GetStringGraphicalLength(listView_Params->ItemNames[i]);
	}
	pos[0] = listView_Params->ItemPositions[0];
	/*Make the positions of the itemStrings centural between two grids*/
	for (i = 0; i < listView_Params->ItemNum; i++)
	{
		pos[i] = GetCentralPosition(listView_Params->ItemPositions[i],
			listView_Params->ItemPositions[i + 1], itemStringLengths[i]) - 1;
	}
	for (i = 0; i < listView_Params->ItemNum; i++)
	{
		OLED_ShowAnyString(pos[i], 1, listView_Params->ItemNames[i], NotOnSelect, 12);
	}
	/*Draw vertical grids*/
	for (i = 0; i < listView_Params->ItemNum; i++)
	{
		OLED_DrawVerticalLine(listView_Params->ItemPositions[i] - 2, 0, 23 + 10 * (displayLineNumInSinglePage - 1), 1);
	}
	/*Draw horizonal grids*/
	for (i = 0; i < displayLineNumInSinglePage; i++)
	{
		OLED_DrawHorizonalLine(23 + 10 * i, 0, 127, 1);
	}
	if (listView_Params->Item1AutoNum == true)
	{
		for (i = 0; i < listView_Params->ItemNum - 1; i++)
		{
			dataPointers[i + 1] = listView_Params->DataPointers[i];
		}
		//		dataNumPointer = listView_Params->DataAutoNumPointer;
		//		for (i = 0; i < listView_Params->ListLength; i++)
		//		{
		//			dataNumPointer[i] = listView_Params->Item1AutoNumStart + i*listView_Params->Item1AutoNumStep;
		//		}
	}
	else
	{
		for (i = 0; i < listView_Params->ItemNum; i++)
		{
			dataPointers[i] = listView_Params->DataPointers[i];
		}
	}
	currentPos = listView_Params->DefaultPos;
	currentRelativePos = 0;
	ClearKeyEvent();
	for (;;)
	{
		xSemaphoreTake(OLEDRelatedMutex, portMAX_DELAY);
		if (listView_Params->Item1AutoNum == true) p = 1;
		else p = 0;
		for (i = 0; i < listView_Params->ItemNum - p; i++)
		{
			for (m = 0; m < displayLineNumInSinglePage; m++)
			{
				sprintf(tempString, listView_Params->sprintfCommandStrings[i], dataPointers[i + p][currentPos + m]);
				lengthTemp = GetStringGraphicalLength(tempString);
				posTemp = GetCentralPosition(listView_Params->ItemPositions[i + p],
					listView_Params->ItemPositions[i + p + 1], lengthTemp);
				OLED_ShowAnyString(posTemp - 1, 15 + 10 * m, tempString, NotOnSelect, 8);
			}
		}

		if (listView_Params->Item1AutoNum == true)
		{
			if (currentPosChanged == true)
			{
				currentPosChanged = false;
				for (i = 0; i < displayLineNumInSinglePage; i++)
					OLED_FillRect(listView_Params->ItemPositions[0], 15 + 10 * i,
						listView_Params->ItemPositions[1] - 4, 22 + 10 * i, 0);
				firstDraw = true;
				updateNumTab = true;
			}
			for (i = 0; i < displayLineNumInSinglePage; i++)
			{
				sprintf(tempString, "%d", listView_Params->Item1AutoNumStart + (currentPos + i)*listView_Params->Item1AutoNumStep);
				lengthTemp = GetStringGraphicalLength(tempString);
				posTemp = GetCentralPosition(listView_Params->ItemPositions[0],
					listView_Params->ItemPositions[1], lengthTemp);
				if (currentRelativePos != i&&lastSelected != i)
					OLED_ShowAnyString(posTemp - 1, 15 + 10 * i, tempString, NotOnSelect, 8);
				if (currentRelativePos == i&&updateNumTab == true)
				{
					if (firstDraw == false)
						OLED_InvertRect(listView_Params->ItemPositions[0], 15 + 10 * lastSelected,
							listView_Params->ItemPositions[1] - 4, 22 + 10 * lastSelected);
					else firstDraw = false;
					OLED_ShowAnyString(posTemp - 1, 15 + 10 * i, tempString, NotOnSelect, 8);
					OLED_InvertRect(listView_Params->ItemPositions[0], 15 + 10 * i,
						listView_Params->ItemPositions[1] - 4, 22 + 10 * i);
					lastSelected = currentRelativePos;
					updateNumTab = false;
				}
			}
		}
		OLED_Refresh_Gram();
		xSemaphoreGive(OLEDRelatedMutex);
		xQueueReceive(Key_Message, &key_Message, portMAX_DELAY);
		if (key_Message.KeyEvent == RightClick || key_Message.AdvancedKeyEvent == RightContinous)
		{
			currentRelativePos++;
			updateNumTab = true;
			if (currentRelativePos >= displayLineNumInSinglePage)
			{
				currentPosChanged = true;
				currentPos++;
				if (currentPos + displayLineNumInSinglePage > listView_Params->ListLength)
				{
					currentPos = listView_Params->ListLength - displayLineNumInSinglePage;
					currentPosChanged = false;
					updateNumTab = false;
				}
				currentRelativePos = displayLineNumInSinglePage - 1;
			}
		}
		else if (key_Message.KeyEvent == LeftClick || key_Message.AdvancedKeyEvent == LeftContinous)
		{
			currentRelativePos--;
			updateNumTab = true;
			if (currentRelativePos < 0)
			{
				currentPosChanged = true;
				currentPos--;
				if (currentPos < 0)
				{
					currentPos = 0;
					currentPosChanged = false;
					updateNumTab = false;
				}
				currentRelativePos = 0;
			}
		}
		else if (key_Message.KeyEvent == MidDouble)
		{
			currentPos = 32767;
			xQueueSend(UI_ListViewMsg, &currentPos, portMAX_DELAY);
			for(;;) vTaskDelay(portMAX_DELAY);
		}
		else if (key_Message.KeyEvent == MidClick)
		{
			xQueueSend(UI_ListViewMsg, &currentPos, portMAX_DELAY);
			for(;;) vTaskDelay(portMAX_DELAY);
		}
	}
}