Debian Codegraph @

source/unix/odesrc/joints/hinge.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 "hinge.h"
27 #include "joint_internal.h"
28
29
30 //****************************************************************************
31 // hinge
32
33 dxJointHinge::dxJointHinge( dxWorld *w ) :
34 dxJoint( w )
35 {
36 dSetZero( anchor1, 4 );
37 dSetZero( anchor2, 4 );
38 dSetZero( axis1, 4 );
39 axis1[0] = 1;
40 dSetZero( axis2, 4 );
41 axis2[0] = 1;
42 dSetZero( qrel, 4 );
43 limot.init( world );
44 }
45
46
47 void
48 dxJointHinge::getSureMaxInfo( SureMaxInfo* info )
49 {
50 info->max_m = 6;
51 }
52
53
54 void
55 dxJointHinge::getInfo1( dxJoint::Info1 *info )
56 {
57 info->nub = 5;
58
59 // see if joint is powered
60 if ( limot.fmax > 0 )
61 info->m = 6; // powered hinge needs an extra constraint row
62 else info->m = 5;
63
64 // see if we're at a joint limit.
65 if (( limot.lostop >= -M_PI || limot.histop <= M_PI ) &&
66 limot.lostop <= limot.histop )
67 {
68 dReal angle = getHingeAngle( node[0].body,
69 node[1].body,
70 axis1, qrel );
71 if ( limot.testRotationalLimit( angle ) )
72 info->m = 6;
73 }
74 }
75
76
77 void dxJointHinge::getInfo2( dReal worldFPS, dReal worldERP,
78 int rowskip, dReal *J1, dReal *J2,
79 int pairskip, dReal *pairRhsCfm, dReal *pairLoHi,
80 int *findex )
81 {
82 // set the three ball-and-socket rows
83 setBall( this, worldFPS, worldERP, rowskip, J1, J2, pairskip, pairRhsCfm, anchor1, anchor2 );
84
85 // set the two hinge rows. the hinge axis should be the only unconstrained
86 // rotational axis, the angular velocity of the two bodies perpendicular to
87 // the hinge axis should be equal. thus the constraint equations are
88 // p*w1 - p*w2 = 0
89 // q*w1 - q*w2 = 0
90 // where p and q are unit vectors normal to the hinge axis, and w1 and w2
91 // are the angular velocity vectors of the two bodies.
92
93 dVector3 ax1; // length 1 joint axis in global coordinates, from 1st body
94 dVector3 p, q; // plane space vectors for ax1
95 dMultiply0_331( ax1, node[0].body->posr.R, axis1 );
96 dPlaneSpace( ax1, p, q );
97
98 dxBody *body1 = node[1].body;
99
100 int currRowSkip = 3 * rowskip;
101 dCopyVector3(J1 + currRowSkip + GI2__JA_MIN, p);
102 if ( body1 ) {
103 dCopyNegatedVector3(J2 + currRowSkip + GI2__JA_MIN, p);
104 }
105
106 currRowSkip += rowskip;
107 dCopyVector3(J1 + currRowSkip + GI2__JA_MIN, q);
108 if ( body1 ) {
109 dCopyNegatedVector3(J2 + currRowSkip + GI2__JA_MIN, q);
110 }
111
112 // compute the right hand side of the constraint equation. set relative
113 // body velocities along p and q to bring the hinge back into alignment.
114 // if ax1,ax2 are the unit length hinge axes as computed from body1 and
115 // body2, we need to rotate both bodies along the axis u = (ax1 x ax2).
116 // if `theta' is the angle between ax1 and ax2, we need an angular velocity
117 // along u to cover angle erp*theta in one step :
118 // |angular_velocity| = angle/time = erp*theta / stepsize
119 // = (erp*fps) * theta
120 // angular_velocity = |angular_velocity| * (ax1 x ax2) / |ax1 x ax2|
121 // = (erp*fps) * theta * (ax1 x ax2) / sin(theta)
122 // ...as ax1 and ax2 are unit length. if theta is smallish,
123 // theta ~= sin(theta), so
124 // angular_velocity = (erp*fps) * (ax1 x ax2)
125 // ax1 x ax2 is in the plane space of ax1, so we project the angular
126 // velocity to p and q to find the right hand side.
127
128 dVector3 b;
129 if ( body1 ) {
130 dVector3 ax2;
131 dMultiply0_331( ax2, body1->posr.R, axis2 );
132 dCalcVectorCross3( b, ax1, ax2 );
133 } else {
134 dCalcVectorCross3( b, ax1, axis2 );
135 }
136
137 dReal k = worldFPS * worldERP;
138 int currPairSkip = 3 * pairskip;
139 pairRhsCfm[currPairSkip + GI2_RHS] = k * dCalcVectorDot3( b, p );
140 currPairSkip += pairskip;
141 pairRhsCfm[currPairSkip + GI2_RHS] = k * dCalcVectorDot3( b, q );
142
143 // if the hinge is powered, or has joint limits, add in the stuff
144 currRowSkip += rowskip;
145 currPairSkip += pairskip;
146 limot.addLimot( this, worldFPS, J1 + currRowSkip, J2 + currRowSkip, pairRhsCfm + currPairSkip, pairLoHi + currPairSkip, ax1, 1 );
147 }
148
149
150
151 void dJointSetHingeAnchor( dJointID j, dReal x, dReal y, dReal z )
152 {
153 dxJointHinge* joint = ( dxJointHinge* )j;
154 dUASSERT( joint, "bad joint argument" );
155 checktype( joint, Hinge );
156 setAnchors( joint, x, y, z, joint->anchor1, joint->anchor2 );
157 joint->computeInitialRelativeRotation();
158 }
159
160
161 void dJointSetHingeAnchorDelta( dJointID j, dReal x, dReal y, dReal z, dReal dx, dReal dy, dReal dz )
162 {
163 dxJointHinge* joint = ( dxJointHinge* )j;
164 dUASSERT( joint, "bad joint argument" );
165 checktype( joint, Hinge );
166
167 if ( joint->node[0].body )
168 {
169 dReal q[4];
170 q[0] = x - joint->node[0].body->posr.pos[0];
171 q[1] = y - joint->node[0].body->posr.pos[1];
172 q[2] = z - joint->node[0].body->posr.pos[2];
173 q[3] = 0;
174 dMultiply1_331( joint->anchor1, joint->node[0].body->posr.R, q );
175
176 if ( joint->node[1].body )
177 {
178 q[0] = x - joint->node[1].body->posr.pos[0];
179 q[1] = y - joint->node[1].body->posr.pos[1];
180 q[2] = z - joint->node[1].body->posr.pos[2];
181 q[3] = 0;
182 dMultiply1_331( joint->anchor2, joint->node[1].body->posr.R, q );
183 }
184 else
185 {
186 // Move the relative displacement between the passive body and the
187 // anchor in the same direction as the passive body has just moved
188 joint->anchor2[0] = x + dx;
189 joint->anchor2[1] = y + dy;
190 joint->anchor2[2] = z + dz;
191 }
192 }
193 joint->anchor1[3] = 0;
194 joint->anchor2[3] = 0;
195
196 joint->computeInitialRelativeRotation();
197 }
198
199
200
201 void dJointSetHingeAxis( dJointID j, dReal x, dReal y, dReal z )
202 {
203 dxJointHinge* joint = ( dxJointHinge* )j;
204 dUASSERT( joint, "bad joint argument" );
205 checktype( joint, Hinge );
206 setAxes( joint, x, y, z, joint->axis1, joint->axis2 );
207 joint->computeInitialRelativeRotation();
208 }
209
210
211 void dJointSetHingeAxisOffset( dJointID j, dReal x, dReal y, dReal z, dReal dangle )
212 {
213 dxJointHinge* joint = ( dxJointHinge* )j;
214 dUASSERT( joint, "bad joint argument" );
215 checktype( joint, Hinge );
216 setAxes( joint, x, y, z, joint->axis1, joint->axis2 );
217 joint->computeInitialRelativeRotation();
218
219 if ( joint->flags & dJOINT_REVERSE ) dangle = -dangle;
220
221 dQuaternion qAngle, qOffset;
222 dQFromAxisAndAngle(qAngle, x, y, z, dangle);
223 dQMultiply3(qOffset, qAngle, joint->qrel);
224 joint->qrel[0] = qOffset[0];
225 joint->qrel[1] = qOffset[1];
226 joint->qrel[2] = qOffset[2];
227 joint->qrel[3] = qOffset[3];
228 }
229
230
231
232 void dJointGetHingeAnchor( dJointID j, dVector3 result )
233 {
234 dxJointHinge* joint = ( dxJointHinge* )j;
235 dUASSERT( joint, "bad joint argument" );
236 dUASSERT( result, "bad result argument" );
237 checktype( joint, Hinge );
238 if ( joint->flags & dJOINT_REVERSE )
239 getAnchor2( joint, result, joint->anchor2 );
240 else
241 getAnchor( joint, result, joint->anchor1 );
242 }
243
244
245 void dJointGetHingeAnchor2( dJointID j, dVector3 result )
246 {
247 dxJointHinge* joint = ( dxJointHinge* )j;
248 dUASSERT( joint, "bad joint argument" );
249 dUASSERT( result, "bad result argument" );
250 checktype( joint, Hinge );
251 if ( joint->flags & dJOINT_REVERSE )
252 getAnchor( joint, result, joint->anchor1 );
253 else
254 getAnchor2( joint, result, joint->anchor2 );
255 }
256
257
258 void dJointGetHingeAxis( dJointID j, dVector3 result )
259 {
260 dxJointHinge* joint = ( dxJointHinge* )j;
261 dUASSERT( joint, "bad joint argument" );
262 dUASSERT( result, "bad result argument" );
263 checktype( joint, Hinge );
264 getAxis( joint, result, joint->axis1 );
265 }
266
267
268 void dJointSetHingeParam( dJointID j, int parameter, dReal value )
269 {
270 dxJointHinge* joint = ( dxJointHinge* )j;
271 dUASSERT( joint, "bad joint argument" );
272 checktype( joint, Hinge );
273 joint->limot.set( parameter, value );
274 }
275
276
277 dReal dJointGetHingeParam( dJointID j, int parameter )
278 {
279 dxJointHinge* joint = ( dxJointHinge* )j;
280 dUASSERT( joint, "bad joint argument" );
281 checktype( joint, Hinge );
282 return joint->limot.get( parameter );
283 }
284
285
286 dReal dJointGetHingeAngle( dJointID j )
287 {
288 dxJointHinge* joint = ( dxJointHinge* )j;
289 dAASSERT( joint );
290 checktype( joint, Hinge );
291 if ( joint->node[0].body )
292 {
293 dReal ang = getHingeAngle( joint->node[0].body,
294 joint->node[1].body,
295 joint->axis1,
296 joint->qrel );
297 if ( joint->flags & dJOINT_REVERSE )
298 return -ang;
299 else
300 return ang;
301 }
302 else return 0;
303 }
304
305
306 dReal dJointGetHingeAngleRate( dJointID j )
307 {
308 dxJointHinge* joint = ( dxJointHinge* )j;
309 dAASSERT( joint );
310 checktype( joint, Hinge );
311 if ( joint->node[0].body )
312 {
313 dVector3 axis;
314 dMultiply0_331( axis, joint->node[0].body->posr.R, joint->axis1 );
315 dReal rate = dCalcVectorDot3( axis, joint->node[0].body->avel );
316 if ( joint->node[1].body ) rate -= dCalcVectorDot3( axis, joint->node[1].body->avel );
317 if ( joint->flags & dJOINT_REVERSE ) rate = - rate;
318 return rate;
319 }
320 else return 0;
321 }
322
323
324 void dJointAddHingeTorque( dJointID j, dReal torque )
325 {
326 dxJointHinge* joint = ( dxJointHinge* )j;
327 dVector3 axis;
328 dAASSERT( joint );
329 checktype( joint, Hinge );
330
331 if ( joint->flags & dJOINT_REVERSE )
332 torque = -torque;
333
334 getAxis( joint, axis, joint->axis1 );
335 axis[0] *= torque;
336 axis[1] *= torque;
337 axis[2] *= torque;
338
339 if ( joint->node[0].body != 0 )
340 dBodyAddTorque( joint->node[0].body, axis[0], axis[1], axis[2] );
341 if ( joint->node[1].body != 0 )
342 dBodyAddTorque( joint->node[1].body, -axis[0], -axis[1], -axis[2] );
343 }
344
345
346 dJointType
347 dxJointHinge::type() const
348 {
349 return dJointTypeHinge;
350 }
351
352
353
354 size_t
355 dxJointHinge::size() const
356 {
357 return sizeof( *this );
358 }
359
360
361 void
362 dxJointHinge::setRelativeValues()
363 {
364 dVector3 vec;
365 dJointGetHingeAnchor(this, vec);
366 setAnchors( this, vec[0], vec[1], vec[2], anchor1, anchor2 );
367
368 dJointGetHingeAxis(this, vec);
369 setAxes( this, vec[0], vec[1], vec[2], axis1, axis2 );
370 computeInitialRelativeRotation();
371 }
372
373
374 /// Compute initial relative rotation body1 -> body2, or env -> body1
375 void
376 dxJointHinge::computeInitialRelativeRotation()
377 {
378 if ( node[0].body )
379 {
380 if ( node[1].body )
381 {
382 dQMultiply1( qrel, node[0].body->q, node[1].body->q );
383 }
384 else
385 {
386 // set qrel to the transpose of the first body q
387 qrel[0] = node[0].body->q[0];
388 qrel[1] = -node[0].body->q[1];
389 qrel[2] = -node[0].body->q[2];
390 qrel[3] = -node[0].body->q[3];
391 }
392 }
393 }
394