⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arsplfield.cpp

📁 多机器人目标围捕 用MFC自行设计了简单的机器人围捕场景。机器人策略采用注册机制易于机器人的策略的扩展。捕捉机器人策略应用了人工势场法。
💻 CPP
字号:
#include "StdAfx.h"
#include "ARSPlField.h"
#include <math.h>


int ARSPlField::ms_rand = 0;

ARSPlField::ARSPlField()
{
	dKExclude = 1;
	dKAttract = 10;
	 ms_rand = 0;
}

ARSAction* ARSPlField::CreateAction()
{
	ARSPlField * tmp = new ARSPlField;

	return tmp;
}

bool ARSPlField::ExecuteAction(const ARSSTUATION & stSituation, MOVEDIRECTION & nextDirection)
{
	int iPoliceID = stSituation.iID-1;
	ARSPOINT ptPoliceSelf = stSituation.ptPloice[iPoliceID];
	ARSPOINT ptThief = stSituation.ptThief;

	

	double dSumX = 0,dSumY = 0,dField,dR;
	dR = sqrt((ptThief.x - ptPoliceSelf.x)*(ptThief.x - ptPoliceSelf.x)+(ptThief.y - ptPoliceSelf.y)*(ptThief.y - ptPoliceSelf.y));
	dField = dKAttract/dR;

	dSumX = dField*(ptThief.x - ptPoliceSelf.x)/dR;
	dSumY = dField*(ptThief.y - ptPoliceSelf.y)/dR;
	
	for (int i = 0;i < stSituation.ptPloice.size();i++)
	{
		if (i==iPoliceID)
			continue;
		dR = sqrt((ptPoliceSelf.x - stSituation.ptPloice[i].x)*(ptPoliceSelf.x - stSituation.ptPloice[i].x)
			+(ptPoliceSelf.y - stSituation.ptPloice[i].y)*(ptPoliceSelf.y - stSituation.ptPloice[i].y));
		dField = dKExclude/dR;
		
		dSumX += dField*(ptPoliceSelf.x - stSituation.ptPloice[i].x)/dR;
		dSumY += dField*(ptPoliceSelf.y - stSituation.ptPloice[i].y)/dR;
		
	}

	double dx,dy;
	dx = fabs(dSumX);
	dy = fabs(dSumY);
	int d;

	
	if (dx >=dy)
	{
		if (dSumX>0)
		{
			nextDirection = RIGHT;
			d = 1;
		}
		else
		{
			nextDirection = LEFT;
			d = -1;
		}
		for (i = 0;i < stSituation.ptPloice.size();i++)
		{
			if (i==iPoliceID)
				continue;
			if (ptPoliceSelf.x+d == stSituation.ptPloice[i].x&&ptPoliceSelf.y == stSituation.ptPloice[i].y
				||ptPoliceSelf.x+d == ptThief.x&&ptPoliceSelf.y == ptThief.y)
			{
				if (dSumY<0.0005)
				{
					ms_rand+=2;
					if (ms_rand%2)
					{
						nextDirection = STILL;
						break;
					}
					
				}
				if (dSumY>0)		
					nextDirection = UP;					
				else		
					nextDirection = DOWN;
				break;
			}
		}
		
	}
	else
	{
		if (dSumY>0)
		{
			nextDirection = UP;
			d = 1;
		}
		else
		{
			nextDirection = DOWN;
			d = -1;
		}
		for (i = 0;i < stSituation.ptPloice.size();i++)
		{
			if (i==iPoliceID)
				continue;
			if (ptPoliceSelf.x == stSituation.ptPloice[i].x&&ptPoliceSelf.y+d == stSituation.ptPloice[i].y
				||ptPoliceSelf.x == ptThief.x&&ptPoliceSelf.y+d == ptThief.y)
			{
				if (dSumX<0.0005)
				{
			    	if ((ms_rand++)%2)
					{
						nextDirection = STILL;
						break;
					}
				}
				if (dSumX>0)		
					nextDirection = RIGHT;					
				else		
					nextDirection = LEFT;
				break;
			}
		}
	}

	



	return true;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -