/indra/newview/llphysicsmotion.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 732 lines · 473 code · 99 blank · 160 comment · 52 complexity · 1ec64911da527e131c1d491298940f99 MD5 · raw file

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