PageRenderTime 54ms CodeModel.GetById 35ms app.highlight 15ms RepoModel.GetById 1ms 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
  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//-----------------------------------------------------------------------------
 28// Header Files
 29//-----------------------------------------------------------------------------
 30#include "linden_common.h"
 31
 32#include "lltargetingmotion.h"
 33#include "llcharacter.h"
 34#include "v3dmath.h"
 35#include "llcriticaldamp.h"
 36
 37//-----------------------------------------------------------------------------
 38// Constants
 39//-----------------------------------------------------------------------------
 40const F32 TORSO_TARGET_HALF_LIFE = 0.25f;
 41const F32 MAX_TIME_DELTA = 2.f; //max two seconds a frame for calculating interpolation
 42const F32 TARGET_PLANE_THRESHOLD_DOT = 0.6f;
 43const F32 TORSO_ROT_FRACTION = 0.5f;
 44
 45//-----------------------------------------------------------------------------
 46// LLTargetingMotion()
 47// Class Constructor
 48//-----------------------------------------------------------------------------
 49LLTargetingMotion::LLTargetingMotion(const LLUUID &id) : LLMotion(id)
 50{
 51	mCharacter = NULL;
 52	mName = "targeting";
 53
 54	mTorsoState = new LLJointState;
 55}
 56
 57
 58//-----------------------------------------------------------------------------
 59// ~LLTargetingMotion()
 60// Class Destructor
 61//-----------------------------------------------------------------------------
 62LLTargetingMotion::~LLTargetingMotion()
 63{
 64}
 65
 66//-----------------------------------------------------------------------------
 67// LLTargetingMotion::onInitialize(LLCharacter *character)
 68//-----------------------------------------------------------------------------
 69LLMotion::LLMotionInitStatus LLTargetingMotion::onInitialize(LLCharacter *character)
 70{
 71	// save character for future use
 72	mCharacter = character;
 73
 74	mPelvisJoint = mCharacter->getJoint("mPelvis");
 75	mTorsoJoint = mCharacter->getJoint("mTorso");
 76	mRightHandJoint = mCharacter->getJoint("mWristRight");
 77
 78	// make sure character skeleton is copacetic
 79	if (!mPelvisJoint ||
 80		!mTorsoJoint ||
 81		!mRightHandJoint)
 82	{
 83		llwarns << "Invalid skeleton for targeting motion!" << llendl;
 84		return STATUS_FAILURE;
 85	}
 86
 87	mTorsoState->setJoint( mTorsoJoint );
 88
 89	// add joint states to the pose
 90	mTorsoState->setUsage(LLJointState::ROT);
 91	addJointState( mTorsoState );
 92
 93	return STATUS_SUCCESS;
 94}
 95
 96//-----------------------------------------------------------------------------
 97// LLTargetingMotion::onActivate()
 98//-----------------------------------------------------------------------------
 99BOOL LLTargetingMotion::onActivate()
100{
101	return TRUE;
102}
103
104//-----------------------------------------------------------------------------
105// LLTargetingMotion::onUpdate()
106//-----------------------------------------------------------------------------
107BOOL LLTargetingMotion::onUpdate(F32 time, U8* joint_mask)
108{
109	F32 slerp_amt = LLCriticalDamp::getInterpolant(TORSO_TARGET_HALF_LIFE);
110
111	LLVector3 target;
112	LLVector3* lookAtPoint = (LLVector3*)mCharacter->getAnimationData("LookAtPoint");
113
114	BOOL result = TRUE;
115
116	if (!lookAtPoint)
117	{
118		return TRUE;
119	}
120	else
121	{
122		target = *lookAtPoint;
123		target.normVec();
124	}
125	
126	//LLVector3 target_plane_normal = LLVector3(1.f, 0.f, 0.f) * mPelvisJoint->getWorldRotation();
127	//LLVector3 torso_dir = LLVector3(1.f, 0.f, 0.f) * (mTorsoJoint->getWorldRotation() * mTorsoState->getRotation());
128
129	LLVector3 skyward(0.f, 0.f, 1.f);
130	LLVector3 left(skyward % target);
131	left.normVec();
132	LLVector3 up(target % left);
133	up.normVec();
134	LLQuaternion target_aim_rot(target, left, up);
135
136	LLQuaternion cur_torso_rot = mTorsoJoint->getWorldRotation();
137
138	LLVector3 right_hand_at = LLVector3(0.f, -1.f, 0.f) * mRightHandJoint->getWorldRotation();
139	left.setVec(skyward % right_hand_at);
140	left.normVec();
141	up.setVec(right_hand_at % left);
142	up.normVec();
143	LLQuaternion right_hand_rot(right_hand_at, left, up);
144
145	LLQuaternion new_torso_rot = (cur_torso_rot * ~right_hand_rot) * target_aim_rot;
146
147	// find ideal additive rotation to make torso point in correct direction
148	new_torso_rot = new_torso_rot * ~cur_torso_rot;
149
150	// slerp from current additive rotation to ideal additive rotation
151	new_torso_rot = nlerp(slerp_amt, mTorsoState->getRotation(), new_torso_rot);
152
153	// constraint overall torso rotation
154	LLQuaternion total_rot = new_torso_rot * mTorsoJoint->getRotation();
155	total_rot.constrain(F_PI_BY_TWO * 0.8f);
156	new_torso_rot = total_rot * ~mTorsoJoint->getRotation();
157
158	mTorsoState->setRotation(new_torso_rot);
159
160	return result;
161}
162
163//-----------------------------------------------------------------------------
164// LLTargetingMotion::onDeactivate()
165//-----------------------------------------------------------------------------
166void LLTargetingMotion::onDeactivate()
167{
168}
169
170
171// End