llphysicsmotion.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /**
  2. * @file llphysicsmotion.cpp
  3. * @brief Implementation of LLPhysicsMotion class.
  4. *
  5. * $LicenseInfo:firstyear=2011&license=viewerlgpl$
  6. *
  7. * Copyright (C) 2011, 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 "llviewerprecompiledheaders.h"
  33. #include "llphysicsmotion.h"
  34. #include "llcharacter.h"
  35. #include "lldriverparam.h"
  36. #include "llfasttimer.h"
  37. #include "llviewervisualparam.h"
  38. #include "llagent.h"
  39. #include "llviewercontrol.h"
  40. #include "llvoavatarself.h"
  41. typedef std::map<std::string, std::string> controller_map_t;
  42. typedef std::map<std::string, F32> default_controller_map_t;
  43. #define MIN_REQUIRED_PIXEL_AREA_AVATAR_PHYSICS_MOTION 0.f
  44. // We use TIME_ITERATION_STEP_MAX in division operation, make sure this is not
  45. // an irrational value so that division won't end with repeated/recurring tail
  46. // like 1.333(3)
  47. #define TIME_ITERATION_STEP_MAX 0.05f
  48. LL_INLINE F32 llsgn(F32 a)
  49. {
  50. return a >= 0.f ? 1.f : -1.f;
  51. }
  52. /*
  53. At a high level, this works by setting temporary parameters that are not
  54. stored in the avatar's list of params, and are not conveyed to other users.
  55. We accomplish this by creating some new temporary driven params inside
  56. avatar_lad that are then driven by the actual params that the user sees and
  57. sets. For example, in the old system, the user sets a param called breast
  58. bouyancy, which controls the Z value of the breasts.
  59. In our new system, the user still sets the breast bouyancy, but that param
  60. is redefined as a driver param so that affects a new temporary driven param
  61. that the bounce is applied to.
  62. */
  63. class LLPhysicsMotion
  64. {
  65. public:
  66. typedef enum
  67. {
  68. SMOOTHING = 0,
  69. MASS,
  70. GRAVITY,
  71. SPRING,
  72. GAIN,
  73. DAMPING,
  74. DRAG,
  75. MAX_EFFECT,
  76. NUM_PARAMS
  77. } eParamName;
  78. /*
  79. param_driver_name: The param that controls the params that are being
  80. affected by the physics.
  81. joint_name: The joint that the body part is attached to. The joint is
  82. used to determine the orientation (rotation) of the body part.
  83. character: The avatar that this physics affects.
  84. motion_direction_vec: The direction (in world coordinates) that
  85. determines the motion. For example, (0, 0, 1) is up-down, and means
  86. that up-down motion is what determines how this joint moves.
  87. controllers: The various settings (e.g. spring force, mass) that
  88. determine how the body part behaves.
  89. */
  90. LLPhysicsMotion(const std::string& param_driver_name, U32 joint_key,
  91. LLCharacter* character,
  92. const LLVector3& motion_direction_vec,
  93. const controller_map_t& controllers)
  94. : mParamDriverName(param_driver_name),
  95. mJointKey(joint_key),
  96. mMotionDirectionVec(motion_direction_vec),
  97. mParamDriver(NULL),
  98. mParamControllers(controllers),
  99. mCharacter(character),
  100. mLastTime(0),
  101. mPosition_local(0.f),
  102. mVelocityJoint_local(0.f),
  103. mPositionLastUpdate_local(0.f),
  104. mAccelerationJoint_local(0.f)
  105. {
  106. mJointState = new LLJointState;
  107. for (U32 i = 0; i < NUM_PARAMS; ++i)
  108. {
  109. mParamCache[i] = NULL;
  110. }
  111. }
  112. bool initialize();
  113. ~LLPhysicsMotion() {}
  114. bool onUpdate(F32 time);
  115. LLPointer<LLJointState> getJointState()
  116. {
  117. return mJointState;
  118. }
  119. protected:
  120. F32 getParamValue(eParamName param)
  121. {
  122. static std::string controller_key[] =
  123. {
  124. "Smoothing",
  125. "Mass",
  126. "Gravity",
  127. "Spring",
  128. "Gain",
  129. "Damping",
  130. "Drag",
  131. "MaxEffect"
  132. };
  133. if (!mParamCache[param])
  134. {
  135. const controller_map_t::const_iterator& entry =
  136. mParamControllers.find(controller_key[param]);
  137. if (entry == mParamControllers.end())
  138. {
  139. return sDefaultController[controller_key[param]];
  140. }
  141. const std::string& param_name = entry->second.c_str();
  142. mParamCache[param] = mCharacter->getVisualParam(param_name.c_str());
  143. }
  144. if (mParamCache[param])
  145. {
  146. return mParamCache[param]->getWeight();
  147. }
  148. else
  149. {
  150. return sDefaultController[controller_key[param]];
  151. }
  152. }
  153. void setParamValue(const LLViewerVisualParam* param,
  154. F32 new_value_local, F32 behavior_maxeffect);
  155. F32 toLocal(const LLVector3& world);
  156. F32 calculateVelocity_local(F32 time_delta);
  157. F32 calculateAcceleration_local(F32 velocity_local, F32 time_delta);
  158. private:
  159. const std::string mParamDriverName;
  160. const std::string mParamControllerName;
  161. const LLVector3 mMotionDirectionVec;
  162. U32 mJointKey;
  163. F32 mPosition_local;
  164. F32 mVelocityJoint_local; // How fast the joint is moving
  165. F32 mAccelerationJoint_local; // Acceleration on the joint
  166. F32 mVelocity_local; // How fast the param is moving
  167. F32 mPositionLastUpdate_local;
  168. LLVector3 mPosition_world;
  169. LLViewerVisualParam *mParamDriver;
  170. const controller_map_t mParamControllers;
  171. LLPointer<LLJointState> mJointState;
  172. LLCharacter *mCharacter;
  173. F32 mLastTime;
  174. LLVisualParam* mParamCache[NUM_PARAMS];
  175. static default_controller_map_t sDefaultController;
  176. };
  177. default_controller_map_t initDefaultController()
  178. {
  179. default_controller_map_t controller;
  180. controller["Mass"] = 0.2f;
  181. controller["Gravity"] = 0.f;
  182. controller["Damping"] = .05f;
  183. controller["Drag"] = 0.15f;
  184. controller["MaxEffect"] = 0.1f;
  185. controller["Spring"] = 0.1f;
  186. controller["Gain"] = 10.f;
  187. return controller;
  188. }
  189. default_controller_map_t LLPhysicsMotion::sDefaultController = initDefaultController();
  190. bool LLPhysicsMotion::initialize()
  191. {
  192. if (!mJointState->setJoint(mCharacter->getJoint(mJointKey)))
  193. {
  194. return false;
  195. }
  196. mJointState->setUsage(LLJointState::ROT);
  197. mParamDriver = (LLViewerVisualParam*)mCharacter->getVisualParam(mParamDriverName.c_str());
  198. if (mParamDriver == NULL)
  199. {
  200. llinfos << "Failure reading in [ " << mParamDriverName << " ]"
  201. << llendl;
  202. return false;
  203. }
  204. return true;
  205. }
  206. LLPhysicsMotionController::LLPhysicsMotionController(const LLUUID& id)
  207. : LLMotion(id),
  208. mCharacter(NULL)
  209. {
  210. mName = "breast_motion";
  211. }
  212. LLPhysicsMotionController::~LLPhysicsMotionController()
  213. {
  214. for (motion_vec_t::iterator iter = mMotions.begin();
  215. iter != mMotions.end(); ++iter)
  216. {
  217. delete (*iter);
  218. }
  219. }
  220. LLMotion::LLMotionInitStatus LLPhysicsMotionController::onInitialize(LLCharacter* character)
  221. {
  222. LL_FAST_TIMER(FTM_AVATAR_UPDATE);
  223. {
  224. LL_FAST_TIMER(FTM_PHYSICS_UPDATE);
  225. mCharacter = character;
  226. mMotions.clear();
  227. // Breast Cleavage
  228. {
  229. controller_map_t controller;
  230. controller["Mass"] = "Breast_Physics_Mass";
  231. controller["Gravity"] = "Breast_Physics_Gravity";
  232. controller["Drag"] = "Breast_Physics_Drag";
  233. controller["Damping"] = "Breast_Physics_InOut_Damping";
  234. controller["MaxEffect"] = "Breast_Physics_InOut_Max_Effect";
  235. controller["Spring"] = "Breast_Physics_InOut_Spring";
  236. controller["Gain"] = "Breast_Physics_InOut_Gain";
  237. LLPhysicsMotion* motion =
  238. new LLPhysicsMotion("Breast_Physics_InOut_Controller",
  239. LL_JOINT_KEY_CHEST, character,
  240. LLVector3::x_axis_neg, controller);
  241. if (!motion->initialize())
  242. {
  243. llassert_always(false);
  244. return STATUS_FAILURE;
  245. }
  246. addMotion(motion);
  247. }
  248. // Breast Bounce
  249. {
  250. controller_map_t controller;
  251. controller["Mass"] = "Breast_Physics_Mass";
  252. controller["Gravity"] = "Breast_Physics_Gravity";
  253. controller["Drag"] = "Breast_Physics_Drag";
  254. controller["Damping"] = "Breast_Physics_UpDown_Damping";
  255. controller["MaxEffect"] = "Breast_Physics_UpDown_Max_Effect";
  256. controller["Spring"] = "Breast_Physics_UpDown_Spring";
  257. controller["Gain"] = "Breast_Physics_UpDown_Gain";
  258. LLPhysicsMotion* motion =
  259. new LLPhysicsMotion("Breast_Physics_UpDown_Controller",
  260. LL_JOINT_KEY_CHEST, character,
  261. LLVector3::z_axis, controller);
  262. if (!motion->initialize())
  263. {
  264. llassert_always(false);
  265. return STATUS_FAILURE;
  266. }
  267. addMotion(motion);
  268. }
  269. // Breast Sway
  270. {
  271. controller_map_t controller;
  272. controller["Mass"] = "Breast_Physics_Mass";
  273. controller["Gravity"] = "Breast_Physics_Gravity";
  274. controller["Drag"] = "Breast_Physics_Drag";
  275. controller["Damping"] = "Breast_Physics_LeftRight_Damping";
  276. controller["MaxEffect"] = "Breast_Physics_LeftRight_Max_Effect";
  277. controller["Spring"] = "Breast_Physics_LeftRight_Spring";
  278. controller["Gain"] = "Breast_Physics_LeftRight_Gain";
  279. LLPhysicsMotion* motion =
  280. new LLPhysicsMotion("Breast_Physics_LeftRight_Controller",
  281. LL_JOINT_KEY_CHEST, character,
  282. LLVector3::y_axis_neg, controller);
  283. if (!motion->initialize())
  284. {
  285. llassert_always(false);
  286. return STATUS_FAILURE;
  287. }
  288. addMotion(motion);
  289. }
  290. // Butt Bounce
  291. {
  292. controller_map_t controller;
  293. controller["Mass"] = "Butt_Physics_Mass";
  294. controller["Gravity"] = "Butt_Physics_Gravity";
  295. controller["Drag"] = "Butt_Physics_Drag";
  296. controller["Damping"] = "Butt_Physics_UpDown_Damping";
  297. controller["MaxEffect"] = "Butt_Physics_UpDown_Max_Effect";
  298. controller["Spring"] = "Butt_Physics_UpDown_Spring";
  299. controller["Gain"] = "Butt_Physics_UpDown_Gain";
  300. LLPhysicsMotion* motion =
  301. new LLPhysicsMotion("Butt_Physics_UpDown_Controller",
  302. LL_JOINT_KEY_PELVIS, character,
  303. LLVector3::z_axis_neg, controller);
  304. if (!motion->initialize())
  305. {
  306. llassert_always(false);
  307. return STATUS_FAILURE;
  308. }
  309. addMotion(motion);
  310. }
  311. // Butt LeftRight
  312. {
  313. controller_map_t controller;
  314. controller["Mass"] = "Butt_Physics_Mass";
  315. controller["Gravity"] = "Butt_Physics_Gravity";
  316. controller["Drag"] = "Butt_Physics_Drag";
  317. controller["Damping"] = "Butt_Physics_LeftRight_Damping";
  318. controller["MaxEffect"] = "Butt_Physics_LeftRight_Max_Effect";
  319. controller["Spring"] = "Butt_Physics_LeftRight_Spring";
  320. controller["Gain"] = "Butt_Physics_LeftRight_Gain";
  321. LLPhysicsMotion* motion =
  322. new LLPhysicsMotion("Butt_Physics_LeftRight_Controller",
  323. LL_JOINT_KEY_PELVIS, character,
  324. LLVector3::y_axis_neg, controller);
  325. if (!motion->initialize())
  326. {
  327. llassert_always(false);
  328. return STATUS_FAILURE;
  329. }
  330. addMotion(motion);
  331. }
  332. // Belly Bounce
  333. {
  334. controller_map_t controller;
  335. controller["Mass"] = "Belly_Physics_Mass";
  336. controller["Gravity"] = "Belly_Physics_Gravity";
  337. controller["Drag"] = "Belly_Physics_Drag";
  338. controller["Damping"] = "Belly_Physics_UpDown_Damping";
  339. controller["MaxEffect"] = "Belly_Physics_UpDown_Max_Effect";
  340. controller["Spring"] = "Belly_Physics_UpDown_Spring";
  341. controller["Gain"] = "Belly_Physics_UpDown_Gain";
  342. LLPhysicsMotion* motion =
  343. new LLPhysicsMotion("Belly_Physics_UpDown_Controller",
  344. LL_JOINT_KEY_PELVIS, character,
  345. LLVector3::z_axis_neg, controller);
  346. if (!motion->initialize())
  347. {
  348. llassert_always(false);
  349. return STATUS_FAILURE;
  350. }
  351. addMotion(motion);
  352. }
  353. }
  354. return STATUS_SUCCESS;
  355. }
  356. void LLPhysicsMotionController::addMotion(LLPhysicsMotion* motion)
  357. {
  358. addJointState(motion->getJointState());
  359. mMotions.push_back(motion);
  360. }
  361. F32 LLPhysicsMotionController::getMinPixelArea()
  362. {
  363. return MIN_REQUIRED_PIXEL_AREA_AVATAR_PHYSICS_MOTION;
  364. }
  365. // Local space means "parameter space".
  366. F32 LLPhysicsMotion::toLocal(const LLVector3& world)
  367. {
  368. LLJoint* joint = mJointState->getJoint();
  369. const LLQuaternion rotation_world = joint->getWorldRotation();
  370. LLVector3 dir_world = mMotionDirectionVec * rotation_world;
  371. dir_world.normalize();
  372. return world * dir_world;
  373. }
  374. F32 LLPhysicsMotion::calculateVelocity_local(F32 time_delta)
  375. {
  376. if (time_delta <= 0.f) return 0.f;
  377. constexpr F32 world_to_model_scale = 100.f;
  378. LLJoint* joint = mJointState->getJoint();
  379. const LLVector3 position_world = joint->getWorldPosition();
  380. const LLVector3 last_position_world = mPosition_world;
  381. const LLVector3 positionchange_world = (position_world - last_position_world) *
  382. world_to_model_scale;
  383. const F32 velocity_local = toLocal(positionchange_world) / time_delta;
  384. return velocity_local;
  385. }
  386. F32 LLPhysicsMotion::calculateAcceleration_local(F32 velocity_local,
  387. F32 time_delta)
  388. {
  389. if (time_delta <= 0.f) return 0.f;
  390. #if 0 // Removed smoothing param since it is probably not necessary:
  391. const F32 smoothing = getParamValue("Smoothing");
  392. #endif
  393. constexpr F32 smoothing = 3.f;
  394. constexpr F32 factor = (smoothing - 1.f) / smoothing;
  395. F32 accel_loc = (velocity_local - mVelocityJoint_local) / time_delta;
  396. F32 smoothed_accel = accel_loc / smoothing +
  397. mAccelerationJoint_local * factor;
  398. return smoothed_accel;
  399. }
  400. bool LLPhysicsMotionController::onUpdate(F32 time, U8* joint_mask)
  401. {
  402. LL_FAST_TIMER(FTM_AVATAR_UPDATE);
  403. // Skip if disabled globally.
  404. if (!LLVOAvatar::sAvatarPhysics)
  405. {
  406. return true;
  407. }
  408. {
  409. LL_FAST_TIMER(FTM_PHYSICS_UPDATE);
  410. bool update_visuals = false;
  411. for (motion_vec_t::iterator iter = mMotions.begin(),
  412. end = mMotions.end();
  413. iter != end; ++iter)
  414. {
  415. LLPhysicsMotion* motion = *iter;
  416. if (motion) // Paranoia
  417. {
  418. update_visuals |= motion->onUpdate(time);
  419. }
  420. }
  421. if (update_visuals)
  422. {
  423. mCharacter->updateVisualParams();
  424. }
  425. }
  426. return true;
  427. }
  428. // Returns true if character has to update visual params.
  429. bool LLPhysicsMotion::onUpdate(F32 time)
  430. {
  431. if (!mParamDriver)
  432. {
  433. return false;
  434. }
  435. if (!mLastTime || mLastTime >= time)
  436. {
  437. mLastTime = time;
  438. return false;
  439. }
  440. ///////////////////////////////////////////////////////////////////////////
  441. // Get all parameters and settings
  442. const F32 time_delta = time - mLastTime;
  443. #if 0
  444. // Do not update too frequently, to avoid precision errors from small time
  445. // slices.
  446. if (time_delta <= .01f)
  447. {
  448. return false;
  449. }
  450. #endif
  451. // If less than 1FPS, we don't want to be spending time updating physics
  452. // at all.
  453. if (time_delta > 1.f)
  454. {
  455. mLastTime = time;
  456. return false;
  457. }
  458. // Higher LOD is better. This controls the granularity and frequency of
  459. // updates for the motions.
  460. const F32 lod_factor = LLVOAvatar::sPhysicsLODFactor;
  461. if (lod_factor == 0.f)
  462. {
  463. return true;
  464. }
  465. LLJoint* joint = mJointState->getJoint();
  466. const F32 behavior_mass = getParamValue(MASS);
  467. const F32 behavior_gravity = getParamValue(GRAVITY);
  468. const F32 behavior_spring = getParamValue(SPRING);
  469. const F32 behavior_gain = getParamValue(GAIN);
  470. const F32 behavior_damping = getParamValue(DAMPING);
  471. const F32 behavior_drag = getParamValue(DRAG);
  472. F32 behavior_maxeffect = getParamValue(MAX_EFFECT);
  473. // Normalize the param position to be from [0,1].
  474. // We have to use normalized values because there may be more than one
  475. // driven param, and each of these driven params may have its own range.
  476. // This means we'll do all our calculations in normalized [0,1] local
  477. // coordinates.
  478. const F32 position_user_local = (mParamDriver->getWeight() -
  479. mParamDriver->getMinWeight()) /
  480. (mParamDriver->getMaxWeight() -
  481. mParamDriver->getMinWeight());
  482. // End parameters and settings
  483. ///////////////////////////////////////////////////////////////////////////
  484. ///////////////////////////////////////////////////////////////////////////
  485. // Calculate velocity and acceleration in parameter space.
  486. const F32 joint_local_factor = 30.f;
  487. const F32 velocity_joint_local =
  488. calculateVelocity_local(time_delta * joint_local_factor);
  489. const F32 acceleration_joint_local =
  490. calculateAcceleration_local(velocity_joint_local,
  491. time_delta * joint_local_factor);
  492. // End velocity and acceleration
  493. ///////////////////////////////////////////////////////////////////////////
  494. bool update_visuals = false;
  495. // Break up the physics into a bunch of iterations so that differing
  496. // framerates will show roughly the same behavior.
  497. U32 steps = (U32)(time_delta / TIME_ITERATION_STEP_MAX) + 1;
  498. // Note: minimal time_iteration_step ends up as 0.025
  499. F32 time_iteration_step = time_delta / (F32)steps;
  500. for (U32 i = 0; i < steps; ++i)
  501. {
  502. // mPositon_local should be in normalized 0,1 range already. Just
  503. // making sure...
  504. const F32 position_current_local = llclamp(mPosition_local, 0.f, 1.f);
  505. // If the effect is turned off then don't process unless we need one
  506. // more update to set the position to the default (i.e. user) position.
  507. if (behavior_maxeffect == 0 &&
  508. position_current_local == position_user_local)
  509. {
  510. return update_visuals;
  511. }
  512. ///////////////////////////////////////////////////////////////////////
  513. // Calculate the total force
  514. // Spring force is a restoring force towards the original user-set
  515. // breast position. F = kx
  516. const F32 spring_length = position_current_local - position_user_local;
  517. const F32 force_spring = -spring_length * behavior_spring;
  518. // Acceleration is the force that comes from the change in velocity of
  519. // the torso. F = ma
  520. const F32 force_accel = behavior_gain * (acceleration_joint_local *
  521. behavior_mass);
  522. // Gravity always points downward in world space. F = mg
  523. const LLVector3 gravity_world(0.f, 0.f, 1.f);
  524. const F32 force_gravity = toLocal(gravity_world) *
  525. behavior_gravity * behavior_mass;
  526. // Damping is a restoring force that opposes the current velocity.
  527. // F = -kv
  528. const F32 force_damping = -behavior_damping * mVelocity_local;
  529. // Drag is a force imparted by velocity (intuitively it is similar to
  530. // wind resistance). F = .5kv^2
  531. const F32 force_drag = .5f * behavior_drag * velocity_joint_local *
  532. velocity_joint_local *
  533. llsgn(velocity_joint_local);
  534. const F32 force_net = force_accel + force_gravity + force_spring +
  535. force_damping + force_drag;
  536. // End total force
  537. ///////////////////////////////////////////////////////////////////////
  538. ///////////////////////////////////////////////////////////////////////
  539. // Calculate new params
  540. // Calculate the new acceleration based on the net force. a = F/m
  541. const F32 acceleration_new_local = force_net / behavior_mass;
  542. // magic number, used to be customizable:
  543. constexpr F32 max_velocity = 100.f;
  544. F32 velocity_new_local = mVelocity_local +
  545. acceleration_new_local * time_iteration_step;
  546. velocity_new_local = llclamp(velocity_new_local, -max_velocity,
  547. max_velocity);
  548. // Calculate the new parameters, or remain unchanged if max speed is 0.
  549. F32 new_pos_local = position_current_local +
  550. velocity_new_local * time_iteration_step;
  551. if (behavior_maxeffect == 0)
  552. {
  553. new_pos_local = position_user_local;
  554. }
  555. // Zero out the velocity if the param is being pushed beyond its limits
  556. if ((new_pos_local < 0.f && velocity_new_local < 0.f) ||
  557. (new_pos_local > 1.f && velocity_new_local > 0.f))
  558. {
  559. velocity_new_local = 0.f;
  560. }
  561. // Check for NaN values. A NaN value is detected if the variables does
  562. // not equal itself. If NaN, then reset everything.
  563. if (mPosition_local != mPosition_local ||
  564. mVelocity_local != mVelocity_local ||
  565. new_pos_local != new_pos_local)
  566. {
  567. new_pos_local = mPosition_local = 0.f;
  568. mVelocity_local = mVelocityJoint_local = 0.f;
  569. mAccelerationJoint_local = 0.f;
  570. mPosition_world.clear();
  571. }
  572. const F32 new_pos_local_clamped = llclamp(new_pos_local, 0.f, 1.f);
  573. LLDriverParam* driver_param = mParamDriver->asDriverParam();
  574. if (!driver_param)
  575. {
  576. llerrs << "Not a driver param !" << llendl;
  577. }
  578. // If this is one of our "hidden" driver params, then make sure it is
  579. // the default value.
  580. if (driver_param->getGroup() != VISUAL_PARAM_GROUP_TWEAKABLE &&
  581. driver_param->getGroup() != VISUAL_PARAM_GROUP_TWEAKABLE_NO_TRANSMIT)
  582. {
  583. mCharacter->setVisualParamWeight(driver_param, 0, false);
  584. }
  585. S32 num_driven = driver_param->getDrivenParamsCount();
  586. for (S32 i = 0; i < num_driven; ++i)
  587. {
  588. const LLViewerVisualParam* driven_param =
  589. driver_param->getDrivenParam(i);
  590. setParamValue(driven_param, new_pos_local_clamped,
  591. behavior_maxeffect);
  592. }
  593. // End calculate new params
  594. ///////////////////////////////////////////////////////////////////////
  595. ///////////////////////////////////////////////////////////////////////
  596. // Conditionally update the visual params
  597. // Updating the visual params (i.e. what the user sees) is fairly
  598. // expensive, so only update if the params have changed enough, and
  599. // also take into account the graphics LOD settings.
  600. // For non-self, if the avatar is small enough visually, then don't update.
  601. constexpr F32 area_for_max_settings = 0.f;
  602. constexpr F32 area_for_min_settings = 1400.f;
  603. const F32 area_for_this_setting = area_for_max_settings +
  604. (area_for_min_settings -
  605. area_for_max_settings) *
  606. (1.f - lod_factor);
  607. const F32 pixel_area = sqrtf(mCharacter->getPixelArea());
  608. // Note: the following static cast is only valid because the sole child
  609. // class of LLCharacter is LLAvatarAppearance which itself got for sole
  610. // child class LLVOAvatar. Should this change in the future, this cast
  611. // would become illegal.
  612. LLVOAvatar* avatarp = (LLVOAvatar*)((LLAvatarAppearance*)mCharacter);
  613. bool is_self = avatarp && avatarp->isSelf();
  614. if ((pixel_area > area_for_this_setting) || is_self)
  615. {
  616. const F32 position_diff_local = fabsf(mPositionLastUpdate_local -
  617. new_pos_local_clamped);
  618. const F32 min_delta = (1.0001f - lod_factor) * 0.4f;
  619. if (fabsf(position_diff_local) > min_delta)
  620. {
  621. update_visuals = true;
  622. mPositionLastUpdate_local = new_pos_local;
  623. }
  624. }
  625. // End update visual params
  626. ///////////////////////////////////////////////////////////////////////
  627. mVelocity_local = velocity_new_local;
  628. mAccelerationJoint_local = acceleration_joint_local;
  629. mPosition_local = new_pos_local;
  630. }
  631. mLastTime = time;
  632. mPosition_world = joint->getWorldPosition();
  633. mVelocityJoint_local = velocity_joint_local;
  634. return update_visuals;
  635. }
  636. // Range of new_value_local is assumed to be [0 , 1] normalized.
  637. void LLPhysicsMotion::setParamValue(const LLViewerVisualParam* param,
  638. F32 new_value_normalized,
  639. F32 behavior_maxeffect)
  640. {
  641. const F32 value_min_local = param->getMinWeight();
  642. const F32 value_max_local = param->getMaxWeight();
  643. const F32 min_val = (1.f - behavior_maxeffect) * 0.5f;
  644. const F32 max_val = (1.f + behavior_maxeffect) * 0.5f;
  645. // Scale from [0,1] to [min_val,max_val]
  646. const F32 new_value_rescaled = min_val +
  647. (max_val - min_val) * new_value_normalized;
  648. // Scale from [0,1] to [value_min_local,value_max_local]
  649. const F32 new_value_local = value_min_local +
  650. (value_max_local - value_min_local) *
  651. new_value_rescaled;
  652. mCharacter->setVisualParamWeight(param, new_value_local, false);
  653. }