PageRenderTime 72ms CodeModel.GetById 14ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/llcharacter/lltargetingmotion.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 171 lines | 80 code | 31 blank | 60 comment | 2 complexity | a529b038250c1e936c8c07d557e78346 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lltargetingmotion.cpp
  3. * @brief Implementation of LLTargetingMotion class.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. //-----------------------------------------------------------------------------
  27. // Header Files
  28. //-----------------------------------------------------------------------------
  29. #include "linden_common.h"
  30. #include "lltargetingmotion.h"
  31. #include "llcharacter.h"
  32. #include "v3dmath.h"
  33. #include "llcriticaldamp.h"
  34. //-----------------------------------------------------------------------------
  35. // Constants
  36. //-----------------------------------------------------------------------------
  37. const F32 TORSO_TARGET_HALF_LIFE = 0.25f;
  38. const F32 MAX_TIME_DELTA = 2.f; //max two seconds a frame for calculating interpolation
  39. const F32 TARGET_PLANE_THRESHOLD_DOT = 0.6f;
  40. const F32 TORSO_ROT_FRACTION = 0.5f;
  41. //-----------------------------------------------------------------------------
  42. // LLTargetingMotion()
  43. // Class Constructor
  44. //-----------------------------------------------------------------------------
  45. LLTargetingMotion::LLTargetingMotion(const LLUUID &id) : LLMotion(id)
  46. {
  47. mCharacter = NULL;
  48. mName = "targeting";
  49. mTorsoState = new LLJointState;
  50. }
  51. //-----------------------------------------------------------------------------
  52. // ~LLTargetingMotion()
  53. // Class Destructor
  54. //-----------------------------------------------------------------------------
  55. LLTargetingMotion::~LLTargetingMotion()
  56. {
  57. }
  58. //-----------------------------------------------------------------------------
  59. // LLTargetingMotion::onInitialize(LLCharacter *character)
  60. //-----------------------------------------------------------------------------
  61. LLMotion::LLMotionInitStatus LLTargetingMotion::onInitialize(LLCharacter *character)
  62. {
  63. // save character for future use
  64. mCharacter = character;
  65. mPelvisJoint = mCharacter->getJoint("mPelvis");
  66. mTorsoJoint = mCharacter->getJoint("mTorso");
  67. mRightHandJoint = mCharacter->getJoint("mWristRight");
  68. // make sure character skeleton is copacetic
  69. if (!mPelvisJoint ||
  70. !mTorsoJoint ||
  71. !mRightHandJoint)
  72. {
  73. llwarns << "Invalid skeleton for targeting motion!" << llendl;
  74. return STATUS_FAILURE;
  75. }
  76. mTorsoState->setJoint( mTorsoJoint );
  77. // add joint states to the pose
  78. mTorsoState->setUsage(LLJointState::ROT);
  79. addJointState( mTorsoState );
  80. return STATUS_SUCCESS;
  81. }
  82. //-----------------------------------------------------------------------------
  83. // LLTargetingMotion::onActivate()
  84. //-----------------------------------------------------------------------------
  85. BOOL LLTargetingMotion::onActivate()
  86. {
  87. return TRUE;
  88. }
  89. //-----------------------------------------------------------------------------
  90. // LLTargetingMotion::onUpdate()
  91. //-----------------------------------------------------------------------------
  92. BOOL LLTargetingMotion::onUpdate(F32 time, U8* joint_mask)
  93. {
  94. F32 slerp_amt = LLCriticalDamp::getInterpolant(TORSO_TARGET_HALF_LIFE);
  95. LLVector3 target;
  96. LLVector3* lookAtPoint = (LLVector3*)mCharacter->getAnimationData("LookAtPoint");
  97. BOOL result = TRUE;
  98. if (!lookAtPoint)
  99. {
  100. return TRUE;
  101. }
  102. else
  103. {
  104. target = *lookAtPoint;
  105. target.normVec();
  106. }
  107. //LLVector3 target_plane_normal = LLVector3(1.f, 0.f, 0.f) * mPelvisJoint->getWorldRotation();
  108. //LLVector3 torso_dir = LLVector3(1.f, 0.f, 0.f) * (mTorsoJoint->getWorldRotation() * mTorsoState->getRotation());
  109. LLVector3 skyward(0.f, 0.f, 1.f);
  110. LLVector3 left(skyward % target);
  111. left.normVec();
  112. LLVector3 up(target % left);
  113. up.normVec();
  114. LLQuaternion target_aim_rot(target, left, up);
  115. LLQuaternion cur_torso_rot = mTorsoJoint->getWorldRotation();
  116. LLVector3 right_hand_at = LLVector3(0.f, -1.f, 0.f) * mRightHandJoint->getWorldRotation();
  117. left.setVec(skyward % right_hand_at);
  118. left.normVec();
  119. up.setVec(right_hand_at % left);
  120. up.normVec();
  121. LLQuaternion right_hand_rot(right_hand_at, left, up);
  122. LLQuaternion new_torso_rot = (cur_torso_rot * ~right_hand_rot) * target_aim_rot;
  123. // find ideal additive rotation to make torso point in correct direction
  124. new_torso_rot = new_torso_rot * ~cur_torso_rot;
  125. // slerp from current additive rotation to ideal additive rotation
  126. new_torso_rot = nlerp(slerp_amt, mTorsoState->getRotation(), new_torso_rot);
  127. // constraint overall torso rotation
  128. LLQuaternion total_rot = new_torso_rot * mTorsoJoint->getRotation();
  129. total_rot.constrain(F_PI_BY_TWO * 0.8f);
  130. new_torso_rot = total_rot * ~mTorsoJoint->getRotation();
  131. mTorsoState->setRotation(new_torso_rot);
  132. return result;
  133. }
  134. //-----------------------------------------------------------------------------
  135. // LLTargetingMotion::onDeactivate()
  136. //-----------------------------------------------------------------------------
  137. void LLTargetingMotion::onDeactivate()
  138. {
  139. }
  140. // End