Debian Codegraph @

source/unix/odesrc/joints/piston.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 "piston.h"
27 #include "joint_internal.h"
28
29
30
31 //****************************************************************************
32 // Piston
33 //
34
35 dxJointPiston::dxJointPiston ( dxWorld *w ) :
36 dxJoint ( w )
37 {
38 dSetZero ( axis1, 4 );
39 dSetZero ( axis2, 4 );
40
41 axis1[0] = 1;
42 axis2[0] = 1;
43
44 dSetZero ( qrel, 4 );
45
46 dSetZero ( anchor1, 4 );
47 dSetZero ( anchor2, 4 );
48
49 limotP.init ( world );
50
51 limotR.init ( world );
52 }
53
54
55 dReal dJointGetPistonPosition ( dJointID j )
56 {
57 dxJointPiston* joint = ( dxJointPiston* ) j;
58 dUASSERT ( joint, "bad joint argument" );
59 checktype ( joint, Piston );
60
61 if ( joint->node[0].body )
62 {
63 dVector3 q;
64 // get the anchor (or offset) in global coordinates
65 dMultiply0_331 ( q, joint->node[0].body->posr.R, joint->anchor1 );
66
67 if ( joint->node[1].body )
68 {
69 dVector3 anchor2;
70 // get the anchor2 in global coordinates
71 dMultiply0_331 ( anchor2, joint->node[1].body->posr.R, joint->anchor2 );
72
73 q[0] = ( ( joint->node[0].body->posr.pos[0] + q[0] ) -
74 ( joint->node[1].body->posr.pos[0] + anchor2[0] ) );
75 q[1] = ( ( joint->node[0].body->posr.pos[1] + q[1] ) -
76 ( joint->node[1].body->posr.pos[1] + anchor2[1] ) );
77 q[2] = ( ( joint->node[0].body->posr.pos[2] + q[2] ) -
78 ( joint->node[1].body->posr.pos[2] + anchor2[2] ) );
79 }
80 else
81 {
82 // N.B. When there is no body 2 the joint->anchor2 is already in
83 // global coordinates
84 q[0] = ( ( joint->node[0].body->posr.pos[0] + q[0] ) -
85 ( joint->anchor2[0] ) );
86 q[1] = ( ( joint->node[0].body->posr.pos[1] + q[1] ) -
87 ( joint->anchor2[1] ) );
88 q[2] = ( ( joint->node[0].body->posr.pos[2] + q[2] ) -
89 ( joint->anchor2[2] ) );
90
91 if ( joint->flags & dJOINT_REVERSE )
92 {
93 q[0] = -q[0];
94 q[1] = -q[1];
95 q[2] = -q[2];
96 }
97 }
98
99 // get axis in global coordinates
100 dVector3 ax;
101 dMultiply0_331 ( ax, joint->node[0].body->posr.R, joint->axis1 );
102
103 return dCalcVectorDot3 ( ax, q );
104 }
105
106 dDEBUGMSG ( "The function always return 0 since no body are attached" );
107 return 0;
108 }
109
110
111 dReal dJointGetPistonPositionRate ( dJointID j )
112 {
113 dxJointPiston* joint = ( dxJointPiston* ) j;
114 dUASSERT ( joint, "bad joint argument" );
115 checktype ( joint, Piston );
116
117 // get axis in global coordinates
118 dVector3 ax;
119 dMultiply0_331 ( ax, joint->node[0].body->posr.R, joint->axis1 );
120
121 // The linear velocity created by the rotation can be discarded since
122 // the rotation is along the prismatic axis and this rotation don't create
123 // linear velocity in the direction of the prismatic axis.
124 if ( joint->node[1].body )
125 {
126 return ( dCalcVectorDot3 ( ax, joint->node[0].body->lvel ) -
127 dCalcVectorDot3 ( ax, joint->node[1].body->lvel ) );
128 }
129 else
130 {
131 dReal rate = dCalcVectorDot3 ( ax, joint->node[0].body->lvel );
132 return ( (joint->flags & dJOINT_REVERSE) ? -rate : rate);
133 }
134 }
135
136
137 dReal dJointGetPistonAngle ( dJointID j )
138 {
139 dxJointPiston* joint = ( dxJointPiston * ) j;
140 dAASSERT ( joint );
141 checktype ( joint, Piston );
142
143 if ( joint->node[0].body )
144 {
145 dReal ang = getHingeAngle ( joint->node[0].body, joint->node[1].body, joint->axis1,
146 joint->qrel );
147 if ( joint->flags & dJOINT_REVERSE )
148 return -ang;
149 else
150 return ang;
151 }
152 else return 0;
153 }
154
155
156 dReal dJointGetPistonAngleRate ( dJointID j )
157 {
158 dxJointPiston* joint = ( dxJointPiston* ) j;
159 dAASSERT ( joint );
160 checktype ( joint, Piston );
161
162 if ( joint->node[0].body )
163 {
164 dVector3 axis;
165 dMultiply0_331 ( axis, joint->node[0].body->posr.R, joint->axis1 );
166 dReal rate = dCalcVectorDot3 ( axis, joint->node[0].body->avel );
167 if ( joint->node[1].body ) rate -= dCalcVectorDot3 ( axis, joint->node[1].body->avel );
168 if ( joint->flags & dJOINT_REVERSE ) rate = - rate;
169 return rate;
170 }
171 else return 0;
172 }
173
174
175 void
176 dxJointPiston::getSureMaxInfo( SureMaxInfo* info )
177 {
178 info->max_m = 6;
179 }
180
181
182 void
183 dxJointPiston::getInfo1 ( dxJoint::Info1 *info )
184 {
185 info->nub = 4; // Number of unbound variables
186 // The only bound variable is one linear displacement
187
188 info->m = 4; // Default number of constraint row
189
190 // see if we're at a joint limit.
191 limotP.limit = 0;
192 if ( ( limotP.lostop > -dInfinity || limotP.histop < dInfinity ) &&
193 limotP.lostop <= limotP.histop )
194 {
195 // measure joint position
196 dReal pos = dJointGetPistonPosition ( this );
197 limotP.testRotationalLimit ( pos ); // N.B. The fucntion is ill named
198 }
199
200 // powered Piston or at limits needs an extra constraint row
201 if ( limotP.limit || limotP.fmax > 0 ) info->m++;
202
203
204 // see if we're at a joint limit.
205 limotR.limit = 0;
206 if ( ( limotR.lostop > -dInfinity || limotR.histop < dInfinity ) &&
207 limotR.lostop <= limotR.histop )
208 {
209 // measure joint position
210 dReal angle = getHingeAngle ( node[0].body, node[1].body, axis1,
211 qrel );
212 limotR.testRotationalLimit ( angle );
213 }
214
215 // powered Piston or at limits needs an extra constraint row
216 if ( limotR.limit || limotR.fmax > 0 ) info->m++;
217
218 }
219
220
221 void
222 dxJointPiston::getInfo2 ( dReal worldFPS, dReal worldERP,
223 int rowskip, dReal *J1, dReal *J2,
224 int pairskip, dReal *pairRhsCfm, dReal *pairLoHi,
225 int *findex )
226 {
227 const dReal k = worldFPS * worldERP;
228
229
230 // Pull out pos and R for both bodies. also get the `connection'
231 // vector pos2-pos1.
232
233 dVector3 dist; // Current position of body_1 w.r.t "anchor"
234 // 2 bodies anchor is center of body 2
235 // 1 bodies anchor is origin
236 dVector3 lanchor2 = { 0,0,0 };
237
238 dReal *pos1 = node[0].body->posr.pos;
239 dReal *R1 = node[0].body->posr.R;
240 dReal *R2 = NULL;
241
242 dxBody *body1 = node[1].body;
243
244 if ( body1 )
245 {
246 dReal *pos2 = body1->posr.pos;
247 R2 = body1->posr.R;
248
249 dMultiply0_331 ( lanchor2, R2, anchor2 );
250 dist[0] = lanchor2[0] + pos2[0] - pos1[0];
251 dist[1] = lanchor2[1] + pos2[1] - pos1[1];
252 dist[2] = lanchor2[2] + pos2[2] - pos1[2];
253 }
254 else
255 {
256 // pos2 = 0; // N.B. We can do that to be safe but it is no necessary
257 // R2 = 0; // N.B. We can do that to be safe but it is no necessary
258 if ( (flags & dJOINT_REVERSE) != 0 )
259 {
260 dSubtractVectors3(dist, pos1, anchor2); // Invert the value
261 }
262 else
263 {
264 dSubtractVectors3(dist, anchor2, pos1);
265 }
266 }
267
268 // ======================================================================
269 // Work on the angular part (i.e. row 0, 1)
270 // Set the two orientation rows. The rotoide axis should be the only
271 // unconstrained rotational axis, the angular velocity of the two bodies
272 // perpendicular to the rotoide axis should be equal.
273 // Thus the constraint equations are:
274 // p*w1 - p*w2 = 0
275 // q*w1 - q*w2 = 0
276 // where p and q are unit vectors normal to the rotoide axis, and w1 and w2
277 // are the angular velocity vectors of the two bodies.
278 // Since the rotoide axis is the same as the prismatic axis.
279 //
280 //
281 // Also, compute the right hand side (RHS) of the rotation constraint equation set.
282 // The first 2 element will result in the relative angular velocity of the two
283 // bodies along axis p and q. This is set to bring the rotoide back into alignment.
284 // if `theta' is the angle between ax1 and ax2, we need an angular velocity
285 // along u to cover angle erp*theta in one step :
286 // |angular_velocity| = angle/time = erp*theta / stepsize
287 // = (erp*fps) * theta
288 // angular_velocity = |angular_velocity| * u
289 // = (erp*fps) * theta * u
290 // where rotation along unit length axis u by theta brings body 2's frame
291 //
292 // if theta is smallish, sin(theta) ~= theta and cos(theta) ~= 1
293 // where the quaternion of the relative rotation between the two bodies is
294 // quat = [cos(theta/2) sin(theta/2)*u]
295 // quat = [1 theta/2*u]
296 // => q[0] ~= 1
297 // 2 * q[1+i] = theta * u[i]
298 //
299 // Since there is no constraint along the rotoide axis
300 // only along p and q that we want the same angular velocity and need to reduce
301 // the error
302 dVector3 b, ax1, p, q;
303 dMultiply0_331 ( ax1, node[0].body->posr.R, axis1 );
304
305 // Find the 2 axis perpendicular to the rotoide axis.
306 dPlaneSpace ( ax1, p, q );
307
308 // LHS
309 dCopyVector3 ( J1 + GI2__JA_MIN, p );
310
311 if ( body1 )
312 {
313 dCopyNegatedVector3 ( J2 + GI2__JA_MIN, p );
314 }
315
316 dCopyVector3 ( J1 + rowskip + GI2__JA_MIN, q );
317
318 if ( body1 )
319 {
320 dCopyNegatedVector3 ( J2 + rowskip + GI2__JA_MIN, q );
321
322 // Some math for the RHS
323 dVector3 ax2;
324 dMultiply0_331 ( ax2, R2, axis2 );
325 dCalcVectorCross3( b, ax1, ax2 );
326 }
327 else
328 {
329 // Some math for the RHS
330 dCalcVectorCross3( b, ax1, axis2 );
331 }
332
333 // RHS
334 pairRhsCfm[GI2_RHS] = k * dCalcVectorDot3 ( p, b );
335 pairRhsCfm[pairskip + GI2_RHS] = k * dCalcVectorDot3 ( q, b );
336
337
338 // ======================================================================
339 // Work on the linear part (i.e row 2,3)
340 // p2 + R2 anchor2' = p1 + R1 dist'
341 // v2 + w2 R2 anchor2' + R2 d(anchor2')/dt = v1 + w1 R1 dist' + R1 d(dist')/dt
342 // v2 + w2 x anchor2 = v1 + w1 x dist + v_p
343 // v_p is speed of prismatic joint (i.e. elongation rate)
344 // Since the constraints are perpendicular to v_p we have:
345 // p . v_p = 0 and q . v_p = 0
346 // Along p and q we have (since sliding along the prismatic axis is disregarded):
347 // u . ( v2 + w2 x anchor2 = v1 + w1 x dist + v_p) ( where u is p or q )
348 // Simplify
349 // u . v2 + u. w2 x anchor2 = u . v1 + u . w1 x dist
350 // or
351 // u . v1 - u . v2 + u . w1 x dist - u2 . w2 x anchor2 = 0
352 // using the fact that (a x b = - b x a)
353 // u . v1 - u . v2 - u . dist x w1 + u . anchor2 x w2 = 0
354 // With the help of the triple product:
355 // i.e. a . b x c = b . c x a = c . a x b or a . b x c = a x b . c
356 // Ref: http://mathworld.wolfram.com/ScalarTripleProduct.html
357 // u . v1 - u . v2 - u x dist . w1 + u x anchor2 . w2 = 0
358 // u . v1 - u . v2 + dist x u . w1 - u x anchor2 . w2 = 0
359 //
360 // Coeff for 1er line of: J1l => p, J2l => -p
361 // Coeff for 2er line of: J1l => q, J2l => -q
362 // Coeff for 1er line of: J1a => dist x p, J2a => p x anchor2
363 // Coeff for 2er line of: J1a => dist x q, J2a => q x anchor2
364
365 int currRowSkip = 2 * rowskip;
366 {
367 dCopyVector3 ( J1 + currRowSkip + GI2__JL_MIN, p );
368 dCalcVectorCross3( J1 + currRowSkip + GI2__JA_MIN, dist, p );
369
370 if ( body1 )
371 {
372 // info->J2l[s2+i] = -p[i];
373 dCopyNegatedVector3 ( J2 + currRowSkip + GI2__JL_MIN, p );
374 // q x anchor2 instead of anchor2 x q since we want the negative value
375 dCalcVectorCross3( J2 + currRowSkip + GI2__JA_MIN, p, lanchor2 );
376 }
377 }
378
379 currRowSkip += rowskip;
380 {
381 dCopyVector3 ( J1 + currRowSkip + GI2__JL_MIN, q );
382 dCalcVectorCross3( J1 + currRowSkip + GI2__JA_MIN, dist, q );
383
384 if ( body1 )
385 {
386 // info->J2l[s3+i] = -q[i];
387 dCopyNegatedVector3 ( J2 + currRowSkip + GI2__JL_MIN, q );
388 // The cross product is in reverse order since we want the negative value
389 dCalcVectorCross3( J2 + currRowSkip + GI2__JA_MIN, q, lanchor2 );
390 }
391 }
392
393 // We want to make correction for motion not in the line of the axis
394 // We calculate the displacement w.r.t. the "anchor" pt.
395 // i.e. Find the difference between the current position and the initial
396 // position along the constrained axies (i.e. axis p and q).
397 // The bodies can move w.r.t each other only along the prismatic axis
398 //
399 // Compute the RHS of rows 2 and 3
400 dVector3 err;
401 dMultiply0_331 ( err, R1, anchor1 );
402 dSubtractVectors3( err, dist, err );
403
404 int currPairSkip = 2 * pairskip;
405 {
406 pairRhsCfm[currPairSkip + GI2_RHS] = k * dCalcVectorDot3 ( p, err );
407 }
408
409 currPairSkip += pairskip;
410 {
411 pairRhsCfm[currPairSkip + GI2_RHS] = k * dCalcVectorDot3 ( q, err );
412 }
413
414 currRowSkip += rowskip; currPairSkip += pairskip;
415
416 if ( body1 || (flags & dJOINT_REVERSE) == 0 )
417 {
418 if (limotP.addLimot ( this, worldFPS, J1 + currRowSkip, J2 + currRowSkip, pairRhsCfm + currPairSkip, pairLoHi + currPairSkip, ax1, 0 ))
419 {
420 currRowSkip += rowskip; currPairSkip += pairskip;
421 }
422 }
423 else
424 {
425 dVector3 rAx1;
426 dCopyNegatedVector3(rAx1, ax1);
427
428 if (limotP.addLimot ( this, worldFPS, J1 + currRowSkip, J2 + currRowSkip, pairRhsCfm + currPairSkip, pairLoHi + currPairSkip, rAx1, 0 ))
429 {
430 currRowSkip += rowskip; currPairSkip += pairskip;
431 }
432 }
433
434 limotR.addLimot ( this, worldFPS, J1 + currRowSkip, J2 + currRowSkip, pairRhsCfm + currPairSkip, pairLoHi + currPairSkip, ax1, 1 );
435 }
436
437 void dJointSetPistonAnchor ( dJointID j, dReal x, dReal y, dReal z )
438 {
439 dxJointPiston* joint = ( dxJointPiston* ) j;
440 dUASSERT ( joint, "bad joint argument" );
441 checktype ( joint, Piston );
442 setAnchors ( joint, x, y, z, joint->anchor1, joint->anchor2 );
443 joint->computeInitialRelativeRotation();
444
445 }
446
447 void dJointSetPistonAnchorOffset (dJointID j, dReal x, dReal y, dReal z,
448 dReal dx, dReal dy, dReal dz)
449 {
450 dxJointPiston* joint = (dxJointPiston*) j;
451 dUASSERT (joint,"bad joint argument");
452 checktype ( joint, Piston );
453
454 if (joint->flags & dJOINT_REVERSE)
455 {
456 dx = -dx;
457 dy = -dy;
458 dz = -dz;
459 }
460
461 if (joint->node[0].body)
462 {
463 joint->node[0].body->posr.pos[0] -= dx;
464 joint->node[0].body->posr.pos[1] -= dy;
465 joint->node[0].body->posr.pos[2] -= dz;
466 }
467
468 setAnchors (joint,x ,y, z, joint->anchor1, joint->anchor2);
469
470 if (joint->node[0].body)
471 {
472 joint->node[0].body->posr.pos[0] += dx;
473 joint->node[0].body->posr.pos[1] += dy;
474 joint->node[0].body->posr.pos[2] += dz;
475 }
476
477 joint->computeInitialRelativeRotation();
478 }
479
480
481
482 void dJointGetPistonAnchor ( dJointID j, dVector3 result )
483 {
484 dxJointPiston* joint = ( dxJointPiston* ) j;
485 dUASSERT ( joint, "bad joint argument" );
486 dUASSERT ( result, "bad result argument" );
487 checktype ( joint, Piston );
488 if ( joint->flags & dJOINT_REVERSE )
489 getAnchor2 ( joint, result, joint->anchor2 );
490 else
491 getAnchor ( joint, result, joint->anchor1 );
492 }
493
494
495 void dJointGetPistonAnchor2 ( dJointID j, dVector3 result )
496 {
497 dxJointPiston* joint = ( dxJointPiston* ) j;
498 dUASSERT ( joint, "bad joint argument" );
499 dUASSERT ( result, "bad result argument" );
500 checktype ( joint, Piston );
501 if ( joint->flags & dJOINT_REVERSE )
502 getAnchor ( joint, result, joint->anchor1 );
503 else
504 getAnchor2 ( joint, result, joint->anchor2 );
505 }
506
507
508
509 void dJointSetPistonAxis ( dJointID j, dReal x, dReal y, dReal z )
510 {
511 dxJointPiston* joint = ( dxJointPiston* ) j;
512 dUASSERT ( joint, "bad joint argument" );
513 checktype ( joint, Piston );
514
515 setAxes ( joint, x, y, z, joint->axis1, joint->axis2 );
516
517 joint->computeInitialRelativeRotation();
518 }
519
520
521 void dJointSetPistonAxisDelta ( dJointID j, dReal x, dReal y, dReal z,
522 dReal dx, dReal dy, dReal dz )
523 {
524 dxJointPiston* joint = ( dxJointPiston* ) j;
525 dUASSERT ( joint, "bad joint argument" );
526 checktype ( joint, Piston );
527
528 setAxes ( joint, x, y, z, joint->axis1, joint->axis2 );
529
530 joint->computeInitialRelativeRotation();
531
532 dVector3 c = {0,0,0};
533 if ( joint->node[1].body )
534 {
535 c[0] = ( joint->node[0].body->posr.pos[0] -
536 joint->node[1].body->posr.pos[0] - dx );
537 c[1] = ( joint->node[0].body->posr.pos[1] -
538 joint->node[1].body->posr.pos[1] - dy );
539 c[2] = ( joint->node[0].body->posr.pos[2] -
540 joint->node[1].body->posr.pos[2] - dz );
541 }
542 else /*if ( joint->node[0].body )*/ // -- body[0] should always be present -- there is a matrix multiplication below
543 {
544 c[0] = joint->node[0].body->posr.pos[0] - dx;
545 c[1] = joint->node[0].body->posr.pos[1] - dy;
546 c[2] = joint->node[0].body->posr.pos[2] - dz;
547 }
548
549 // Convert into frame of body 1
550 dMultiply1_331 ( joint->anchor1, joint->node[0].body->posr.R, c );
551 }
552
553
554
555 void dJointGetPistonAxis ( dJointID j, dVector3 result )
556 {
557 dxJointPiston* joint = ( dxJointPiston* ) j;
558 dUASSERT ( joint, "bad joint argument" );
559 dUASSERT ( result, "bad result argument" );
560 checktype ( joint, Piston );
561
562 getAxis ( joint, result, joint->axis1 );
563 }
564
565 void dJointSetPistonParam ( dJointID j, int parameter, dReal value )
566 {
567 dxJointPiston* joint = ( dxJointPiston* ) j;
568 dUASSERT ( joint, "bad joint argument" );
569 checktype ( joint, Piston );
570
571 if ( ( parameter & 0xff00 ) == 0x100 )
572 {
573 joint->limotR.set ( parameter & 0xff, value );
574 }
575 else
576 {
577 joint->limotP.set ( parameter, value );
578 }
579 }
580
581
582 dReal dJointGetPistonParam ( dJointID j, int parameter )
583 {
584 dxJointPiston* joint = ( dxJointPiston* ) j;
585 dUASSERT ( joint, "bad joint argument" );
586 checktype ( joint, Piston );
587
588 if ( ( parameter & 0xff00 ) == 0x100 )
589 {
590 return joint->limotR.get ( parameter & 0xff );
591 }
592 else
593 {
594 return joint->limotP.get ( parameter );
595 }
596 }
597
598
599 void dJointAddPistonForce ( dJointID j, dReal force )
600 {
601 dxJointPiston* joint = ( dxJointPiston* ) j;
602 dUASSERT ( joint, "bad joint argument" );
603 checktype ( joint, Piston );
604
605 if ( joint->flags & dJOINT_REVERSE )
606 force -= force;
607
608 dVector3 axis;
609 getAxis ( joint, axis, joint->axis1 );
610 // axis[i] *= force
611 dScaleVector3( axis, force );
612
613
614 if ( joint->node[0].body != 0 )
615 dBodyAddForce ( joint->node[0].body, axis[0], axis[1], axis[2] );
616 if ( joint->node[1].body != 0 )
617 dBodyAddForce ( joint->node[1].body, -axis[0], -axis[1], -axis[2] );
618
619 if ( joint->node[0].body != 0 && joint->node[1].body != 0 )
620 {
621 // Case where we don't need ltd since center of mass of both bodies
622 // pass by the anchor point '*' when travelling along the prismatic axis.
623 // Body_2
624 // Body_1 -----
625 // --- |-- | |
626 // | |---------------*-------------| | ---> prismatic axis
627 // --- |-- | |
628 // -----
629 // Body_2
630 // Case where we need ltd
631 // Body_1
632 // ---
633 // | |---------
634 // --- |
635 // | |--
636 // -----*----- ---> prismatic axis
637 // |-- |
638 // |
639 // |
640 // | -----
641 // | | |
642 // -------| |
643 // | |
644 // -----
645 // Body_2
646 //
647 // In real life force apply at the '*' point
648 // But in ODE the force are applied on the center of mass of Body_1 and Body_2
649 // So we have to add torques on both bodies to compensate for that when there
650 // is an offset between the anchor point and the center of mass of both bodies.
651 //
652 // We need to add to each body T = r x F
653 // Where r is the distance between the cm and '*'
654
655 dVector3 ltd; // Linear Torque Decoupling vector (a torque)
656 dVector3 c; // Distance of the body w.r.t the anchor
657 // N.B. The distance along the prismatic axis might not
658 // not be included in this variable since it won't add
659 // anything to the ltd.
660
661 // Calculate the distance of the body w.r.t the anchor
662
663 // The anchor1 of body1 can be used since:
664 // Real anchor = Position of body 1 + anchor + d* axis1 = anchor in world frame
665 // d is the position of the prismatic joint (i.e. elongation)
666 // Since axis1 x axis1 == 0
667 // We can do the following.
668 dMultiply0_331 ( c, joint->node[0].body->posr.R, joint->anchor1 );
669 dCalcVectorCross3( ltd, c, axis );
670 dBodyAddTorque ( joint->node[0].body, ltd[0], ltd[1], ltd[2] );
671
672
673 dMultiply0_331 ( c, joint->node[1].body->posr.R, joint->anchor2 );
674 dCalcVectorCross3( ltd, c, axis );
675 dBodyAddTorque ( joint->node[1].body, ltd[0], ltd[1], ltd[2] );
676 }
677 }
678
679
680 dJointType
681 dxJointPiston::type() const
682 {
683 return dJointTypePiston;
684 }
685
686
687 size_t
688 dxJointPiston::size() const
689 {
690 return sizeof ( *this );
691 }
692
693
694
695 void
696 dxJointPiston::setRelativeValues()
697 {
698 dVector3 vec;
699 dJointGetPistonAnchor(this, vec);
700 setAnchors( this, vec[0], vec[1], vec[2], anchor1, anchor2 );
701
702 dJointGetPistonAxis(this, vec);
703 setAxes( this, vec[0], vec[1], vec[2], axis1, axis2 );
704
705 computeInitialRelativeRotation();
706 }
707
708
709
710
711 void
712 dxJointPiston::computeInitialRelativeRotation()
713 {
714 if ( node[0].body )
715 {
716 if ( node[1].body )
717 {
718 dQMultiply1 ( qrel, node[0].body->q, node[1].body->q );
719 }
720 else
721 {
722 // set joint->qrel to the transpose of the first body q
723 qrel[0] = node[0].body->q[0];
724 for ( int i = 1; i < 4; i++ )
725 qrel[i] = -node[0].body->q[i];
726 // WARNING do we need the - in -joint->node[0].body->q[i]; or not
727 }
728 }
729 }