objectmovingtowardscondition.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. ** Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  3. **
  4. ** File: objectmovingtowardscondition.cpp
  5. **
  6. ** Author:
  7. **
  8. ** Description:
  9. ** Implementation of the training library "objectmovingtowardscondition" interface.
  10. **
  11. ** History:
  12. */
  13. #include "pch.h"
  14. #include "ObjectMovingTowardsCondition.h"
  15. namespace Training
  16. {
  17. //------------------------------------------------------------------------------
  18. // class methods
  19. //------------------------------------------------------------------------------
  20. /* void */ ObjectMovingTowardsCondition::ObjectMovingTowardsCondition (const TRef<ImodelIGC>& object, const TRef<ImodelIGC>& target) :
  21. m_object (object),
  22. m_target (target)
  23. {
  24. }
  25. //------------------------------------------------------------------------------
  26. /* void */ ObjectMovingTowardsCondition::~ObjectMovingTowardsCondition (void)
  27. {
  28. m_object = 0;
  29. m_target = 0;
  30. }
  31. //------------------------------------------------------------------------------
  32. bool ObjectMovingTowardsCondition::Evaluate (void)
  33. {
  34. // check that both objects in this condition still exist. This is mostly
  35. // to avoid unwanted crashes, not to provide any kind of desired behavior.
  36. // Note that if the objects don't exist, a constraint should have caught
  37. // the situation and appropriately handled it, so this shouldn't fail.
  38. assert (m_object->GetMission () and m_target->GetMission ());
  39. if (m_object->GetMission () and m_target->GetMission ())
  40. {
  41. // The algorithm here is to get the vector of the motion and the vector towards
  42. // the target (the look vector). If the object is moving towards the target,
  43. // then the angle from its velocity vector to the look vector is less than
  44. // the angle subtended by the target's radius around the look vector.
  45. Vector forward = m_object->GetVelocity ();
  46. float fSpeed = forward.Length ();
  47. if (fSpeed > 0.01f)
  48. {
  49. Vector lookAtTarget = m_target->GetPosition () - m_object->GetPosition ();
  50. float fDistanceToTarget = lookAtTarget.Length ();
  51. lookAtTarget /= fDistanceToTarget;
  52. forward /= fSpeed;
  53. float fCosLookAngle = forward * lookAtTarget; // dot product
  54. if (fCosLookAngle > 0.0f)
  55. {
  56. float fLookAngle = acosf (fCosLookAngle);
  57. float fCoverageRadius = m_target->GetRadius () * 0.75f;
  58. float fCoverageAngle = asinf (fCoverageRadius / fDistanceToTarget);
  59. return (fLookAngle < fCoverageAngle) ? true : false;
  60. }
  61. }
  62. }
  63. return false;
  64. }
  65. //------------------------------------------------------------------------------
  66. }