objectwithinradiuscondition.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. ** Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  3. **
  4. ** File: objectwithinradiuscondition.cpp
  5. **
  6. ** Author:
  7. **
  8. ** Description:
  9. ** Implementation of the training library "objectwithinradiuscondition" interface.
  10. **
  11. ** History:
  12. */
  13. #include "pch.h"
  14. #include "ObjectWithinRadiusCondition.h"
  15. #include "TypeIDTarget.h"
  16. namespace Training
  17. {
  18. //------------------------------------------------------------------------------
  19. // class methods
  20. //------------------------------------------------------------------------------
  21. /* void */ ObjectWithinRadiusCondition::ObjectWithinRadiusCondition (ImodelIGC* pObject, ImodelIGC* pTarget, float fRadius) :
  22. m_pObject (new TypeIDTarget (pObject->GetObjectType (), pObject->GetObjectID ())),
  23. m_pTarget (new TypeIDTarget (pTarget->GetObjectType (), pTarget->GetObjectID ())),
  24. m_fRadiusSquared (fRadius),
  25. m_bConditionInitialized (false)
  26. {
  27. }
  28. //------------------------------------------------------------------------------
  29. /* void */ ObjectWithinRadiusCondition::ObjectWithinRadiusCondition (ImodelIGC* pObject, ObjectType targetType, ObjectID targetID, float fRadius) :
  30. m_pObject (new TypeIDTarget (pObject->GetObjectType (), pObject->GetObjectID ())),
  31. m_pTarget (new TypeIDTarget (targetType, targetID)),
  32. m_fRadiusSquared (fRadius),
  33. m_bConditionInitialized (false)
  34. {
  35. }
  36. //------------------------------------------------------------------------------
  37. /* void */ ObjectWithinRadiusCondition::ObjectWithinRadiusCondition (ImodelIGC* pObject, AbstractTarget* pTarget, float fRadius) :
  38. m_pObject (new TypeIDTarget (pObject->GetObjectType (), pObject->GetObjectID ())),
  39. m_pTarget (pTarget),
  40. m_fRadiusSquared (fRadius),
  41. m_bConditionInitialized (false)
  42. {
  43. }
  44. //------------------------------------------------------------------------------
  45. /* void */ ObjectWithinRadiusCondition::ObjectWithinRadiusCondition (ObjectType objectType, ObjectID objectID, ObjectType targetType, ObjectID targetID, float fRadius) :
  46. m_pObject (new TypeIDTarget (objectType, objectID)),
  47. m_pTarget (new TypeIDTarget (targetType, targetID)),
  48. m_fRadiusSquared (fRadius),
  49. m_bConditionInitialized (false)
  50. {
  51. }
  52. //------------------------------------------------------------------------------
  53. /* void */ ObjectWithinRadiusCondition::~ObjectWithinRadiusCondition (void)
  54. {
  55. delete m_pObject;
  56. delete m_pTarget;
  57. }
  58. //------------------------------------------------------------------------------
  59. bool ObjectWithinRadiusCondition::Evaluate (void)
  60. {
  61. if (Initialized ())
  62. {
  63. // check that both objects in this condition still exist. This is mostly
  64. // to avoid unwanted crashes, not to provide any kind of desired behavior.
  65. // Note that if the objects don't exist, a constraint should have caught
  66. // the situation and appropriately handled it, so this shouldn't fail.
  67. assert (*m_pObject and *m_pTarget);
  68. if (*m_pObject and *m_pTarget)
  69. {
  70. // Check to see if the objects are closer than the specified distance,
  71. // after accounting for the radii of the two objects. We use the squared
  72. // lengths to avoid the unnecessary square root operation.
  73. Vector delta = (*m_pObject)->GetPosition () - (*m_pTarget)->GetPosition ();
  74. float fLengthSquared = delta.LengthSquared ();
  75. return (fLengthSquared > m_fRadiusSquared) ? false : true;
  76. }
  77. }
  78. return false;
  79. }
  80. //------------------------------------------------------------------------------
  81. float ObjectWithinRadiusCondition::GetRadarRadius (void) const
  82. {
  83. // make sure the condition is initialized (if possible)
  84. const_cast<ObjectWithinRadiusCondition*> (this) ->Initialized ();
  85. // This returns the number that the radar will show at the moment the
  86. // condition becomes true. If the condition is not initialized, we assume
  87. // a target radius of zero
  88. return m_bConditionInitialized ? sqrtf (m_fRadiusSquared) : (*m_pObject)->GetRadius () + m_fRadiusSquared;
  89. }
  90. //------------------------------------------------------------------------------
  91. bool ObjectWithinRadiusCondition::Initialized (void)
  92. {
  93. if (not m_bConditionInitialized)
  94. {
  95. // fetch the target object, if we can
  96. if (*m_pTarget)
  97. {
  98. // we want the radius to represent distance from outer edge to outer edge,
  99. // not an absolute distance value.
  100. m_fRadiusSquared += (*m_pObject)->GetRadius () + (*m_pTarget)->GetRadius ();
  101. m_fRadiusSquared *= m_fRadiusSquared;
  102. m_bConditionInitialized = true;
  103. }
  104. }
  105. return m_bConditionInitialized;
  106. }
  107. //------------------------------------------------------------------------------
  108. }