lltargetingmotion.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @file lltargetingmotion.cpp
  3. * @brief Implementation of LLTargetingMotion class.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "lltargetingmotion.h"
  34. #include "llcharacter.h"
  35. #include "llcriticaldamp.h"
  36. #include "llvector3d.h"
  37. constexpr F32 TORSO_TARGET_HALF_LIFE = 0.25f;
  38. LLTargetingMotion::LLTargetingMotion(const LLUUID& id)
  39. : LLMotion(id),
  40. mCharacter(NULL)
  41. {
  42. mName = "targeting";
  43. mTorsoState = new LLJointState;
  44. }
  45. LLMotion::LLMotionInitStatus LLTargetingMotion::onInitialize(LLCharacter* character)
  46. {
  47. // save character for future use
  48. mCharacter = character;
  49. mPelvisJoint = mCharacter->getJoint(LL_JOINT_KEY_PELVIS);
  50. mTorsoJoint = mCharacter->getJoint(LL_JOINT_KEY_TORSO);
  51. mRightHandJoint = mCharacter->getJoint(LL_JOINT_KEY_WRISTRIGHT);
  52. // make sure character skeleton is copacetic
  53. if (!mPelvisJoint || !mTorsoJoint || !mRightHandJoint)
  54. {
  55. llwarns << "Invalid skeleton for targeting motion!" << llendl;
  56. return STATUS_FAILURE;
  57. }
  58. mTorsoState->setJoint(mTorsoJoint);
  59. // add joint states to the pose
  60. mTorsoState->setUsage(LLJointState::ROT);
  61. addJointState(mTorsoState);
  62. return STATUS_SUCCESS;
  63. }
  64. bool LLTargetingMotion::onUpdate(F32, U8* joint_mask)
  65. {
  66. LLVector3* lookat_pt =
  67. (LLVector3*)mCharacter->getAnimationData("LookAtPoint");
  68. if (!lookat_pt)
  69. {
  70. return true;
  71. }
  72. F32 slerp_amt = LLCriticalDamp::getInterpolant(TORSO_TARGET_HALF_LIFE);
  73. LLVector3 target = *lookat_pt;
  74. target.normalize();
  75. #if 0
  76. LLVector3 target_plane_normal = LLVector3(1.f, 0.f, 0.f) *
  77. mPelvisJoint->getWorldRotation();
  78. LLVector3 torso_dir = LLVector3(1.f, 0.f, 0.f) *
  79. (mTorsoJoint->getWorldRotation() *
  80. mTorsoState->getRotation());
  81. #endif
  82. LLVector3 skyward(0.f, 0.f, 1.f);
  83. LLVector3 left(skyward % target);
  84. left.normalize();
  85. LLVector3 up(target % left);
  86. up.normalize();
  87. LLQuaternion target_aim_rot(target, left, up);
  88. LLQuaternion cur_torso_rot = mTorsoJoint->getWorldRotation();
  89. LLVector3 right_hand_at = LLVector3(0.f, -1.f, 0.f) *
  90. mRightHandJoint->getWorldRotation();
  91. left.set(skyward % right_hand_at);
  92. left.normalize();
  93. up.set(right_hand_at % left);
  94. up.normalize();
  95. LLQuaternion right_hand_rot(right_hand_at, left, up);
  96. LLQuaternion new_torso_rot = (cur_torso_rot * ~right_hand_rot) * target_aim_rot;
  97. // find ideal additive rotation to make torso point in correct direction
  98. new_torso_rot = new_torso_rot * ~cur_torso_rot;
  99. // slerp from current additive rotation to ideal additive rotation
  100. new_torso_rot = nlerp(slerp_amt, mTorsoState->getRotation(), new_torso_rot);
  101. // constraint overall torso rotation
  102. LLQuaternion total_rot = new_torso_rot * mTorsoJoint->getRotation();
  103. total_rot.constrain(F_PI_BY_TWO * 0.8f);
  104. new_torso_rot = total_rot * ~mTorsoJoint->getRotation();
  105. mTorsoState->setRotation(new_torso_rot);
  106. return true;
  107. }