llkeyframefallmotion.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * @file llkeyframefallmotion.cpp
  3. * @brief Implementation of LLKeyframeFallMotion 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 "llkeyframefallmotion.h"
  34. #include "llcharacter.h"
  35. #include "llmatrix3.h"
  36. LLKeyframeFallMotion::LLKeyframeFallMotion(const LLUUID& id)
  37. : LLKeyframeMotion(id),
  38. mCharacter(NULL),
  39. mVelocityZ(0.f)
  40. {
  41. }
  42. LLMotion::LLMotionInitStatus LLKeyframeFallMotion::onInitialize(LLCharacter* chrp)
  43. {
  44. // Save character pointer for later use
  45. mCharacter = chrp;
  46. // Load keyframe data, setup pose and joint states
  47. LLMotion::LLMotionInitStatus result = LLKeyframeMotion::onInitialize(chrp);
  48. if (result != LLMotion::STATUS_SUCCESS)
  49. {
  50. return result;
  51. }
  52. for (U32 jm = 0, count = mJointMotionList->getNumJointMotions();
  53. jm < count; ++jm)
  54. {
  55. LLJointState* jstate = mJointStates[jm];
  56. if (jstate)
  57. {
  58. LLJoint* joint = jstate->getJoint();
  59. if (joint && joint->getName() == "mPelvis")
  60. {
  61. mPelvisState = jstate;
  62. return result;
  63. }
  64. }
  65. }
  66. return result;
  67. }
  68. bool LLKeyframeFallMotion::onActivate()
  69. {
  70. mVelocityZ = -mCharacter->getCharacterVelocity().mV[VZ];
  71. LLVector3 ground_pos;
  72. LLVector3 ground_normal;
  73. mCharacter->getGround(mCharacter->getCharacterPosition(), ground_pos,
  74. ground_normal);
  75. ground_normal.normalize();
  76. LLQuaternion inverse_pelvis_rot = mCharacter->getCharacterRotation();
  77. inverse_pelvis_rot.transpose();
  78. // Find ground normal in pelvis space
  79. ground_normal = ground_normal * inverse_pelvis_rot;
  80. // Calculate new foward axis
  81. LLVector3 fwd_axis = LLVector3::x_axis -
  82. ground_normal * (ground_normal * LLVector3::x_axis);
  83. fwd_axis.normalize();
  84. mRotationToGroundNormal = LLQuaternion(fwd_axis, ground_normal % fwd_axis,
  85. ground_normal);
  86. return LLKeyframeMotion::onActivate();
  87. }
  88. bool LLKeyframeFallMotion::onUpdate(F32 time, U8* joint_mask)
  89. {
  90. bool result = LLKeyframeMotion::onUpdate(time, joint_mask);
  91. F32 slerp_amt = clamp_rescale(time / getDuration(), 0.5f, 0.75f, 0.f, 1.f);
  92. if (mPelvisState.notNull())
  93. {
  94. mPelvisState->setRotation(mPelvisState->getRotation() *
  95. slerp(slerp_amt, mRotationToGroundNormal,
  96. LLQuaternion()));
  97. }
  98. return result;
  99. }