Guest User

Untitled

a guest
Jan 21st, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. void Obj::MoveToLocation(D3DXVECTOR3 newLocation, float deltaTime)
  2. {
  3. D3DXVECTOR3 directionToTarget = newLocation - location;
  4. D3DXVec3Normalize(&directionToTarget, &directionToTarget);
  5.  
  6. location += maxSpeed * directionToTarget * deltaTime;
  7. }
  8.  
  9. void Obj::Patrol(std::vector<D3DXVECTOR3> locations, float deltaTime)
  10. {
  11. hasArrived = false;
  12.  
  13. for (int i = 0; i < locations.size(); ++i)
  14. {
  15. if (!hasArrived)
  16. MoveToLocation(locations[i], deltaTime);
  17.  
  18. if ((location.x <= locations[i].x + radius.x) && (location.x >= locations[i].x - radius.x) &&
  19. (location.z <= locations[i].z + radius.z) && (location.z >= locations[i].z - radius.z))
  20. {
  21. hasArrived = true;
  22. }
  23. }
  24. }
  25.  
  26. void Obj::MoveToLocation(D3DXVECTOR3 newLocation, float deltaTime)
  27. {
  28. D3DXVECTOR3 directionToTarget = newLocation - location;
  29.  
  30. if (D3DXVec3Length(&directionToTarget) <= maxSpeed * deltaTime)
  31. {
  32. // If this step would take us past the destination, just stop there
  33. // instead.
  34. location = newLocation;
  35. }
  36. else
  37. {
  38. D3DXVec3Normalize(&directionToTarget, &directionToTarget);
  39.  
  40. location += maxSpeed * directionToTarget * deltaTime;
  41. }
  42. }
  43.  
  44. // Call this once to set the patrol route
  45. void Obj::Patrol(std::vector<D3DXVECTOR3> locations)
  46. {
  47. patrolRoute = locations;
  48. patrolIndex = 0; // Maybe pick the closest point instead?
  49. }
  50.  
  51. // Call this each time the object should move (once per frame/turn)
  52. void Obj::UpdatePatrol(float deltaTime)
  53. {
  54. if (patrolRoute == NULL || patrolRoute.empty())
  55. {
  56. return;
  57. }
  58.  
  59. if (patrolIndex >= patrolRoute.size())
  60. {
  61. // Start again from the beginning
  62. patrolIndex -= patrolRoute.size();
  63. }
  64.  
  65. // Move towards the next location
  66. D3DXVECTOR3 nextLocation = patrolRoute[patrolIndex];
  67. MoveToLocation(nextLocation, deltaTime);
  68.  
  69. float dx = location.x - nextLocation.x;
  70. float dz = location.z - nextLocation.z;
  71.  
  72. if ((dx <= radius.x) && (dx >= -radius.x) &&
  73. (dz <= radius.z) && (dz >= -radius.z))
  74. {
  75. // We have reached it. Select the next destionation.
  76. patrolIndex++;
  77. }
  78. }
Add Comment
Please, Sign In to add comment