listing2.cpp

来自「C人工智能游戏开发的一些实例源代码 C Game development in 」· C++ 代码 · 共 44 行

CPP
44
字号
/* Copyright (C) Marco Pinter, 2001. 
 * All rights reserved worldwide.
 *
 * This software is provided "as is" without express or implied
 * warranties. You may freely copy and compile this source into
 * applications you distribute provided that the copyright text
 * below is included in the resulting source code, for example:
 * "Portions Copyright (C) Marco Pinter, 2001"
 */

// Determining position and orientation at a given time interval.

// CalcPosition: Determine current position based on
//   pre-calculated path between waypoints.
// Input Parameters:  speed, elapsed, radius, P, Q,
//       curveLength, bTurnRight, angleStart, orientFinal
// Output parameters: orientation, curPos

// Compute the total distance covered in the path so far
dist = speed * elapsed;
// If the agent is still in the curved portion...
if (dist < curveLength)
{
	// Find the angle on the arc where the agent is
	theta = angleStart 
              + (bTurnRight ? -1.0 : 1.0) * dist / radius;

	// Determine the current position and orientation
	curPos.x = P.x + radius * cos(theta);
	curPos.y = P.y + radius * sin(theta);
	orientation = bTurnRight ? theta - PI_VAL / 2 
                                 : theta + PI_VAL / 2);
}
// If the agent is on the linear portion
else
{
	// Find the distance we've moved along the straight line
	distNow = dist - curveLength;
	// Find the current position and orientation
	curPos.x = Q.x + distNow * cos(orientFinal);
	curPos.y = Q.y + distNow * sin(orientFinal);
	orientation = orientFinal;
}

⌨️ 快捷键说明

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