1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
|
/**
* @file llphysicsmotion.cpp
* @brief Implementation of LLPhysicsMotion class.
*
* $LicenseInfo:firstyear=2001&license=viewergpl$
*
* Copyright (c) 2001-2009, Linden Research, Inc.
*
* Second Life Viewer Source Code
* The source code in this file ("Source Code") is provided by Linden Lab
* to you under the terms of the GNU General Public License, version 2.0
* ("GPL"), unless you have obtained a separate licensing agreement
* ("Other License"), formally executed by you and Linden Lab. Terms of
* the GPL can be found in doc/GPL-license.txt in this distribution, or
* online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
*
* There are special exceptions to the terms and conditions of the GPL as
* it is applied to this Source Code. View the full text of the exception
* in the file doc/FLOSS-exception.txt in this software distribution, or
* online at
* http://secondlifegrid.net/programs/open_source/licensing/flossexception
*
* By copying, modifying or distributing this software, you acknowledge
* that you have read and understood your obligations described above,
* and agree to abide by those obligations.
*
* ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
* WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
* COMPLETENESS OR PERFORMANCE.
* $/LicenseInfo$
*/
//-----------------------------------------------------------------------------
// Header Files
//-----------------------------------------------------------------------------
#include "llviewerprecompiledheaders.h"
#include "linden_common.h"
#include "m3math.h"
#include "v3dmath.h"
#include "llphysicsmotion.h"
#include "llcharacter.h"
#include "llviewercontrol.h"
#include "llviewervisualparam.h"
#include "llvoavatarself.h"
typedef std::map<std::string, std::string> controller_map_t;
typedef std::map<std::string, F32> default_controller_map_t;
#define MIN_REQUIRED_PIXEL_AREA_BREAST_MOTION 0.f;
inline F64 llsgn(const F64 a)
{
if (a >= 0)
return 1;
return -1;
}
/*
At a high level, this works by setting temporary parameters that are not stored
in the avatar's list of params, and are not conveyed to other users. We accomplish
this by creating some new temporary driven params inside avatar_lad that are then driven
by the actual params that the user sees and sets. For example, in the old system,
the user sets a param called breast bouyancy, which controls the Z value of the breasts.
In our new system, the user still sets the breast bouyancy, but that param is redefined
as a driver param so that affects a new temporary driven param that the bounce is applied
to.
*/
class LLPhysicsMotion
{
public:
/*
param_user_name: The param (if any) that the user sees and controls. This is what
the particular property would look like without physics. For example, it may be
the breast gravity. This param's value should will not be altered, and is only
used as a reference point for the rest position of the body party. This is usually
a driver param and the param(s) that physics is altering are the driven params.
param_driven_name: The param whose value is actually set by the physics. If you
leave this blank (which should suffice normally), the physics will assume that
param_user_name is a driver param and will set the params that the driver is
in charge of (i.e. the "driven" params).
joint_name: The joint that the body part is attached to. The joint is
used to determine the orientation (rotation) of the body part.
character: The avatar that this physics affects.
motion_direction_vec: The direction (in world coordinates) that determines the
motion. For example, (0,0,1) is up-down, and means that up-down motion is what
determines how this joint moves.
controllers: The various settings (e.g. spring force, mass) that determine how
the body part behaves.
*/
LLPhysicsMotion(const std::string ¶m_user_name,
const std::string ¶m_driven_name,
const std::string &joint_name,
LLCharacter *character,
const LLVector3 &motion_direction_vec,
const controller_map_t &controllers) :
mParamUserName(param_user_name),
mParamDrivenName(param_driven_name),
mJointName(joint_name),
mMotionDirectionVec(motion_direction_vec),
mParamUser(NULL),
mParamDriven(NULL),
mParamControllers(controllers),
mCharacter(character),
mLastTime(0),
mPosition_local(0),
mVelocityJoint_local(0),
mPositionLastUpdate_local(0)
{
mJointState = new LLJointState;
}
BOOL initialize();
~LLPhysicsMotion() {}
BOOL onUpdate(F32 time);
LLPointer<LLJointState> getJointState()
{
return mJointState;
}
protected:
F32 getParamValue(const std::string& controller_key)
{
const controller_map_t::const_iterator& entry = mParamControllers.find(controller_key);
if (entry == mParamControllers.end())
{
return sDefaultController[controller_key];
}
const std::string& param_name = (*entry).second.c_str();
return mCharacter->getVisualParamWeight(param_name.c_str());
}
void setParamValue(LLViewerVisualParam *param,
const F32 new_value_local);
F32 toLocal(const LLVector3 &world);
F32 calculateVelocity_local(const F32 time_delta);
F32 calculateAcceleration_local(F32 velocity_local,
const F32 time_delta);
private:
const std::string mParamDrivenName;
const std::string mParamUserName;
const LLVector3 mMotionDirectionVec;
const std::string mJointName;
F32 mPosition_local;
F32 mVelocityJoint_local; // How fast the joint is moving
F32 mAccelerationJoint_local; // Acceleration on the joint
F32 mVelocity_local; // How fast the param is moving
F32 mPositionLastUpdate_local;
LLVector3 mPosition_world;
LLViewerVisualParam *mParamUser;
LLViewerVisualParam *mParamDriven;
const controller_map_t mParamControllers;
LLPointer<LLJointState> mJointState;
LLCharacter *mCharacter;
F32 mLastTime;
static default_controller_map_t sDefaultController;
};
default_controller_map_t initDefaultController()
{
default_controller_map_t controller;
controller["Mass"] = 0.2f;
controller["Smoothing"] = 2.0f;
controller["Gravity"] = 0.0f;
controller["Damping"] = .05f;
controller["Drag"] = 0.15f;
controller["MaxSpeed"] = 0.1f;
controller["Spring"] = 0.1f;
controller["Gain"] = 10.0f;
return controller;
}
default_controller_map_t LLPhysicsMotion::sDefaultController = initDefaultController();
BOOL LLPhysicsMotion::initialize()
{
if (!mJointState->setJoint(mCharacter->getJoint(mJointName.c_str())))
return FALSE;
mJointState->setUsage(LLJointState::ROT);
mParamUser = (LLViewerVisualParam*)mCharacter->getVisualParam(mParamUserName.c_str());
if (mParamDrivenName != "")
mParamDriven = (LLViewerVisualParam*)mCharacter->getVisualParam(mParamDrivenName.c_str());
if (mParamUser == NULL)
{
llinfos << "Failure reading in [ " << mParamUserName << " ]" << llendl;
return FALSE;
}
return TRUE;
}
LLPhysicsMotionController::LLPhysicsMotionController(const LLUUID &id) :
LLMotion(id),
mCharacter(NULL)
{
mName = "breast_motion";
}
LLPhysicsMotionController::~LLPhysicsMotionController()
{
for (motion_vec_t::iterator iter = mMotions.begin();
iter != mMotions.end();
++iter)
{
delete (*iter);
}
}
BOOL LLPhysicsMotionController::onActivate()
{
return TRUE;
}
void LLPhysicsMotionController::onDeactivate()
{
}
LLMotion::LLMotionInitStatus LLPhysicsMotionController::onInitialize(LLCharacter *character)
{
mCharacter = character;
mMotions.clear();
controller_map_t controllers_cleavage;
controllers_cleavage["Mass"] = "Breast_Physics_Mass";
controllers_cleavage["Smoothing"] = "Breast_Physics_Smoothing";
controllers_cleavage["Gravity"] = "Breast_Physics_Gravity";
controllers_cleavage["Damping"] = "Breast_Physics_Side_Damping";
controllers_cleavage["Drag"] = "Breast_Physics_Side_Drag";
controllers_cleavage["MaxSpeed"] = "Breast_Physics_Side_Max_Velocity";
controllers_cleavage["Spring"] = "Breast_Physics_Side_Spring";
controllers_cleavage["Gain"] = "Breast_Physics_Side_Gain";
LLPhysicsMotion *cleavage_motion = new LLPhysicsMotion("Breast_Physics_Side_Controller",
"",
"mChest",
character,
LLVector3(-1,0,0),
controllers_cleavage);
if (!cleavage_motion->initialize())
return STATUS_FAILURE;
addMotion(cleavage_motion);
controller_map_t controllers_bounce;
controllers_bounce["Mass"] = "Breast_Physics_Mass";
controllers_bounce["Smoothing"] = "Breast_Physics_Smoothing";
controllers_bounce["Gravity"] = "Breast_Physics_Gravity";
controllers_bounce["Damping"] = "Breast_Physics_UpDown_Damping";
controllers_bounce["Drag"] = "Breast_Physics_UpDown_Drag";
controllers_bounce["MaxSpeed"] = "Breast_Physics_UpDown_Max_Velocity";
controllers_bounce["Spring"] = "Breast_Physics_UpDown_Spring";
controllers_bounce["Gain"] = "Breast_Physics_UpDown_Gain";
LLPhysicsMotion *bounce_motion = new LLPhysicsMotion("Breast_Physics_UpDown_Controller",
"",
"mChest",
character,
LLVector3(0,0,1),
controllers_bounce);
if (!bounce_motion->initialize())
{
llassert_always(FALSE);
return STATUS_FAILURE;
}
addMotion(bounce_motion);
controller_map_t controllers_butt_bounce;
controllers_butt_bounce["Damping"] = "Butt_Physics_Updown_Damping";
controllers_butt_bounce["MaxSpeed"] = "Butt_Physics_Updown_Max_Velocity";
controllers_butt_bounce["Spring"] = "Butt_Physics_Updown_Spring";
controllers_butt_bounce["Gain"] = "Butt_Physics_Updown_Gain";
LLPhysicsMotion *butt_bounce_motion = new LLPhysicsMotion("Butt_Physics_UpDown_Controller",
"",
"mPelvis",
character,
LLVector3(0,0,-1),
controllers_butt_bounce);
if (!butt_bounce_motion->initialize())
{
llassert_always(FALSE);
return STATUS_FAILURE;
}
addMotion(butt_bounce_motion);
controller_map_t controllers_belly_bounce;
controllers_belly_bounce["Damping"] = "Belly_Physics_Updown_Damping";
controllers_belly_bounce["MaxSpeed"] = "Belly_Physics_Updown_Max_Velocity";
controllers_belly_bounce["Spring"] = "Belly_Physics_Updown_Spring";
controllers_belly_bounce["Gain"] = "Belly_Physics_Updown_Gain";
LLPhysicsMotion *belly_bounce_motion = new LLPhysicsMotion("Belly_Physics_UpDown_Controller",
"",
"mChest",
character,
LLVector3(0,0,-1),
controllers_belly_bounce);
if (!belly_bounce_motion->initialize())
{
llassert_always(FALSE);
return STATUS_FAILURE;
}
addMotion(belly_bounce_motion);
return STATUS_SUCCESS;
}
void LLPhysicsMotionController::addMotion(LLPhysicsMotion *motion)
{
addJointState(motion->getJointState());
mMotions.push_back(motion);
}
F32 LLPhysicsMotionController::getMinPixelArea()
{
return MIN_REQUIRED_PIXEL_AREA_BREAST_MOTION;
}
// Local space means "parameter space".
F32 LLPhysicsMotion::toLocal(const LLVector3 &world)
{
LLJoint *joint = mJointState->getJoint();
const LLQuaternion rotation_world = joint->getWorldRotation();
LLVector3 dir_world = mMotionDirectionVec * rotation_world;
dir_world.normalize();
return world * dir_world;
}
F32 LLPhysicsMotion::calculateVelocity_local(const F32 time_delta)
{
LLJoint *joint = mJointState->getJoint();
const LLVector3 position_world = joint->getWorldPosition();
const LLQuaternion rotation_world = joint->getWorldRotation();
const LLVector3 last_position_world = mPosition_world;
const LLVector3 velocity_world = (position_world-last_position_world) / time_delta;
const F32 velocity_local = toLocal(velocity_world);
return velocity_local;
}
F32 LLPhysicsMotion::calculateAcceleration_local(const F32 velocity_local,
const F32 time_delta)
{
const F32 smoothing = getParamValue("Smoothing");
const F32 acceleration_local = velocity_local - mVelocityJoint_local;
const F32 smoothed_acceleration_local =
acceleration_local * 1.0/smoothing +
mAccelerationJoint_local * (smoothing-1.0)/smoothing;
return smoothed_acceleration_local;
}
BOOL LLPhysicsMotionController::onUpdate(F32 time, U8* joint_mask)
{
// Skip if disabled globally.
if (!gSavedSettings.getBOOL("AvatarPhysics"))
{
return TRUE;
}
BOOL update_visuals = FALSE;
for (motion_vec_t::iterator iter = mMotions.begin();
iter != mMotions.end();
++iter)
{
LLPhysicsMotion *motion = (*iter);
update_visuals |= motion->onUpdate(time);
}
if (update_visuals)
mCharacter->updateVisualParams();
return TRUE;
}
// Return TRUE if character has to update visual params.
BOOL LLPhysicsMotion::onUpdate(F32 time)
{
// static FILE *mFileWrite = fopen("c:\\temp\\avatar_data.txt","w");
if (!mParamUser)
return FALSE;
if (!mLastTime)
{
mLastTime = time;
return FALSE;
}
////////////////////////////////////////////////////////////////////////////////
// Get all parameters and settings
//
const F32 time_delta = time - mLastTime;
if (time_delta > 3.0 || time_delta <= 0.01)
{
mLastTime = time;
return FALSE;
}
// Higher LOD is better. This controls the granularity
// and frequency of updates for the motions.
const F32 lod_factor = LLVOAvatar::sPhysicsLODFactor;
if (lod_factor == 0)
{
return TRUE;
}
LLJoint *joint = mJointState->getJoint();
const F32 behavior_mass = getParamValue("Mass");
const F32 behavior_gravity = getParamValue("Gravity");
const F32 behavior_spring = getParamValue("Spring");
const F32 behavior_gain = getParamValue("Gain");
const F32 behavior_damping = getParamValue("Damping");
const F32 behavior_maxspeed = getParamValue("MaxSpeed");
const F32 behavior_drag = getParamValue("Drag");
F32 position_current_local = mPosition_local; // Normalized [0,1] range
// Normalize the param position to be from [0,1].
// We have to use normalized values because there may be more than one driven param,
// and each of these driven params may have its own range.
// This means we'll do all our calculations in normalized [0,1] local coordinates.
F32 position_user_local = mParamUser->getWeight();
position_user_local = (position_user_local - mParamUser->getMinWeight()) / (mParamUser->getMaxWeight() - mParamUser->getMinWeight());
//
// End parameters and settings
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Calculate velocity and acceleration in parameter space.
//
const F32 velocity_joint_local = calculateVelocity_local(time_delta);
const F32 acceleration_joint_local = calculateAcceleration_local(velocity_joint_local, time_delta);
//
// End velocity and acceleration
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Calculate the total force
//
// Spring force is a restoring force towards the original user-set breast position.
// F = kx
const F32 spring_length = position_current_local - position_user_local;
const F32 force_spring = -spring_length * behavior_spring;
// Acceleration is the force that comes from the change in velocity of the torso.
// F = ma
const F32 force_accel = behavior_gain * (acceleration_joint_local * behavior_mass);
// Gravity always points downward in world space.
// F = mg
const LLVector3 gravity_world(0,0,1);
const F32 force_gravity = behavior_gain * (toLocal(gravity_world) * behavior_gravity * behavior_mass);
// Damping is a restoring force that opposes the current velocity.
// F = -kv
const F32 force_damping = -behavior_damping * mVelocity_local;
// Drag is a force imparted by velocity (intuitively it is similar to wind resistance)
// F = .5kv^2
const F32 force_drag = .5*behavior_drag*velocity_joint_local*velocity_joint_local*llsgn(velocity_joint_local);
const F32 force_net = (force_accel +
force_gravity +
force_spring +
force_damping +
force_drag);
//
// End total force
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Calculate new params
//
// Calculate the new acceleration based on the net force.
// a = F/m
const F32 acceleration_new_local = force_net / behavior_mass;
F32 velocity_new_local = mVelocity_local + acceleration_new_local;
velocity_new_local = llclamp(velocity_new_local,
-behavior_maxspeed*100.0f, behavior_maxspeed*100.0f);
// Temporary debugging setting to cause all avatars to move, for profiling purposes.
if (gSavedSettings.getBOOL("AvatarPhysicsTest"))
{
velocity_new_local = sin(time*4.0)*5.0;
}
// Calculate the new parameters, or remain unchanged if max speed is 0.
const F32 position_new_local = (behavior_maxspeed != 0) ?
(position_current_local + velocity_new_local*time_delta) :
position_user_local;
const F32 position_new_local_clamped = llclamp(position_new_local,
0.0f,
1.0f);
// Set the new param.
// If a specific param has been declared, then set that one.
// Otherwise, assume that the param is a driver param, and
// set the params that it drives.
if (mParamDriven)
{
setParamValue(mParamDriven,position_new_local_clamped);
}
else
{
LLDriverParam *driver_param = dynamic_cast<LLDriverParam *>(mParamUser);
llassert_always(driver_param);
if (driver_param)
{
for (LLDriverParam::entry_list_t::iterator iter = driver_param->mDriven.begin();
iter != driver_param->mDriven.end();
++iter)
{
LLDrivenEntry &entry = (*iter);
LLViewerVisualParam *driven_param = entry.mParam;
setParamValue(driven_param,position_new_local_clamped);
}
}
}
//
// End calculate new params
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Conditionally update the visual params
//
// Updating the visual params (i.e. what the user sees) is fairly expensive.
// So only update if the params have changed enough, and also take into account
// the graphics LOD settings.
BOOL update_visuals = FALSE;
// For non-self, if the avatar is small enough visually, then don't update.
const F32 area_for_max_settings = 0.0;
const F32 area_for_min_settings = 1400.0;
const F32 area_for_this_setting = area_for_max_settings + (area_for_min_settings-area_for_max_settings)*(1.0-lod_factor);
const F32 pixel_area = fsqrtf(mCharacter->getPixelArea());
const BOOL is_self = (dynamic_cast<LLVOAvatarSelf *>(mCharacter) != NULL);
if ((pixel_area > area_for_this_setting) || is_self)
{
const F32 position_diff_local = llabs(mPositionLastUpdate_local-position_new_local_clamped);
const F32 min_delta = (1.0f-lod_factor)*4.0f; // Magic number 2.0f, can change this if experimentally something works better.
if (llabs(position_diff_local) > min_delta)
{
update_visuals = TRUE;
mPositionLastUpdate_local = position_new_local;
}
}
//
// End update visual params
////////////////////////////////////////////////////////////////////////////////
mVelocityJoint_local = velocity_joint_local;
mVelocity_local = velocity_new_local;
mAccelerationJoint_local = acceleration_joint_local;
mPosition_local = position_new_local;
mPosition_world = joint->getWorldPosition();
mLastTime = time;
/*
// Write out debugging info into a spreadsheet.
if (mFileWrite != NULL && is_self)
{
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",
position_new_local,
velocity_new_local,
acceleration_new_local,
time_delta,
mPosition_world[0],
mPosition_world[1],
mPosition_world[2],
force_net,
force_spring,
force_accel,
force_damping,
force_drag,
spring_length,
velocity_joint_local,
acceleration_joint_local
);
}
*/
return update_visuals;
}
// Range of new_value_local is assumed to be [0 , 1] normalized.
void LLPhysicsMotion::setParamValue(LLViewerVisualParam *param,
F32 new_value_normalized)
{
const F32 value_min_local = param->getMinWeight();
const F32 value_max_local = param->getMaxWeight();
const F32 new_value_local = value_min_local + (value_max_local-value_min_local) * new_value_normalized;
mCharacter->setVisualParamWeight(param,
new_value_local,
FALSE);
}
|