Debian Codegraph @

source/unix/odesrc/joints/universal.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 #include <ode/odeconfig.h>
25 #include "config.h"
26 #include "universal.h"
27 #include "joint_internal.h"
28
29
30
31 //****************************************************************************
32 // universal
33
34 // I just realized that the universal joint is equivalent to a hinge 2 joint with
35 // perfectly stiff suspension. By comparing the hinge 2 implementation to
36 // the universal implementation, you may be able to improve this
37 // implementation (or, less likely, the hinge2 implementation).
38
39 dxJointUniversal::dxJointUniversal( dxWorld *w ) :
40 dxJoint( w )
41 {
42 dSetZero( anchor1, 4 );
43 dSetZero( anchor2, 4 );
44 dSetZero( axis1, 4 );
45 axis1[0] = 1;
46 dSetZero( axis2, 4 );
47 axis2[1] = 1;
48 dSetZero( qrel1, 4 );
49 dSetZero( qrel2, 4 );
50 limot1.init( world );
51 limot2.init( world );
52 }
53
54
55 void
56 dxJointUniversal::getAxes( dVector3 ax1, dVector3 ax2 )
57 {
58 // This says "ax1 = joint->node[0].body->posr.R * joint->axis1"
59 dMultiply0_331( ax1, node[0].body->posr.R, axis1 );
60
61 if ( node[1].body )
62 {
63 dMultiply0_331( ax2, node[1].body->posr.R, axis2 );
64 }
65 else
66 {
67 ax2[0] = axis2[0];
68 ax2[1] = axis2[1];
69 ax2[2] = axis2[2];
70 }
71 }
72
73 void
74 dxJointUniversal::getAngles( dReal *angle1, dReal *angle2 )
75 {
76 if ( node[0].body )
77 {
78 // length 1 joint axis in global coordinates, from each body
79 dVector3 ax1, ax2;
80 dMatrix3 R;
81 dQuaternion qcross, qq, qrel;
82
83 getAxes( ax1, ax2 );
84
85 // It should be possible to get both angles without explicitly
86 // constructing the rotation matrix of the cross. Basically,
87 // orientation of the cross about axis1 comes from body 2,
88 // about axis 2 comes from body 1, and the perpendicular
89 // axis can come from the two bodies somehow. (We don't really
90 // want to assume it's 90 degrees, because in general the
91 // constraints won't be perfectly satisfied, or even very well
92 // satisfied.)
93 //
94 // However, we'd need a version of getHingeAngleFromRElativeQuat()
95 // that CAN handle when its relative quat is rotated along a direction
96 // other than the given axis. What I have here works,
97 // although it's probably much slower than need be.
98
99 dRFrom2Axes( R, ax1[0], ax1[1], ax1[2], ax2[0], ax2[1], ax2[2] );
100
101 dRtoQ( R, qcross );
102
103
104 // This code is essentialy the same as getHingeAngle(), see the comments
105 // there for details.
106
107 // get qrel = relative rotation between node[0] and the cross
108 dQMultiply1( qq, node[0].body->q, qcross );
109 dQMultiply2( qrel, qq, qrel1 );
110
111 *angle1 = getHingeAngleFromRelativeQuat( qrel, axis1 );
112
113 // This is equivalent to
114 // dRFrom2Axes(R, ax2[0], ax2[1], ax2[2], ax1[0], ax1[1], ax1[2]);
115 // You see that the R is constructed from the same 2 axis as for angle1
116 // but the first and second axis are swapped.
117 // So we can take the first R and rapply a rotation to it.
118 // The rotation is around the axis between the 2 axes (ax1 and ax2).
119 // We do a rotation of 180deg.
120
121 dQuaternion qcross2;
122 // Find the vector between ax1 and ax2 (i.e. in the middle)
123 // We need to turn around this vector by 180deg
124
125 // The 2 axes should be normalize so to find the vector between the 2.
126 // Add and devide by 2 then normalize or simply normalize
127 // ax2
128 // ^
129 // |
130 // |
131 /// *------------> ax1
132 // We want the vector a 45deg
133 //
134 // N.B. We don't need to normalize the ax1 and ax2 since there are
135 // normalized when we set them.
136
137 // We set the quaternion q = [cos(theta), dir*sin(theta)] = [w, x, y, Z]
138 qrel[0] = 0; // equivalent to cos(Pi/2)
139 qrel[1] = ax1[0] + ax2[0]; // equivalent to x*sin(Pi/2); since sin(Pi/2) = 1
140 qrel[2] = ax1[1] + ax2[1];
141 qrel[3] = ax1[2] + ax2[2];
142
143 dReal l = dRecip( sqrt( qrel[1] * qrel[1] + qrel[2] * qrel[2] + qrel[3] * qrel[3] ) );
144 qrel[1] *= l;
145 qrel[2] *= l;
146 qrel[3] *= l;
147
148 dQMultiply0( qcross2, qrel, qcross );
149
150 if ( node[1].body )
151 {
152 dQMultiply1( qq, node[1].body->q, qcross2 );
153 dQMultiply2( qrel, qq, qrel2 );
154 }
155 else
156 {
157 // pretend joint->node[1].body->q is the identity
158 dQMultiply2( qrel, qcross2, qrel2 );
159 }
160
161 *angle2 = - getHingeAngleFromRelativeQuat( qrel, axis2 );
162 }
163 else
164 {
165 *angle1 = 0;
166 *angle2 = 0;
167 }
168 }
169
170 dReal
171 dxJointUniversal::getAngle1()
172 {
173 if ( node[0].body )
174 {
175 // length 1 joint axis in global coordinates, from each body
176 dVector3 ax1, ax2;
177 dMatrix3 R;
178 dQuaternion qcross, qq, qrel;
179
180 getAxes( ax1, ax2 );
181
182 // It should be possible to get both angles without explicitly
183 // constructing the rotation matrix of the cross. Basically,
184 // orientation of the cross about axis1 comes from body 2,
185 // about axis 2 comes from body 1, and the perpendicular
186 // axis can come from the two bodies somehow. (We don't really
187 // want to assume it's 90 degrees, because in general the
188 // constraints won't be perfectly satisfied, or even very well
189 // satisfied.)
190 //
191 // However, we'd need a version of getHingeAngleFromRElativeQuat()
192 // that CAN handle when its relative quat is rotated along a direction
193 // other than the given axis. What I have here works,
194 // although it's probably much slower than need be.
195
196 dRFrom2Axes( R, ax1[0], ax1[1], ax1[2], ax2[0], ax2[1], ax2[2] );
197 dRtoQ( R, qcross );
198
199 // This code is essential the same as getHingeAngle(), see the comments
200 // there for details.
201
202 // get qrel = relative rotation between node[0] and the cross
203 dQMultiply1( qq, node[0].body->q, qcross );
204 dQMultiply2( qrel, qq, qrel1 );
205
206 return getHingeAngleFromRelativeQuat( qrel, axis1 );
207 }
208 return 0;
209 }
210
211
212 dReal
213 dxJointUniversal::getAngle2()
214 {
215 if ( node[0].body )
216 {
217 // length 1 joint axis in global coordinates, from each body
218 dVector3 ax1, ax2;
219 dMatrix3 R;
220 dQuaternion qcross, qq, qrel;
221
222 getAxes( ax1, ax2 );
223
224 // It should be possible to get both angles without explicitly
225 // constructing the rotation matrix of the cross. Basically,
226 // orientation of the cross about axis1 comes from body 2,
227 // about axis 2 comes from body 1, and the perpendicular
228 // axis can come from the two bodies somehow. (We don't really
229 // want to assume it's 90 degrees, because in general the
230 // constraints won't be perfectly satisfied, or even very well
231 // satisfied.)
232 //
233 // However, we'd need a version of getHingeAngleFromRElativeQuat()
234 // that CAN handle when its relative quat is rotated along a direction
235 // other than the given axis. What I have here works,
236 // although it's probably much slower than need be.
237
238 dRFrom2Axes( R, ax2[0], ax2[1], ax2[2], ax1[0], ax1[1], ax1[2] );
239 dRtoQ( R, qcross );
240
241 if ( node[1].body )
242 {
243 dQMultiply1( qq, node[1].body->q, qcross );
244 dQMultiply2( qrel, qq, qrel2 );
245 }
246 else
247 {
248 // pretend joint->node[1].body->q is the identity
249 dQMultiply2( qrel, qcross, qrel2 );
250 }
251
252 return - getHingeAngleFromRelativeQuat( qrel, axis2 );
253 }
254 return 0;
255 }
256
257
258 void
259 dxJointUniversal::getSureMaxInfo( SureMaxInfo* info )
260 {
261 info->max_m = 6;
262 }
263
264
265 void
266 dxJointUniversal::getInfo1( dxJoint::Info1 *info )
267 {
268 info->nub = 4;
269 info->m = 4;
270
271 bool limiting1 = ( limot1.lostop >= -M_PI || limot1.histop <= M_PI ) &&
272 limot1.lostop <= limot1.histop;
273 bool limiting2 = ( limot2.lostop >= -M_PI || limot2.histop <= M_PI ) &&
274 limot2.lostop <= limot2.histop;
275
276 // We need to call testRotationLimit() even if we're motored, since it
277 // records the result.
278 limot1.limit = 0;
279 limot2.limit = 0;
280
281 if ( limiting1 || limiting2 )
282 {
283 dReal angle1, angle2;
284 getAngles( &angle1, &angle2 );
285 if ( limiting1 )
286 limot1.testRotationalLimit( angle1 );
287 if ( limiting2 )
288 limot2.testRotationalLimit( angle2 );
289 }
290
291 if ( limot1.limit || limot1.fmax > 0 ) info->m++;
292 if ( limot2.limit || limot2.fmax > 0 ) info->m++;
293 }
294
295
296 void
297 dxJointUniversal::getInfo2( dReal worldFPS, dReal worldERP,
298 int rowskip, dReal *J1, dReal *J2,
299 int pairskip, dReal *pairRhsCfm, dReal *pairLoHi,
300 int *findex )
301 {
302 // set the three ball-and-socket rows
303 setBall( this, worldFPS, worldERP, rowskip, J1, J2, pairskip, pairRhsCfm, anchor1, anchor2 );
304
305 // set the universal joint row. the angular velocity about an axis
306 // perpendicular to both joint axes should be equal. thus the constraint
307 // equation is
308 // p*w1 - p*w2 = 0
309 // where p is a vector normal to both joint axes, and w1 and w2
310 // are the angular velocity vectors of the two bodies.
311
312 // length 1 joint axis in global coordinates, from each body
313 dVector3 ax1, ax2;
314 // length 1 vector perpendicular to ax1 and ax2. Neither body can rotate
315 // about this.
316 dVector3 p;
317
318 // Since axis1 and axis2 may not be perpendicular
319 // we find a axis2_tmp which is really perpendicular to axis1
320 // and in the plane of axis1 and axis2
321 getAxes( ax1, ax2 );
322
323 dReal k = dCalcVectorDot3( ax1, ax2 );
324
325 dVector3 ax2_temp;
326 dAddVectorScaledVector3(ax2_temp, ax2, ax1, -k);
327 dCalcVectorCross3( p, ax1, ax2_temp );
328 dNormalize3( p );
329
330 int currRowSkip = 3 * rowskip;
331 {
332 dCopyVector3( J1 + currRowSkip + GI2__JA_MIN, p);
333
334 if ( node[1].body )
335 {
336 dCopyNegatedVector3( J2 + currRowSkip + GI2__JA_MIN, p);
337 }
338 }
339
340 // compute the right hand side of the constraint equation. set relative
341 // body velocities along p to bring the axes back to perpendicular.
342 // If ax1, ax2 are unit length joint axes as computed from body1 and
343 // body2, we need to rotate both bodies along the axis p. If theta
344 // is the angle between ax1 and ax2, we need an angular velocity
345 // along p to cover the angle erp * (theta - Pi/2) in one step:
346 //
347 // |angular_velocity| = angle/time = erp*(theta - Pi/2) / stepsize
348 // = (erp*fps) * (theta - Pi/2)
349 //
350 // if theta is close to Pi/2,
351 // theta - Pi/2 ~= cos(theta), so
352 // |angular_velocity| ~= (erp*fps) * (ax1 dot ax2)
353
354 int currPairSkip = 3 * pairskip;
355 {
356 pairRhsCfm[currPairSkip + GI2_RHS] = worldFPS * worldERP * (-k);
357 }
358
359 currRowSkip += rowskip; currPairSkip += pairskip;
360
361 // if the first angle is powered, or has joint limits, add in the stuff
362 if (limot1.addLimot( this, worldFPS, J1 + currRowSkip, J2 + currRowSkip, pairRhsCfm + currPairSkip, pairLoHi + currPairSkip, ax1, 1 ))
363 {
364 currRowSkip += rowskip; currPairSkip += pairskip;
365 }
366
367 // if the second angle is powered, or has joint limits, add in more stuff
368 limot2.addLimot( this, worldFPS, J1 + currRowSkip, J2 + currRowSkip, pairRhsCfm + currPairSkip, pairLoHi + currPairSkip, ax2, 1 );
369 }
370
371
372 void
373 dxJointUniversal::computeInitialRelativeRotations()
374 {
375 if ( node[0].body )
376 {
377 dVector3 ax1, ax2;
378 dMatrix3 R;
379 dQuaternion qcross;
380
381 getAxes( ax1, ax2 );
382
383 // Axis 1.
384 dRFrom2Axes( R, ax1[0], ax1[1], ax1[2], ax2[0], ax2[1], ax2[2] );
385 dRtoQ( R, qcross );
386 dQMultiply1( qrel1, node[0].body->q, qcross );
387
388 // Axis 2.
389 dRFrom2Axes( R, ax2[0], ax2[1], ax2[2], ax1[0], ax1[1], ax1[2] );
390 dRtoQ( R, qcross );
391 if ( node[1].body )
392 {
393 dQMultiply1( qrel2, node[1].body->q, qcross );
394 }
395 else
396 {
397 // set joint->qrel to qcross
398 for ( int i = 0; i < 4; i++ ) qrel2[i] = qcross[i];
399 }
400 }
401 }
402
403
404 void dJointSetUniversalAnchor( dJointID j, dReal x, dReal y, dReal z )
405 {
406 dxJointUniversal* joint = ( dxJointUniversal* )j;
407 dUASSERT( joint, "bad joint argument" );
408 checktype( joint, Universal );
409 setAnchors( joint, x, y, z, joint->anchor1, joint->anchor2 );
410 joint->computeInitialRelativeRotations();
411 }
412
413
414 void dJointSetUniversalAxis1( dJointID j, dReal x, dReal y, dReal z )
415 {
416 dxJointUniversal* joint = ( dxJointUniversal* )j;
417 dUASSERT( joint, "bad joint argument" );
418 checktype( joint, Universal );
419 if ( joint->flags & dJOINT_REVERSE )
420 setAxes( joint, x, y, z, NULL, joint->axis2 );
421 else
422 setAxes( joint, x, y, z, joint->axis1, NULL );
423 joint->computeInitialRelativeRotations();
424 }
425
426 void dJointSetUniversalAxis1Offset( dJointID j, dReal x, dReal y, dReal z,
427 dReal offset1, dReal offset2 )
428 {
429 dxJointUniversal* joint = ( dxJointUniversal* )j;
430 dUASSERT( joint, "bad joint argument" );
431 checktype( joint, Universal );
432 if ( joint->flags & dJOINT_REVERSE )
433 {
434 setAxes( joint, x, y, z, NULL, joint->axis2 );
435 offset1 = -offset1;
436 offset2 = -offset2;
437 }
438 else
439 setAxes( joint, x, y, z, joint->axis1, NULL );
440
441 joint->computeInitialRelativeRotations();
442
443
444 dVector3 ax2;
445 getAxis2( joint, ax2, joint->axis2 );
446
447 {
448 dVector3 ax1;
449 joint->getAxes(ax1, ax2);
450 }
451
452
453
454 dQuaternion qAngle;
455 dQFromAxisAndAngle(qAngle, x, y, z, offset1);
456
457 dMatrix3 R;
458 dRFrom2Axes( R, x, y, z, ax2[0], ax2[1], ax2[2] );
459
460 dQuaternion qcross;
461 dRtoQ( R, qcross );
462
463 dQuaternion qOffset;
464 dQMultiply0(qOffset, qAngle, qcross);
465
466 dQMultiply1( joint->qrel1, joint->node[0].body->q, qOffset );
467
468 // Calculating the second offset
469 dQFromAxisAndAngle(qAngle, ax2[0], ax2[1], ax2[2], offset2);
470
471 dRFrom2Axes( R, ax2[0], ax2[1], ax2[2], x, y, z );
472 dRtoQ( R, qcross );
473
474 dQMultiply1(qOffset, qAngle, qcross);
475 if ( joint->node[1].body )
476 {
477 dQMultiply1( joint->qrel2, joint->node[1].body->q, qOffset );
478 }
479 else
480 {
481 joint->qrel2[0] = qcross[0];
482 joint->qrel2[1] = qcross[1];
483 joint->qrel2[2] = qcross[2];
484 joint->qrel2[3] = qcross[3];
485 }
486 }
487
488
489 void dJointSetUniversalAxis2( dJointID j, dReal x, dReal y, dReal z )
490 {
491 dxJointUniversal* joint = ( dxJointUniversal* )j;
492 dUASSERT( joint, "bad joint argument" );
493 checktype( joint, Universal );
494 if ( joint->flags & dJOINT_REVERSE )
495 setAxes( joint, x, y, z, joint->axis1, NULL );
496 else
497 setAxes( joint, x, y, z, NULL, joint->axis2 );
498 joint->computeInitialRelativeRotations();
499 }
500
501 void dJointSetUniversalAxis2Offset( dJointID j, dReal x, dReal y, dReal z,
502 dReal offset1, dReal offset2 )
503 {
504 dxJointUniversal* joint = ( dxJointUniversal* )j;
505 dUASSERT( joint, "bad joint argument" );
506 checktype( joint, Universal );
507
508 if ( joint->flags & dJOINT_REVERSE )
509 {
510 setAxes( joint, x, y, z, joint->axis1, NULL );
511 offset1 = -offset2;
512 offset2 = -offset1;
513 }
514 else
515 setAxes( joint, x, y, z, NULL, joint->axis2 );
516
517
518 joint->computeInitialRelativeRotations();
519
520 // It is easier to retreive the 2 axes here since
521 // when there is only one body B2 (the axes switch position)
522 // Doing this way eliminate the need to write the code differently
523 // for both case.
524 dVector3 ax1, ax2;
525 joint->getAxes(ax1, ax2 );
526
527
528
529 dQuaternion qAngle;
530 dQFromAxisAndAngle(qAngle, ax1[0], ax1[1], ax1[2], offset1);
531
532 dMatrix3 R;
533 dRFrom2Axes( R, ax1[0], ax1[1], ax1[2], ax2[0], ax2[1], ax2[2]);
534
535 dQuaternion qcross;
536 dRtoQ( R, qcross );
537
538 dQuaternion qOffset;
539 dQMultiply0(qOffset, qAngle, qcross);
540
541
542
543 dQMultiply1( joint->qrel1, joint->node[0].body->q, qOffset );
544
545
546 // Calculating the second offset
547 dQFromAxisAndAngle(qAngle, ax2[0], ax2[1], ax2[2], offset2);
548
549 dRFrom2Axes( R, ax2[0], ax2[1], ax2[2], ax1[0], ax1[1], ax1[2]);
550 dRtoQ( R, qcross );
551
552 dQMultiply1(qOffset, qAngle, qcross);
553 if ( joint->node[1].body )
554 {
555 dQMultiply1( joint->qrel2, joint->node[1].body->q, qOffset );
556 }
557 else
558 {
559 joint->qrel2[0] = qcross[0];
560 joint->qrel2[1] = qcross[1];
561 joint->qrel2[2] = qcross[2];
562 joint->qrel2[3] = qcross[3];
563 }
564 }
565
566
567 void dJointGetUniversalAnchor( dJointID j, dVector3 result )
568 {
569 dxJointUniversal* joint = ( dxJointUniversal* )j;
570 dUASSERT( joint, "bad joint argument" );
571 dUASSERT( result, "bad result argument" );
572 checktype( joint, Universal );
573 if ( joint->flags & dJOINT_REVERSE )
574 getAnchor2( joint, result, joint->anchor2 );
575 else
576 getAnchor( joint, result, joint->anchor1 );
577 }
578
579
580 void dJointGetUniversalAnchor2( dJointID j, dVector3 result )
581 {
582 dxJointUniversal* joint = ( dxJointUniversal* )j;
583 dUASSERT( joint, "bad joint argument" );
584 dUASSERT( result, "bad result argument" );
585 checktype( joint, Universal );
586 if ( joint->flags & dJOINT_REVERSE )
587 getAnchor( joint, result, joint->anchor1 );
588 else
589 getAnchor2( joint, result, joint->anchor2 );
590 }
591
592
593 void dJointGetUniversalAxis1( dJointID j, dVector3 result )
594 {
595 dxJointUniversal* joint = ( dxJointUniversal* )j;
596 dUASSERT( joint, "bad joint argument" );
597 dUASSERT( result, "bad result argument" );
598 checktype( joint, Universal );
599 if ( joint->flags & dJOINT_REVERSE )
600 getAxis2( joint, result, joint->axis2 );
601 else
602 getAxis( joint, result, joint->axis1 );
603 }
604
605
606 void dJointGetUniversalAxis2( dJointID j, dVector3 result )
607 {
608 dxJointUniversal* joint = ( dxJointUniversal* )j;
609 dUASSERT( joint, "bad joint argument" );
610 dUASSERT( result, "bad result argument" );
611 checktype( joint, Universal );
612 if ( joint->flags & dJOINT_REVERSE )
613 getAxis( joint, result, joint->axis1 );
614 else
615 getAxis2( joint, result, joint->axis2 );
616 }
617
618
619 void dJointSetUniversalParam( dJointID j, int parameter, dReal value )
620 {
621 dxJointUniversal* joint = ( dxJointUniversal* )j;
622 dUASSERT( joint, "bad joint argument" );
623 checktype( joint, Universal );
624 if (( parameter & 0xff00 ) == 0x100 )
625 {
626 joint->limot2.set( parameter & 0xff, value );
627 }
628 else
629 {
630 joint->limot1.set( parameter, value );
631 }
632 }
633
634
635 dReal dJointGetUniversalParam( dJointID j, int parameter )
636 {
637 dxJointUniversal* joint = ( dxJointUniversal* )j;
638 dUASSERT( joint, "bad joint argument" );
639 checktype( joint, Universal );
640 if (( parameter & 0xff00 ) == 0x100 )
641 {
642 return joint->limot2.get( parameter & 0xff );
643 }
644 else
645 {
646 return joint->limot1.get( parameter );
647 }
648 }
649
650 void dJointGetUniversalAngles( dJointID j, dReal *angle1, dReal *angle2 )
651 {
652 dxJointUniversal* joint = ( dxJointUniversal* )j;
653 dUASSERT( joint, "bad joint argument" );
654 checktype( joint, Universal );
655 if ( joint->flags & dJOINT_REVERSE )
656 {
657 joint->getAngles( angle2, angle1 );
658 *angle2 = -(*angle2);
659 return;
660 }
661 else
662 return joint->getAngles( angle1, angle2 );
663 }
664
665
666 dReal dJointGetUniversalAngle1( dJointID j )
667 {
668 dxJointUniversal* joint = ( dxJointUniversal* )j;
669 dUASSERT( joint, "bad joint argument" );
670 checktype( joint, Universal );
671 if ( joint->flags & dJOINT_REVERSE )
672 return joint->getAngle2();
673 else
674 return joint->getAngle1();
675 }
676
677
678 dReal dJointGetUniversalAngle2( dJointID j )
679 {
680 dxJointUniversal* joint = ( dxJointUniversal* )j;
681 dUASSERT( joint, "bad joint argument" );
682 checktype( joint, Universal );
683 if ( joint->flags & dJOINT_REVERSE )
684 return -joint->getAngle1();
685 else
686 return joint->getAngle2();
687 }
688
689
690 dReal dJointGetUniversalAngle1Rate( dJointID j )
691 {
692 dxJointUniversal* joint = ( dxJointUniversal* )j;
693 dUASSERT( joint, "bad joint argument" );
694 checktype( joint, Universal );
695
696 if ( joint->node[0].body )
697 {
698 dVector3 axis;
699
700 if ( joint->flags & dJOINT_REVERSE )
701 getAxis2( joint, axis, joint->axis2 );
702 else
703 getAxis( joint, axis, joint->axis1 );
704
705 dReal rate = dCalcVectorDot3( axis, joint->node[0].body->avel );
706 if ( joint->node[1].body )
707 rate -= dCalcVectorDot3( axis, joint->node[1].body->avel );
708 return rate;
709 }
710 return 0;
711 }
712
713
714 dReal dJointGetUniversalAngle2Rate( dJointID j )
715 {
716 dxJointUniversal* joint = ( dxJointUniversal* )j;
717 dUASSERT( joint, "bad joint argument" );
718 checktype( joint, Universal );
719
720 if ( joint->node[0].body )
721 {
722 dVector3 axis;
723
724 if ( joint->flags & dJOINT_REVERSE )
725 getAxis( joint, axis, joint->axis1 );
726 else
727 getAxis2( joint, axis, joint->axis2 );
728
729 dReal rate = dCalcVectorDot3( axis, joint->node[0].body->avel );
730 if ( joint->node[1].body ) rate -= dCalcVectorDot3( axis, joint->node[1].body->avel );
731 return rate;
732 }
733 return 0;
734 }
735
736
737 void dJointAddUniversalTorques( dJointID j, dReal torque1, dReal torque2 )
738 {
739 dxJointUniversal* joint = ( dxJointUniversal* )j;
740 dVector3 axis1, axis2;
741 dAASSERT( joint );
742 checktype( joint, Universal );
743
744 if ( joint->flags & dJOINT_REVERSE )
745 {
746 dReal temp = torque1;
747 torque1 = - torque2;
748 torque2 = - temp;
749 }
750
751 getAxis( joint, axis1, joint->axis1 );
752 getAxis2( joint, axis2, joint->axis2 );
753 axis1[0] = axis1[0] * torque1 + axis2[0] * torque2;
754 axis1[1] = axis1[1] * torque1 + axis2[1] * torque2;
755 axis1[2] = axis1[2] * torque1 + axis2[2] * torque2;
756
757 if ( joint->node[0].body != 0 )
758 dBodyAddTorque( joint->node[0].body, axis1[0], axis1[1], axis1[2] );
759 if ( joint->node[1].body != 0 )
760 dBodyAddTorque( joint->node[1].body, -axis1[0], -axis1[1], -axis1[2] );
761 }
762
763
764 dJointType
765 dxJointUniversal::type() const
766 {
767 return dJointTypeUniversal;
768 }
769
770
771 size_t
772 dxJointUniversal::size() const
773 {
774 return sizeof( *this );
775 }
776
777
778
779 void
780 dxJointUniversal::setRelativeValues()
781 {
782 dVector3 anchor;
783 dJointGetUniversalAnchor(this, anchor);
784 setAnchors( this, anchor[0], anchor[1], anchor[2], anchor1, anchor2 );
785
786 dVector3 ax1,ax2;
787 dJointGetUniversalAxis1(this, ax1);
788 dJointGetUniversalAxis2(this, ax2);
789
790 if ( flags & dJOINT_REVERSE )
791 {
792 setAxes( this, ax1[0],ax1[1],ax1[2], NULL, axis2 );
793 setAxes( this, ax2[0],ax2[1],ax2[2], axis1, NULL );
794 }
795 else
796 {
797 setAxes( this, ax1[0],ax1[1],ax1[2], axis1, NULL );
798 setAxes( this, ax2[0],ax2[1],ax2[2], NULL, axis2 );
799 }
800
801 computeInitialRelativeRotations();
802 }
803