ball.cpp

来自「一个C编写的足球机器人比赛程序」· C++ 代码 · 共 69 行

CPP
69
字号
#include "Ball.h"


Ball::Ball()
{
	objectType = OT_Ball;
}


BallStateT Ball::doMove(VecPosition *pos,VecPosition *vel,VecPosition *acc)
{
	Rect field(VecPosition(0,0),VecPosition(CFLDLEN,CFLDWIDTH));
	Line leftDoor(0,1,0);
	Line rightDoor(0,1,-CFLDLEN);
	VecPosition oldPos = *pos;	
	if(acc->getMagnitude() > CMAXBALLACC)
	{
		acc->setMagnitude(CMAXBALLACC);
	}
	*vel += *acc;
	if(vel->getMagnitude() > CMAXBALLV)
	{
		vel->setMagnitude(CMAXBALLV);
	}	
	*acc =  VecPosition(0,0);
	*pos += *vel;
	if(!field.isInside(*pos))
	{
		Line ballLine = Line::makeLineFromTwoPoints(oldPos,*pos);		
		if(pos->getX() < 0)
		{
			VecPosition posCross = ballLine.getIntersection(leftDoor);
			if(posCross.getY() > CDOORY1 && posCross.getY() < CDOORY2)
			{				
				return BS_LEFT_Goal;
			}
			else
			{				
				vel->setX(- vel->getX());
				pos->setX(- pos->getX());
			}
		}
		else if(pos->getX() > CFLDLEN)
		{
			VecPosition posCross = ballLine.getIntersection(rightDoor);
			if(posCross.getY() > CDOORY1 && posCross.getY() < CDOORY2)
			{				
				return BS_RIGHT_Goal;
			}
			else
			{				
				vel->setX(- vel->getX());
				pos->setX(2.0 * CFLDLEN - pos->getX());
			}
		}
		if( pos->getY() < 0)
		{
			vel->setY(- vel->getY());
			pos->setY(- pos->getY());
		}
		else if(pos->getY() > CFLDWIDTH)
		{
			vel->setY(- vel->getY());
			pos->setY(2 * CFLDWIDTH - pos->getY());
		}
	}
	*vel *= CBALLDECAY;
	return BS_Normal;
}

⌨️ 快捷键说明

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