本文整理汇总了C++中Vector2::Clamp方法的典型用法代码示例。如果您正苦于以下问题:C++ Vector2::Clamp方法的具体用法?C++ Vector2::Clamp怎么用?C++ Vector2::Clamp使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Vector2
的用法示例。
在下文中一共展示了Vector2::Clamp方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: IdentifyTarget
/*!
* Identify the target state that we wish the robot to be in. This will be the target which the A* algorithm plots towards.
*/
RobotState Eagle::IdentifyTarget(RobotState &ourRobotState, RobotState &enemyRobotState, Vector2 ballPos, bool doWeHaveBall, bool &isMovingToBall)
{
RobotState targetState;
Vector2 ourRobotPos = ourRobotState.Position();
Vector2 enemyRobotPos = enemyRobotState.Position();
Vector2 ourGoalCentre = GoalCentrePosition(m_pitchSide);
Vector2 enemyGoalCentre;
if (m_pitchSide == eLeftSide)
{
enemyGoalCentre = GoalCentrePosition(eRightSide);
}
else
{
enemyGoalCentre = GoalCentrePosition(eLeftSide);
}
ballPos.Clamp(Vector2(0,0), Vector2(m_pitchSizeX-1, m_pitchSizeY-1));
isMovingToBall = false;
// doWeHaveBall is a value which comes from the robot's rotational sensors.
//ourRobotState.SetHasBall(doWeHaveBall);
ourRobotState.SetHasBall(DoesRobotHaveBall(ourRobotState, ballPos));
enemyRobotState.SetHasBall(DoesRobotHaveBall(enemyRobotState, ballPos));
if (m_state == ePenaltyAttack)
{
m_isKickingPosSet = false;
// When taking a penalty, we want to find a free position to kick to.
// Position should stay the same - we only want to re-orientate.
targetState.SetPosition(ourRobotPos);
// We'll do this by checking for intersections between us and three positions on the goal line.
Vector2 targetPositions[3];
targetPositions[0] = enemyGoalCentre;
targetPositions[1] = enemyGoalCentre - Vector2(0,50);
targetPositions[2] = enemyGoalCentre + Vector2(0,50);
int arrayLength = sizeof(targetPositions)/sizeof(Vector2);
Vector2 optimalShootingTarget;
float bestDistanceFromEnemy = 0;
// Iterate through the positions, finding the best one, based on if it's unblocked and how far it is from the enemy robot.
for (int i=0; i < arrayLength; i++)
{
// Check if the target is unblocked.
bool isBlocked = m_intersection.LineCircleIntersection(ourRobotPos, targetPositions[i], enemyRobotPos, ROBOT_RADIUS);
if (isBlocked)
{
continue;
}
float distanceSqdToEnemy = enemyRobotPos.DistanceSquared(&targetPositions[i]);
// Check if this beats our previous best distance.
if (distanceSqdToEnemy > bestDistanceFromEnemy)
{
bestDistanceFromEnemy = distanceSqdToEnemy;
optimalShootingTarget = targetPositions[i];
}
// Check that we do actually have a target set.
if (optimalShootingTarget.IsSet())
{
float angleToTarget = ourRobotPos.GetAngleTo(&optimalShootingTarget);
targetState.SetOrientation(angleToTarget);
}
else
{
targetState.SetOrientation(ourRobotState.Orientation());
}
}
}
else if (m_state == ePenaltyDefend)
{
m_isKickingPosSet = false;
// When defending, we're permitted to move up and down the goalline.
// Orientation should stay the same.
targetState.SetOrientation(ourRobotState.Orientation());
// X-axis position should be the same, y-axis should be a position extrapolated in the direction of the enemy robot.
//float extrapolationGradient = tan(enemyRobotState.Orientation());
// I'm experimenting with extrapolating on a line between the robot and ball instead.
float extrapolationGradient = enemyRobotPos.Gradient(&ballPos);
int extrapolatedY = enemyRobotPos.Y() + ((ourRobotPos.X() - enemyRobotPos.X()) * extrapolationGradient);
//.........这里部分代码省略.........