Debian Codegraph @

source/unix/odesrc/joints/dhinge.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 "dhinge.h"
27 #include "joint_internal.h"
28
29 /*
30 * Double Hinge joint
31 */
32
33 dxJointDHinge::dxJointDHinge(dxWorld* w) :
34 dxJointDBall(w)
35 {
36 dSetZero(axis1, 3);
37 dSetZero(axis2, 3);
38 }
39
40
41 void
42 dxJointDHinge::getSureMaxInfo( SureMaxInfo* info )
43 {
44 info->max_m = 4;
45 }
46
47
48 void
49 dxJointDHinge::getInfo1( dxJoint::Info1* info )
50 {
51 info->m = 4;
52 info->nub = 4;
53 }
54
55
56 void
57 dxJointDHinge::getInfo2( dReal worldFPS, dReal worldERP,
58 int rowskip, dReal *J1, dReal *J2,
59 int pairskip, dReal *pairRhsCfm, dReal *pairLoHi,
60 int *findex )
61 {
62 dxJointDBall::getInfo2( worldFPS, worldERP, rowskip, J1, J2, pairskip, pairRhsCfm, pairLoHi, findex ); // sets row0
63
64 dVector3 globalAxis1;
65 dBodyVectorToWorld(node[0].body, axis1[0], axis1[1], axis1[2], globalAxis1);
66
67 dxBody *body1 = node[1].body;
68
69 // angular constraints, perpendicular to axis
70 dVector3 p, q;
71 dPlaneSpace(globalAxis1, p, q);
72
73 dCopyVector3(J1 + rowskip + GI2__JA_MIN, p);
74 if ( body1 ) {
75 dCopyNegatedVector3(J2 + rowskip + GI2__JA_MIN, p);
76 }
77
78 dCopyVector3(J1 + 2 * rowskip + GI2__JA_MIN, q);
79 if ( body1 ) {
80 dCopyNegatedVector3(J2 + 2 * rowskip + GI2__JA_MIN, q);
81 }
82
83 dVector3 globalAxis2;
84 if ( body1 ) {
85 dBodyVectorToWorld(body1, axis2[0], axis2[1], axis2[2], globalAxis2);
86 } else {
87 dCopyVector3(globalAxis2, axis2);
88 }
89
90 // similar to the hinge joint
91 dVector3 u;
92 dCalcVectorCross3(u, globalAxis1, globalAxis2);
93
94 const dReal k = worldFPS * this->erp;
95 pairRhsCfm[pairskip + GI2_RHS] = k * dCalcVectorDot3( u, p );
96 pairRhsCfm[2 * pairskip + GI2_RHS] = k * dCalcVectorDot3( u, q );
97
98
99
100
101 /*
102 * Constraint along the axis: translation along it should couple angular movement.
103 * This is just the ball-and-socket derivation, projected onto the hinge axis,
104 * producing a single constraint at the end.
105 *
106 * The choice of "ball" position can be arbitrary; we could place it at the center
107 * of one of the bodies, canceling out its rotational jacobian; or we could make
108 * everything symmetrical by just placing at the midpoint between the centers.
109 *
110 * I like symmetry, so I'll use the second approach here. I'll call the midpoint h.
111 *
112 * Of course, if the second body is NULL, the first body is pretty much locked
113 * along this axis, and the linear constraint is enough.
114 */
115
116 int rowskip_mul_3 = 3 * rowskip;
117 dCopyVector3(J1 + rowskip_mul_3 + GI2__JL_MIN, globalAxis1);
118
119 if ( body1 ) {
120 dVector3 h;
121 dAddScaledVectors3(h, node[0].body->posr.pos, body1->posr.pos, -0.5, 0.5);
122
123 dCalcVectorCross3(J1 + rowskip_mul_3 + GI2__JA_MIN, h, globalAxis1);
124
125 dCopyNegatedVector3(J2 + rowskip_mul_3 + GI2__JL_MIN, globalAxis1);
126 dCopyVector3(J2 + rowskip_mul_3 + GI2__JA_MIN, J1 + rowskip_mul_3 + GI2__JA_MIN);
127 }
128
129 // error correction: both anchors should lie on the same plane perpendicular to the axis
130 dVector3 globalA1, globalA2;
131 dBodyGetRelPointPos(node[0].body, anchor1[0], anchor1[1], anchor1[2], globalA1);
132
133 if ( body1 ) {
134 dBodyGetRelPointPos(body1, anchor2[0], anchor2[1], anchor2[2], globalA2);
135 } else {
136 dCopyVector3(globalA2, anchor2);
137 }
138
139 dVector3 d;
140 dSubtractVectors3(d, globalA1, globalA2); // displacement error
141 pairRhsCfm[3 * pairskip + GI2_RHS] = -k * dCalcVectorDot3(globalAxis1, d);
142 }
143
144 void dJointSetDHingeAxis( dJointID j, dReal x, dReal y, dReal z )
145 {
146 dxJointDHinge* joint = static_cast<dxJointDHinge*>(j);
147 dUASSERT( joint, "bad joint argument" );
148
149 dBodyVectorFromWorld(joint->node[0].body, x, y, z, joint->axis1);
150 if (joint->node[1].body)
151 dBodyVectorFromWorld(joint->node[1].body, x, y, z, joint->axis2);
152 else {
153 joint->axis2[0] = x;
154 joint->axis2[1] = y;
155 joint->axis2[2] = z;
156 }
157 dNormalize3(joint->axis1);
158 dNormalize3(joint->axis2);
159 }
160
161 void dJointGetDHingeAxis( dJointID j, dVector3 result )
162 {
163 dxJointDHinge* joint = static_cast<dxJointDHinge*>(j);
164 dUASSERT( joint, "bad joint argument" );
165
166 dBodyVectorToWorld(joint->node[0].body, joint->axis1[0], joint->axis1[1], joint->axis1[2], result);
167 }
168
169
170 void dJointSetDHingeAnchor1( dJointID j, dReal x, dReal y, dReal z )
171 {
172 dJointSetDBallAnchor1(j, x, y, z);
173 }
174
175
176 void dJointSetDHingeAnchor2( dJointID j, dReal x, dReal y, dReal z )
177 {
178 dJointSetDBallAnchor2(j, x, y, z);
179 }
180
181 dReal dJointGetDHingeDistance(dJointID j)
182 {
183 return dJointGetDBallDistance(j);
184 }
185
186
187 void dJointGetDHingeAnchor1( dJointID j, dVector3 result )
188 {
189 dJointGetDBallAnchor1(j, result);
190 }
191
192
193 void dJointGetDHingeAnchor2( dJointID j, dVector3 result )
194 {
195 dJointGetDBallAnchor2(j, result);
196 }
197
198
199 void dJointSetDHingeParam( dJointID j, int parameter, dReal value )
200 {
201 dJointSetDBallParam(j, parameter, value);
202 }
203
204
205 dReal dJointGetDHingeParam( dJointID j, int parameter )
206 {
207 return dJointGetDBallParam(j, parameter);
208 }
209
210 dJointType
211 dxJointDHinge::type() const
212 {
213 return dJointTypeDHinge;
214 }
215
216 size_t
217 dxJointDHinge::size() const
218 {
219 return sizeof( *this );
220 }