home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 04 Pathfinding and Movement / 04 Pinter / Listing2.cpp < prev   
Encoding:
Text File  |  2001-12-09  |  1.5 KB  |  44 lines

  1. /* Copyright (C) Marco Pinter, 2001. 
  2.  * All rights reserved worldwide.
  3.  *
  4.  * This software is provided "as is" without express or implied
  5.  * warranties. You may freely copy and compile this source into
  6.  * applications you distribute provided that the copyright text
  7.  * below is included in the resulting source code, for example:
  8.  * "Portions Copyright (C) Marco Pinter, 2001"
  9.  */
  10.  
  11. // Determining position and orientation at a given time interval.
  12.  
  13. // CalcPosition: Determine current position based on
  14. //   pre-calculated path between waypoints.
  15. // Input Parameters:  speed, elapsed, radius, P, Q,
  16. //       curveLength, bTurnRight, angleStart, orientFinal
  17. // Output parameters: orientation, curPos
  18.  
  19. // Compute the total distance covered in the path so far
  20. dist = speed * elapsed;
  21. // If the agent is still in the curved portion...
  22. if (dist < curveLength)
  23. {
  24.     // Find the angle on the arc where the agent is
  25.     theta = angleStart 
  26.               + (bTurnRight ? -1.0 : 1.0) * dist / radius;
  27.  
  28.     // Determine the current position and orientation
  29.     curPos.x = P.x + radius * cos(theta);
  30.     curPos.y = P.y + radius * sin(theta);
  31.     orientation = bTurnRight ? theta - PI_VAL / 2 
  32.                                  : theta + PI_VAL / 2);
  33. }
  34. // If the agent is on the linear portion
  35. else
  36. {
  37.     // Find the distance we've moved along the straight line
  38.     distNow = dist - curveLength;
  39.     // Find the current position and orientation
  40.     curPos.x = Q.x + distNow * cos(orientFinal);
  41.     curPos.y = Q.y + distNow * sin(orientFinal);
  42.     orientation = orientFinal;
  43. }
  44.