BVB Source Codes

CRYENGINE Show MovementBlock_TurnTowardsPosition.cpp Source code

Return Download CRYENGINE: download MovementBlock_TurnTowardsPosition.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #include "StdAfx.h"
  4. #include "MovementBlock_TurnTowardsPosition.h"
  5. #include <CryAISystem/MovementUpdateContext.h>
  6. #include "MovementActor.h"
  7. #include "PipeUser.h"
  8.  
  9. namespace Movement
  10. {
  11. namespace MovementBlocks
  12. {
  13. TurnTowardsPosition::TurnTowardsPosition(const Vec3& position)
  14.         : m_positionToTurnTowards(position)
  15.         , m_timeSpentAligning(0.0f)
  16.         , m_correctBodyDirTime(0.0f)
  17. {
  18. }
  19.  
  20. Movement::Block::Status TurnTowardsPosition::Update(const MovementUpdateContext& context)
  21. {
  22.         // Align body towards the move target
  23.         const Vec3 actorPysicalPosition = context.actor.GetAdapter().GetPhysicsPosition();
  24.         Vec3 dirToMoveTarget = m_positionToTurnTowards - actorPysicalPosition;
  25.         dirToMoveTarget.z = 0.0f;
  26.         dirToMoveTarget.Normalize();
  27.         context.actor.GetAdapter().SetBodyTargetDirection(dirToMoveTarget);
  28.  
  29.         // If animation body direction is within the angle threshold,
  30.         // wait for some time and then mark the agent as ready to move
  31.         const Vec3 actualBodyDir = context.actor.GetAdapter().GetAnimationBodyDirection();
  32.         const bool lookingTowardsMoveTarget = (actualBodyDir.Dot(dirToMoveTarget) > cosf(DEG2RAD(17.0f)));
  33.         if (lookingTowardsMoveTarget)
  34.                 m_correctBodyDirTime += context.updateTime;
  35.         else
  36.                 m_correctBodyDirTime = 0.0f;
  37.  
  38.         const float timeSpentAligning = m_timeSpentAligning + context.updateTime;
  39.         m_timeSpentAligning = timeSpentAligning;
  40.  
  41.         if (m_correctBodyDirTime > 0.2f)
  42.                 return Movement::Block::Finished;
  43.  
  44.         const float timeout = 8.0f;
  45.         if (timeSpentAligning > timeout)
  46.         {
  47.                 gEnv->pLog->LogWarning("Agent '%s' at %f %f %f failed to turn towards %f %f %f within %f seconds. Proceeding anyway.",
  48.                                        context.actor.GetName(), actorPysicalPosition.x, actorPysicalPosition.y, actorPysicalPosition.z,
  49.                                        m_positionToTurnTowards.x, m_positionToTurnTowards.y, m_positionToTurnTowards.z, timeout);
  50.                 return Movement::Block::Finished;
  51.         }
  52.  
  53.         return Movement::Block::Running;
  54. }
  55.  
  56. void TurnTowardsPosition::End(IMovementActor& actor)
  57. {
  58.         actor.GetAdapter().ResetBodyTarget();
  59. }
  60. }
  61. }
  62.  
downloadMovementBlock_TurnTowardsPosition.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top