Debian Codegraph @

source/unix/odesrc/joints/joint.cpp

raw
1 /*************************************************************************
2 * *
3 * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
4 * All rights reserved. Email: [email protected] Web: www.q12.org *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of EITHER: *
8 * (1) The GNU Lesser General Public License as published by the Free *
9 * Software Foundation; either version 2.1 of the License, or (at *
10 * your option) any later version. The text of the GNU Lesser *
11 * General Public License is included with this library in the *
12 * file LICENSE.TXT. *
13 * (2) The BSD-style license that is included with this library in *
14 * the file LICENSE-BSD.TXT. *
15 * *
16 * This library is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
19 * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
20 * *
21 *************************************************************************/
22
23 /*
24
25 design note: the general principle for giving a joint the option of connecting
26 to the static environment (i.e. the absolute frame) is to check the second
27 body (joint->node[1].body), and if it is zero then behave as if its body
28 transform is the identity.
29
30 */
31
32 #include <ode/ode.h>
33 #include <ode/rotation.h>
34 #include "config.h"
35 #include "matrix.h"
36 #include "odemath.h"
37 #include "joint.h"
38 #include "joint_internal.h"
39 #include "util.h"
40
41 extern void addObjectToList( dObject *obj, dObject **first );
42
43 dxJoint::dxJoint( dxWorld *w ) :
44 dObject( w )
45 {
46 //printf("constructing %p\n", this);
47 dIASSERT( w );
48 flags = 0;
49 node[0].joint = this;
50 node[0].body = 0;
51 node[0].next = 0;
52 node[1].joint = this;
53 node[1].body = 0;
54 node[1].next = 0;
55 dSetZero( lambda, 6 );
56
57 addObjectToList( this, ( dObject ** ) &w->firstjoint );
58
59 w->nj++;
60 feedback = 0;
61 }
62
63 dxJoint::~dxJoint()
64 { }
65
66
67 /*virtual */
68 void dxJoint::setRelativeValues()
69 {
70 // Do nothing
71 }
72
73 bool dxJoint::isEnabled() const
74 {
75 return ( (flags & dJOINT_DISABLED) == 0 &&
76 (node[0].body->invMass > 0 ||
77 (node[1].body && node[1].body->invMass > 0)) );
78 }
79
80
81 size_t dxJointGroup::exportJoints(dxJoint **jlist)
82 {
83 size_t i=0;
84 dxJoint *j = (dxJoint*) m_stack.rewind();
85 while (j != NULL) {
86 jlist[i++] = j;
87 j = (dxJoint*) (m_stack.next (j->size()));
88 }
89 return i;
90 }
91
92 void dxJointGroup::freeAll()
93 {
94 m_num = 0;
95 m_stack.freeAll();
96 }
97
98
99 //****************************************************************************
100 // externs
101
102 // extern "C" void dBodyAddTorque (dBodyID, dReal fx, dReal fy, dReal fz);
103 // extern "C" void dBodyAddForce (dBodyID, dReal fx, dReal fy, dReal fz);
104
105 //****************************************************************************
106 // utility
107
108 // set three "ball-and-socket" rows in the constraint equation, and the
109 // corresponding right hand side.
110
111 void setBall( dxJoint *joint, dReal fps, dReal erp,
112 int rowskip, dReal *J1, dReal *J2,
113 int pairskip, dReal *pairRhsCfm,
114 dVector3 anchor1, dVector3 anchor2 )
115 {
116 // anchor points in global coordinates with respect to body PORs.
117 dVector3 a1, a2;
118
119 // set Jacobian
120 J1[dxJoint::GI2_JLX] = 1;
121 J1[rowskip + dxJoint::GI2_JLY] = 1;
122 J1[2 * rowskip + dxJoint::GI2_JLZ] = 1;
123 dMultiply0_331( a1, joint->node[0].body->posr.R, anchor1 );
124 dSetCrossMatrixMinus( J1 + dxJoint::GI2__JA_MIN, a1, rowskip );
125
126 dxBody *b1 = joint->node[1].body;
127 if ( b1 )
128 {
129 J2[dxJoint::GI2_JLX] = -1;
130 J2[rowskip + dxJoint::GI2_JLY] = -1;
131 J2[2 * rowskip + dxJoint::GI2_JLZ] = -1;
132 dMultiply0_331( a2, b1->posr.R, anchor2 );
133 dSetCrossMatrixPlus( J2 + dxJoint::GI2__JA_MIN, a2, rowskip );
134 }
135
136 // set right hand side
137 dReal k = fps * erp;
138 dxBody *b0 = joint->node[0].body;
139 if ( b1 )
140 {
141 dReal *currRhsCfm = pairRhsCfm;
142 for ( int j = dSA__MIN; j != dSA__MAX; j++ )
143 {
144 currRhsCfm[dxJoint::GI2_RHS] = k * ( a2[j] + b1->posr.pos[j] - a1[j] - b0->posr.pos[j] );
145 currRhsCfm += pairskip;
146 }
147 }
148 else
149 {
150 dReal *currRhsCfm = pairRhsCfm;
151 for ( int j = dSA__MIN; j != dSA__MAX; j++ )
152 {
153 currRhsCfm[dxJoint::GI2_RHS] = k * ( anchor2[j] - a1[j] - b0->posr.pos[j] );
154 currRhsCfm += pairskip;
155 }
156 }
157 }
158
159
160 // this is like setBall(), except that `axis' is a unit length vector
161 // (in global coordinates) that should be used for the first jacobian
162 // position row (the other two row vectors will be derived from this).
163 // `erp1' is the erp value to use along the axis.
164
165 void setBall2( dxJoint *joint, dReal fps, dReal erp,
166 int rowskip, dReal *J1, dReal *J2,
167 int pairskip, dReal *pairRhsCfm,
168 dVector3 anchor1, dVector3 anchor2,
169 dVector3 axis, dReal erp1 )
170 {
171 // anchor points in global coordinates with respect to body PORs.
172 dVector3 a1, a2;
173
174 // get vectors normal to the axis. in setBall() axis,q1,q2 is [1 0 0],
175 // [0 1 0] and [0 0 1], which makes everything much easier.
176 dVector3 q1, q2;
177 dPlaneSpace( axis, q1, q2 );
178
179 // set Jacobian
180 dCopyVector3(J1 + dxJoint::GI2__JL_MIN, axis);
181 dCopyVector3(J1 + rowskip + dxJoint::GI2__JL_MIN, q1);
182 dCopyVector3(J1 + 2 * rowskip + dxJoint::GI2__JL_MIN, q2);
183 dMultiply0_331( a1, joint->node[0].body->posr.R, anchor1 );
184 dCalcVectorCross3( J1 + dxJoint::GI2__JA_MIN, a1, axis );
185 dCalcVectorCross3( J1 + rowskip + dxJoint::GI2__JA_MIN, a1, q1 );
186 dCalcVectorCross3( J1 + 2 * rowskip + dxJoint::GI2__JA_MIN, a1, q2 );
187
188 dxBody *b0 = joint->node[0].body;
189 dAddVectors3(a1, a1, b0->posr.pos);
190
191 // set right hand side - measure error along (axis,q1,q2)
192 dReal k1 = fps * erp1;
193 dReal k = fps * erp;
194
195 dxBody *b1 = joint->node[1].body;
196 if ( b1 )
197 {
198 dCopyNegatedVector3(J2 + dxJoint::GI2__JL_MIN, axis);
199 dCopyNegatedVector3(J2 + rowskip + dxJoint::GI2__JL_MIN, q1);
200 dCopyNegatedVector3(J2 + 2 * rowskip + dxJoint::GI2__JL_MIN, q2);
201 dMultiply0_331( a2, b1->posr.R, anchor2 );
202 dCalcVectorCross3( J2 + dxJoint::GI2__JA_MIN, axis, a2 ); //== dCalcVectorCross3( J2 + dxJoint::GI2__J2A_MIN, a2, axis ); dNegateVector3( J2 + dxJoint::GI2__J2A_MIN );
203 dCalcVectorCross3( J2 + rowskip + dxJoint::GI2__JA_MIN, q1, a2 ); //== dCalcVectorCross3( J2 + rowskip + dxJoint::GI2__J2A_MIN, a2, q1 ); dNegateVector3( J2 + rowskip + dxJoint::GI2__J2A_MIN );
204 dCalcVectorCross3( J2 + 2 * rowskip + dxJoint::GI2__JA_MIN, q2, a2 ); //== dCalcVectorCross3( J2 + 2 * rowskip + dxJoint::GI2__J2A_MIN, a2, q2 ); dNegateVector3( J2 + 2 * rowskip + dxJoint::GI2__J2A_MIN );
205
206 dAddVectors3(a2, a2, b1->posr.pos);
207
208 dVector3 a2_minus_a1;
209 dSubtractVectors3(a2_minus_a1, a2, a1);
210 pairRhsCfm[dxJoint::GI2_RHS] = k1 * dCalcVectorDot3( axis, a2_minus_a1 );
211 pairRhsCfm[pairskip + dxJoint::GI2_RHS] = k * dCalcVectorDot3( q1, a2_minus_a1 );
212 pairRhsCfm[2 * pairskip + dxJoint::GI2_RHS] = k * dCalcVectorDot3( q2, a2_minus_a1 );
213 }
214 else
215 {
216 dVector3 anchor2_minus_a1;
217 dSubtractVectors3(anchor2_minus_a1, anchor2, a1);
218 pairRhsCfm[dxJoint::GI2_RHS] = k1 * dCalcVectorDot3( axis, anchor2_minus_a1 );
219 pairRhsCfm[pairskip + dxJoint::GI2_RHS] = k * dCalcVectorDot3( q1, anchor2_minus_a1 );
220 pairRhsCfm[2 * pairskip + dxJoint::GI2_RHS] = k * dCalcVectorDot3( q2, anchor2_minus_a1 );
221 }
222 }
223
224
225 // set three orientation rows in the constraint equation, and the
226 // corresponding right hand side.
227
228 void setFixedOrientation( dxJoint *joint, dReal fps, dReal erp,
229 int rowskip, dReal *J1, dReal *J2,
230 int pairskip, dReal *pairRhsCfm,
231 dQuaternion qrel )
232 {
233 // 3 rows to make body rotations equal
234 J1[dxJoint::GI2_JAX] = 1;
235 J1[rowskip + dxJoint::GI2_JAY] = 1;
236 J1[2 * rowskip + dxJoint::GI2_JAZ] = 1;
237
238 dxBody *b1 = joint->node[1].body;
239 if ( b1 )
240 {
241 J2[dxJoint::GI2_JAX] = -1;
242 J2[rowskip + dxJoint::GI2_JAY] = -1;
243 J2[2 * rowskip + dxJoint::GI2_JAZ] = -1;
244 }
245
246 // compute the right hand side. the first three elements will result in
247 // relative angular velocity of the two bodies - this is set to bring them
248 // back into alignment. the correcting angular velocity is
249 // |angular_velocity| = angle/time = erp*theta / stepsize
250 // = (erp*fps) * theta
251 // angular_velocity = |angular_velocity| * u
252 // = (erp*fps) * theta * u
253 // where rotation along unit length axis u by theta brings body 2's frame
254 // to qrel with respect to body 1's frame. using a small angle approximation
255 // for sin(), this gives
256 // angular_velocity = (erp*fps) * 2 * v
257 // where the quaternion of the relative rotation between the two bodies is
258 // q = [cos(theta/2) sin(theta/2)*u] = [s v]
259
260 // get qerr = relative rotation (rotation error) between two bodies
261 dQuaternion qerr, e;
262 dxBody *b0 = joint->node[0].body;
263 if ( b1 )
264 {
265 dQuaternion qq;
266 dQMultiply1( qq, b0->q, b1->q );
267 dQMultiply2( qerr, qq, qrel );
268 }
269 else
270 {
271 dQMultiply3( qerr, b0->q, qrel );
272 }
273 if ( qerr[0] < 0 )
274 {
275 qerr[1] = -qerr[1]; // adjust sign of qerr to make theta small
276 qerr[2] = -qerr[2];
277 qerr[3] = -qerr[3];
278 }
279 dMultiply0_331( e, b0->posr.R, qerr + 1 ); // @@@ bad SIMD padding!
280 dReal k_mul_2 = fps * erp * REAL(2.0);
281 pairRhsCfm[dxJoint::GI2_RHS] = k_mul_2 * e[dSA_X];
282 pairRhsCfm[pairskip + dxJoint::GI2_RHS] = k_mul_2 * e[dSA_Y];
283 pairRhsCfm[2 * pairskip + dxJoint::GI2_RHS] = k_mul_2 * e[dSA_Z];
284 }
285
286
287 // compute anchor points relative to bodies
288
289 void setAnchors( dxJoint *j, dReal x, dReal y, dReal z,
290 dVector3 anchor1, dVector3 anchor2 )
291 {
292 dxBody *b0 = j->node[0].body;
293 if ( b0 )
294 {
295 dReal q[4];
296 q[0] = x - b0->posr.pos[0];
297 q[1] = y - b0->posr.pos[1];
298 q[2] = z - b0->posr.pos[2];
299 q[3] = 0;
300 dMultiply1_331( anchor1, b0->posr.R, q );
301
302 dxBody *b1 = j->node[1].body;
303 if ( b1 )
304 {
305 q[0] = x - b1->posr.pos[0];
306 q[1] = y - b1->posr.pos[1];
307 q[2] = z - b1->posr.pos[2];
308 q[3] = 0;
309 dMultiply1_331( anchor2, b1->posr.R, q );
310 }
311 else
312 {
313 anchor2[0] = x;
314 anchor2[1] = y;
315 anchor2[2] = z;
316 }
317 }
318 anchor1[3] = 0;
319 anchor2[3] = 0;
320 }
321
322
323 // compute axes relative to bodies. either axis1 or axis2 can be 0.
324
325 void setAxes( dxJoint *j, dReal x, dReal y, dReal z,
326 dVector3 axis1, dVector3 axis2 )
327 {
328 dxBody *b0 = j->node[0].body;
329 if ( b0 )
330 {
331 dReal q[4];
332 q[0] = x;
333 q[1] = y;
334 q[2] = z;
335 q[3] = 0;
336 dNormalize3( q );
337
338 if ( axis1 )
339 {
340 dMultiply1_331( axis1, b0->posr.R, q );
341 axis1[3] = 0;
342 }
343
344 if ( axis2 )
345 {
346 dxBody *b1 = j->node[1].body;
347 if ( b1 )
348 {
349 dMultiply1_331( axis2, b1->posr.R, q );
350 }
351 else
352 {
353 axis2[0] = x;
354 axis2[1] = y;
355 axis2[2] = z;
356 }
357 axis2[3] = 0;
358 }
359 }
360 }
361
362
363 void getAnchor( dxJoint *j, dVector3 result, dVector3 anchor1 )
364 {
365 dxBody *b0 = j->node[0].body;
366 if ( b0 )
367 {
368 dMultiply0_331( result, b0->posr.R, anchor1 );
369 result[0] += b0->posr.pos[0];
370 result[1] += b0->posr.pos[1];
371 result[2] += b0->posr.pos[2];
372 }
373 }
374
375
376 void getAnchor2( dxJoint *j, dVector3 result, dVector3 anchor2 )
377 {
378 dxBody *b1 = j->node[1].body;
379 if ( b1 )
380 {
381 dMultiply0_331( result, b1->posr.R, anchor2 );
382 result[0] += b1->posr.pos[0];
383 result[1] += b1->posr.pos[1];
384 result[2] += b1->posr.pos[2];
385 }
386 else
387 {
388 result[0] = anchor2[0];
389 result[1] = anchor2[1];
390 result[2] = anchor2[2];
391 }
392 }
393
394
395 void getAxis( dxJoint *j, dVector3 result, dVector3 axis1 )
396 {
397 dxBody *b0 = j->node[0].body;
398 if ( b0 )
399 {
400 dMultiply0_331( result, b0->posr.R, axis1 );
401 }
402 }
403
404
405 void getAxis2( dxJoint *j, dVector3 result, dVector3 axis2 )
406 {
407 dxBody *b1 = j->node[1].body;
408 if ( b1 )
409 {
410 dMultiply0_331( result, b1->posr.R, axis2 );
411 }
412 else
413 {
414 result[0] = axis2[0];
415 result[1] = axis2[1];
416 result[2] = axis2[2];
417 }
418 }
419
420
421 dReal getHingeAngleFromRelativeQuat( dQuaternion qrel, dVector3 axis )
422 {
423 // the angle between the two bodies is extracted from the quaternion that
424 // represents the relative rotation between them. recall that a quaternion
425 // q is:
426 // [s,v] = [ cos(theta/2) , sin(theta/2) * u ]
427 // where s is a scalar and v is a 3-vector. u is a unit length axis and
428 // theta is a rotation along that axis. we can get theta/2 by:
429 // theta/2 = atan2 ( sin(theta/2) , cos(theta/2) )
430 // but we can't get sin(theta/2) directly, only its absolute value, i.e.:
431 // |v| = |sin(theta/2)| * |u|
432 // = |sin(theta/2)|
433 // using this value will have a strange effect. recall that there are two
434 // quaternion representations of a given rotation, q and -q. typically as
435 // a body rotates along the axis it will go through a complete cycle using
436 // one representation and then the next cycle will use the other
437 // representation. this corresponds to u pointing in the direction of the
438 // hinge axis and then in the opposite direction. the result is that theta
439 // will appear to go "backwards" every other cycle. here is a fix: if u
440 // points "away" from the direction of the hinge (motor) axis (i.e. more
441 // than 90 degrees) then use -q instead of q. this represents the same
442 // rotation, but results in the cos(theta/2) value being sign inverted.
443
444 // extract the angle from the quaternion. cost2 = cos(theta/2),
445 // sint2 = |sin(theta/2)|
446 dReal cost2 = qrel[0];
447 dReal sint2 = dSqrt( qrel[1] * qrel[1] + qrel[2] * qrel[2] + qrel[3] * qrel[3] );
448 dReal theta = ( dCalcVectorDot3( qrel + 1, axis ) >= 0 ) ? // @@@ padding assumptions
449 ( 2 * dAtan2( sint2, cost2 ) ) : // if u points in direction of axis
450 ( 2 * dAtan2( sint2, -cost2 ) ); // if u points in opposite direction
451
452 // the angle we get will be between 0..2*pi, but we want to return angles
453 // between -pi..pi
454 if ( theta > M_PI ) theta -= ( dReal )( 2 * M_PI );
455
456 // the angle we've just extracted has the wrong sign
457 theta = -theta;
458
459 return theta;
460 }
461
462
463 // given two bodies (body1,body2), the hinge axis that they are connected by
464 // w.r.t. body1 (axis), and the initial relative orientation between them
465 // (q_initial), return the relative rotation angle. the initial relative
466 // orientation corresponds to an angle of zero. if body2 is 0 then measure the
467 // angle between body1 and the static frame.
468 //
469 // this will not return the correct angle if the bodies rotate along any axis
470 // other than the given hinge axis.
471
472 dReal getHingeAngle( dxBody *body1, dxBody *body2, dVector3 axis,
473 dQuaternion q_initial )
474 {
475 // get qrel = relative rotation between the two bodies
476 dQuaternion qrel;
477 if ( body2 )
478 {
479 dQuaternion qq;
480 dQMultiply1( qq, body1->q, body2->q );
481 dQMultiply2( qrel, qq, q_initial );
482 }
483 else
484 {
485 // pretend body2->q is the identity
486 dQMultiply3( qrel, body1->q, q_initial );
487 }
488
489 return getHingeAngleFromRelativeQuat( qrel, axis );
490 }
491
492 //****************************************************************************
493 // dxJointLimitMotor
494
495 void dxJointLimitMotor::init( dxWorld *world )
496 {
497 vel = 0;
498 fmax = 0;
499 lostop = -dInfinity;
500 histop = dInfinity;
501 fudge_factor = 1;
502 normal_cfm = world->global_cfm;
503 stop_erp = world->global_erp;
504 stop_cfm = world->global_cfm;
505 bounce = 0;
506 limit = 0;
507 limit_err = 0;
508 }
509
510
511 void dxJointLimitMotor::set( int num, dReal value )
512 {
513 switch ( num )
514 {
515 case dParamLoStop:
516 lostop = value;
517 break;
518 case dParamHiStop:
519 histop = value;
520 break;
521 case dParamVel:
522 vel = value;
523 break;
524 case dParamFMax:
525 if ( value >= 0 ) fmax = value;
526 break;
527 case dParamFudgeFactor:
528 if ( value >= 0 && value <= 1 ) fudge_factor = value;
529 break;
530 case dParamBounce:
531 bounce = value;
532 break;
533 case dParamCFM:
534 normal_cfm = value;
535 break;
536 case dParamStopERP:
537 stop_erp = value;
538 break;
539 case dParamStopCFM:
540 stop_cfm = value;
541 break;
542 }
543 }
544
545
546 dReal dxJointLimitMotor::get( int num ) const
547 {
548 switch ( num )
549 {
550 case dParamLoStop:
551 return lostop;
552 case dParamHiStop:
553 return histop;
554 case dParamVel:
555 return vel;
556 case dParamFMax:
557 return fmax;
558 case dParamFudgeFactor:
559 return fudge_factor;
560 case dParamBounce:
561 return bounce;
562 case dParamCFM:
563 return normal_cfm;
564 case dParamStopERP:
565 return stop_erp;
566 case dParamStopCFM:
567 return stop_cfm;
568 default:
569 return 0;
570 }
571 }
572
573
574 bool dxJointLimitMotor::testRotationalLimit( dReal angle )
575 {
576 if ( angle <= lostop )
577 {
578 limit = 1;
579 limit_err = angle - lostop;
580 return true;
581 }
582 else if ( angle >= histop )
583 {
584 limit = 2;
585 limit_err = angle - histop;
586 return true;
587 }
588 else
589 {
590 limit = 0;
591 return false;
592 }
593 }
594
595
596 bool dxJointLimitMotor::addLimot( dxJoint *joint,
597 dReal fps, dReal *J1, dReal *J2, dReal *pairRhsCfm, dReal *pairLoHi,
598 const dVector3 ax1, int rotational )
599 {
600 // if the joint is powered, or has joint limits, add in the extra row
601 int powered = fmax > 0;
602 if ( powered || limit )
603 {
604 dReal *J1Used = rotational ? J1 + GI2__JA_MIN : J1 + GI2__JL_MIN;
605 dReal *J2Used = rotational ? J2 + GI2__JA_MIN : J2 + GI2__JL_MIN;
606
607 dCopyVector3(J1Used, ax1);
608
609 dxBody *b1 = joint->node[1].body;
610 if ( b1 )
611 {
612 dCopyNegatedVector3(J2Used, ax1);
613 }
614
615 // linear limot torque decoupling step:
616 //
617 // if this is a linear limot (e.g. from a slider), we have to be careful
618 // that the linear constraint forces (+/- ax1) applied to the two bodies
619 // do not create a torque couple. in other words, the points that the
620 // constraint force is applied at must lie along the same ax1 axis.
621 // a torque couple will result in powered or limited slider-jointed free
622 // bodies from gaining angular momentum.
623 // the solution used here is to apply the constraint forces at the point
624 // halfway between the body centers. there is no penalty (other than an
625 // extra tiny bit of computation) in doing this adjustment. note that we
626 // only need to do this if the constraint connects two bodies.
627
628 dVector3 ltd = {0,0,0}; // Linear Torque Decoupling vector (a torque)
629 if ( !rotational && b1 )
630 {
631 dxBody *b0 = joint->node[0].body;
632 dVector3 c;
633 c[0] = REAL( 0.5 ) * ( b1->posr.pos[0] - b0->posr.pos[0] );
634 c[1] = REAL( 0.5 ) * ( b1->posr.pos[1] - b0->posr.pos[1] );
635 c[2] = REAL( 0.5 ) * ( b1->posr.pos[2] - b0->posr.pos[2] );
636 dCalcVectorCross3( ltd, c, ax1 );
637 dCopyVector3(J1 + dxJoint::GI2__JA_MIN, ltd);
638 dCopyVector3(J2 + dxJoint::GI2__JA_MIN, ltd);
639 }
640
641 // if we're limited low and high simultaneously, the joint motor is
642 // ineffective
643 if ( limit && ( lostop == histop ) ) powered = 0;
644
645 if ( powered )
646 {
647 pairRhsCfm[GI2_CFM] = normal_cfm;
648 if ( ! limit )
649 {
650 pairRhsCfm[GI2_RHS] = vel;
651 pairLoHi[GI2_LO] = -fmax;
652 pairLoHi[GI2_HI] = fmax;
653 }
654 else
655 {
656 // the joint is at a limit, AND is being powered. if the joint is
657 // being powered into the limit then we apply the maximum motor force
658 // in that direction, because the motor is working against the
659 // immovable limit. if the joint is being powered away from the limit
660 // then we have problems because actually we need *two* lcp
661 // constraints to handle this case. so we fake it and apply some
662 // fraction of the maximum force. the fraction to use can be set as
663 // a fudge factor.
664
665 dReal fm = fmax;
666 if (( vel > 0 ) || ( vel == 0 && limit == 2 ) ) fm = -fm;
667
668 // if we're powering away from the limit, apply the fudge factor
669 if (( limit == 1 && vel > 0 ) || ( limit == 2 && vel < 0 ) ) fm *= fudge_factor;
670
671
672 dReal fm_ax1_0 = fm*ax1[0], fm_ax1_1 = fm*ax1[1], fm_ax1_2 = fm*ax1[2];
673
674 dxBody *b0 = joint->node[0].body;
675 dxWorldProcessContext *world_process_context = b0->world->UnsafeGetWorldProcessingContext();
676
677 world_process_context->LockForAddLimotSerialization();
678
679 if ( rotational )
680 {
681 dxBody *b1 = joint->node[1].body;
682 if ( b1 != NULL )
683 {
684 dBodyAddTorque( b1, fm_ax1_0, fm_ax1_1, fm_ax1_2 );
685 }
686
687 dBodyAddTorque( b0, -fm_ax1_0, -fm_ax1_1, -fm_ax1_2 );
688 }
689 else
690 {
691 dxBody *b1 = joint->node[1].body;
692 if ( b1 != NULL )
693 {
694 // linear limot torque decoupling step: refer to above discussion
695 dReal neg_fm_ltd_0 = -fm*ltd[0], neg_fm_ltd_1 = -fm*ltd[1], neg_fm_ltd_2 = -fm*ltd[2];
696 dBodyAddTorque( b0, neg_fm_ltd_0, neg_fm_ltd_1, neg_fm_ltd_2 );
697 dBodyAddTorque( b1, neg_fm_ltd_0, neg_fm_ltd_1, neg_fm_ltd_2 );
698
699 dBodyAddForce( b1, fm_ax1_0, fm_ax1_1, fm_ax1_2 );
700 }
701
702 dBodyAddForce( b0, -fm_ax1_0, -fm_ax1_1, -fm_ax1_2 );
703 }
704
705 world_process_context->UnlockForAddLimotSerialization();
706 }
707 }
708
709 if ( limit )
710 {
711 dReal k = fps * stop_erp;
712 pairRhsCfm[GI2_RHS] = -k * limit_err;
713 pairRhsCfm[GI2_CFM] = stop_cfm;
714
715 if ( lostop == histop )
716 {
717 // limited low and high simultaneously
718 pairLoHi[GI2_LO] = -dInfinity;
719 pairLoHi[GI2_HI] = dInfinity;
720 }
721 else
722 {
723 if ( limit == 1 )
724 {
725 // low limit
726 pairLoHi[GI2_LO] = 0;
727 pairLoHi[GI2_HI] = dInfinity;
728 }
729 else
730 {
731 // high limit
732 pairLoHi[GI2_LO] = -dInfinity;
733 pairLoHi[GI2_HI] = 0;
734 }
735
736 // deal with bounce
737 if ( bounce > 0 )
738 {
739 // calculate joint velocity
740 dReal vel;
741 if ( rotational )
742 {
743 vel = dCalcVectorDot3( joint->node[0].body->avel, ax1 );
744 if ( joint->node[1].body )
745 vel -= dCalcVectorDot3( joint->node[1].body->avel, ax1 );
746 }
747 else
748 {
749 vel = dCalcVectorDot3( joint->node[0].body->lvel, ax1 );
750 if ( joint->node[1].body )
751 vel -= dCalcVectorDot3( joint->node[1].body->lvel, ax1 );
752 }
753
754 // only apply bounce if the velocity is incoming, and if the
755 // resulting c[] exceeds what we already have.
756 if ( limit == 1 )
757 {
758 // low limit
759 if ( vel < 0 )
760 {
761 dReal newc = -bounce * vel;
762 if ( newc > pairRhsCfm[GI2_RHS] ) pairRhsCfm[GI2_RHS] = newc;
763 }
764 }
765 else
766 {
767 // high limit - all those computations are reversed
768 if ( vel > 0 )
769 {
770 dReal newc = -bounce * vel;
771 if ( newc < pairRhsCfm[GI2_RHS] ) pairRhsCfm[GI2_RHS] = newc;
772 }
773 }
774 }
775 }
776 }
777 return true;
778 }
779 return false;
780 }
781
782 /**
783 This function generalizes the "linear limot torque decoupling"
784 in addLimot to use anchor points provided by the caller.
785
786 This makes it so that the appropriate torques are applied to
787 a body when it's being linearly motored or limited using anchor points
788 that aren't at the center of mass.
789
790 pt1 and pt2 are centered in body coordinates but use global directions.
791 I.e., they are conveniently found within joint code with:
792 getAxis(joint,pt1,anchor1);
793 getAxis2(joint,pt2,anchor2);
794 */
795 bool dxJointLimitMotor::addTwoPointLimot( dxJoint *joint, dReal fps,
796 dReal *J1, dReal *J2, dReal *pairRhsCfm, dReal *pairLoHi,
797 const dVector3 ax1, const dVector3 pt1, const dVector3 pt2 )
798 {
799 // if the joint is powered, or has joint limits, add in the extra row
800 int powered = fmax > 0;
801 if ( powered || limit )
802 {
803 // Set the linear portion
804 dCopyVector3(J1 + GI2__JL_MIN, ax1);
805 // Set the angular portion (to move the linear constraint
806 // away from the center of mass).
807 dCalcVectorCross3(J1 + GI2__JA_MIN, pt1, ax1);
808 // Set the constraints for the second body
809 if ( joint->node[1].body ) {
810 dCopyNegatedVector3(J2 + GI2__JL_MIN, ax1);
811 dCalcVectorCross3(J2 + GI2__JA_MIN, pt2, J2 + GI2__JL_MIN);
812 }
813
814 // if we're limited low and high simultaneously, the joint motor is
815 // ineffective
816 if ( limit && ( lostop == histop ) ) powered = 0;
817
818 if ( powered )
819 {
820 pairRhsCfm[GI2_CFM] = normal_cfm;
821 if ( ! limit )
822 {
823 pairRhsCfm[GI2_RHS] = vel;
824 pairLoHi[GI2_LO] = -fmax;
825 pairLoHi[GI2_HI] = fmax;
826 }
827 else
828 {
829 // the joint is at a limit, AND is being powered. if the joint is
830 // being powered into the limit then we apply the maximum motor force
831 // in that direction, because the motor is working against the
832 // immovable limit. if the joint is being powered away from the limit
833 // then we have problems because actually we need *two* lcp
834 // constraints to handle this case. so we fake it and apply some
835 // fraction of the maximum force. the fraction to use can be set as
836 // a fudge factor.
837
838 dReal fm = fmax;
839 if (( vel > 0 ) || ( vel == 0 && limit == 2 ) ) fm = -fm;
840
841 // if we're powering away from the limit, apply the fudge factor
842 if (( limit == 1 && vel > 0 ) || ( limit == 2 && vel < 0 ) ) fm *= fudge_factor;
843
844
845 const dReal* tAx1 = J1 + GI2__JA_MIN;
846 dBodyAddForce( joint->node[0].body, -fm*ax1[dSA_X], -fm*ax1[dSA_Y], -fm*ax1[dSA_Z] );
847 dBodyAddTorque( joint->node[0].body, -fm*tAx1[dSA_X], -fm*tAx1[dSA_Y], -fm*tAx1[dSA_Z] );
848
849 if ( joint->node[1].body )
850 {
851 const dReal* tAx2 = J2 + GI2__JA_MIN;
852 dBodyAddForce( joint->node[1].body, fm*ax1[dSA_X], fm*ax1[dSA_Y], fm*ax1[dSA_Z] );
853 dBodyAddTorque( joint->node[1].body, -fm*tAx2[dSA_X], -fm*tAx2[dSA_Y], -fm*tAx2[dSA_Z] );
854 }
855
856 }
857 }
858
859 if ( limit )
860 {
861 dReal k = fps * stop_erp;
862 pairRhsCfm[GI2_RHS] = -k * limit_err;
863 pairRhsCfm[GI2_CFM] = stop_cfm;
864
865 if ( lostop == histop )
866 {
867 // limited low and high simultaneously
868 pairLoHi[GI2_LO] = -dInfinity;
869 pairLoHi[GI2_HI] = dInfinity;
870 }
871 else
872 {
873 if ( limit == 1 )
874 {
875 // low limit
876 pairLoHi[GI2_LO] = 0;
877 pairLoHi[GI2_HI] = dInfinity;
878 }
879 else
880 {
881 // high limit
882 pairLoHi[GI2_LO] = -dInfinity;
883 pairLoHi[GI2_HI] = 0;
884 }
885
886 // deal with bounce
887 if ( bounce > 0 )
888 {
889 // calculate relative velocity of the two anchor points
890 dReal vel =
891 dCalcVectorDot3( joint->node[0].body->lvel, J1 + GI2__JL_MIN ) +
892 dCalcVectorDot3( joint->node[0].body->avel, J1 + GI2__JA_MIN );
893 if (joint->node[1].body) {
894 vel +=
895 dCalcVectorDot3( joint->node[1].body->lvel, J2 + GI2__JL_MIN ) +
896 dCalcVectorDot3( joint->node[1].body->avel, J2 + GI2__JA_MIN );
897 }
898
899 // only apply bounce if the velocity is incoming, and if the
900 // resulting c[] exceeds what we already have.
901 if ( limit == 1 )
902 {
903 // low limit
904 if ( vel < 0 )
905 {
906 dReal newc = -bounce * vel;
907 if ( newc > pairRhsCfm[GI2_RHS] ) pairRhsCfm[GI2_RHS] = newc;
908 }
909 }
910 else
911 {
912 // high limit - all those computations are reversed
913 if ( vel > 0 )
914 {
915 dReal newc = -bounce * vel;
916 if ( newc < pairRhsCfm[GI2_RHS] ) pairRhsCfm[GI2_RHS] = newc;
917 }
918 }
919 }
920 }
921 }
922 return true;
923 }
924 return false;
925 }
926
927
928 // Local Variables:
929 // mode:c++
930 // c-basic-offset:4
931 // End: