| 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 "dball.h" |
| 27 |
#include "joint_internal.h" |
| 28 |
|
| 29 |
/* |
| 30 |
* Double Ball joint: tries to maintain a fixed distance between two anchor |
| 31 |
* points. |
| 32 |
*/ |
| 33 |
|
| 34 |
dxJointDBall::dxJointDBall(dxWorld *w) : |
| 35 |
dxJoint(w) |
| 36 |
{ |
| 37 |
dSetZero(anchor1, 3); |
| 38 |
dSetZero(anchor2, 3); |
| 39 |
targetDistance = 0; |
| 40 |
erp = world->global_erp; |
| 41 |
cfm = world->global_cfm; |
| 42 |
} |
| 43 |
|
| 44 |
void |
| 45 |
dxJointDBall::getSureMaxInfo( SureMaxInfo* info ) |
| 46 |
{ |
| 47 |
info->max_m = 1; |
| 48 |
} |
| 49 |
void |
| 50 |
dxJointDBall::getInfo1( dxJoint::Info1 *info ) |
| 51 |
{ |
| 52 |
info->m = 1; |
| 53 |
info->nub = 1; |
| 54 |
} |
| 55 |
|
| 56 |
void |
| 57 |
dxJointDBall::getInfo2( dReal worldFPS, dReal /*worldERP*/, |
| 58 |
int rowskip, dReal *J1, dReal *J2, |
| 59 |
int pairskip, dReal *pairRhsCfm, dReal *pairLoHi, |
| 60 |
int *findex ) |
| 61 |
{ |
| 62 |
dVector3 globalA1, globalA2; |
| 63 |
dBodyGetRelPointPos(node[0].body, anchor1[0], anchor1[1], anchor1[2], globalA1); |
| 64 |
|
| 65 |
if (node[1].body) { |
| 66 |
dBodyGetRelPointPos(node[1].body, anchor2[0], anchor2[1], anchor2[2], globalA2); |
| 67 |
} else { |
| 68 |
dCopyVector3(globalA2, anchor2); |
| 69 |
} |
| 70 |
|
| 71 |
dVector3 q; |
| 72 |
dSubtractVectors3(q, globalA1, globalA2); |
| 73 |
|
| 74 |
#ifdef dSINGLE |
| 75 |
const dReal MIN_LENGTH = REAL(1e-7); |
| 76 |
#else |
| 77 |
const dReal MIN_LENGTH = REAL(1e-12); |
| 78 |
#endif |
| 79 |
|
| 80 |
if (dCalcVectorLength3(q) < MIN_LENGTH) { |
| 81 |
// too small, let's choose an arbitrary direction |
| 82 |
// heuristic: difference in velocities at anchors |
| 83 |
dVector3 v1, v2; |
| 84 |
dBodyGetPointVel(node[0].body, globalA1[0], globalA1[1], globalA1[2], v1); |
| 85 |
|
| 86 |
if (node[1].body) { |
| 87 |
dBodyGetPointVel(node[1].body, globalA2[0], globalA2[1], globalA2[2], v2); |
| 88 |
} else { |
| 89 |
dZeroVector3(v2); |
| 90 |
} |
| 91 |
|
| 92 |
dSubtractVectors3(q, v1, v2); |
| 93 |
|
| 94 |
if (dCalcVectorLength3(q) < MIN_LENGTH) { |
| 95 |
// this direction is as good as any |
| 96 |
dAssignVector3(q, 1, 0, 0); |
| 97 |
} |
| 98 |
} |
| 99 |
dNormalize3(q); |
| 100 |
|
| 101 |
dCopyVector3(J1 + GI2__JL_MIN, q); |
| 102 |
|
| 103 |
dVector3 relA1; |
| 104 |
dBodyVectorToWorld(node[0].body, |
| 105 |
anchor1[0], anchor1[1], anchor1[2], |
| 106 |
relA1); |
| 107 |
|
| 108 |
dMatrix3 a1m; |
| 109 |
dZeroMatrix3(a1m); |
| 110 |
dSetCrossMatrixMinus(a1m, relA1, 4); |
| 111 |
|
| 112 |
dMultiply1_331(J1 + GI2__JA_MIN, a1m, q); |
| 113 |
|
| 114 |
if (node[1].body) { |
| 115 |
dCopyNegatedVector3(J2 + GI2__JL_MIN, q); |
| 116 |
|
| 117 |
dVector3 relA2; |
| 118 |
dBodyVectorToWorld(node[1].body, |
| 119 |
anchor2[0], anchor2[1], anchor2[2], |
| 120 |
relA2); |
| 121 |
dMatrix3 a2m; |
| 122 |
dZeroMatrix3(a2m); |
| 123 |
dSetCrossMatrixPlus(a2m, relA2, 4); |
| 124 |
dMultiply1_331(J2 + GI2__JA_MIN, a2m, q); |
| 125 |
} |
| 126 |
|
| 127 |
const dReal k = worldFPS * this->erp; |
| 128 |
pairRhsCfm[GI2_RHS] = k * (targetDistance - dCalcPointsDistance3(globalA1, globalA2)); |
| 129 |
pairRhsCfm[GI2_CFM] = this->cfm; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
void |
| 134 |
dxJointDBall::updateTargetDistance() |
| 135 |
{ |
| 136 |
dVector3 p1, p2; |
| 137 |
|
| 138 |
if (node[0].body) |
| 139 |
dBodyGetRelPointPos(node[0].body, anchor1[0], anchor1[1], anchor1[2], p1); |
| 140 |
else |
| 141 |
dCopyVector3(p1, anchor1); |
| 142 |
if (node[1].body) |
| 143 |
dBodyGetRelPointPos(node[1].body, anchor2[0], anchor2[1], anchor2[2], p2); |
| 144 |
else |
| 145 |
dCopyVector3(p2, anchor2); |
| 146 |
|
| 147 |
targetDistance = dCalcPointsDistance3(p1, p2); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
void dJointSetDBallAnchor1( dJointID j, dReal x, dReal y, dReal z ) |
| 152 |
{ |
| 153 |
dxJointDBall* joint = static_cast<dxJointDBall*>(j); |
| 154 |
dUASSERT( joint, "bad joint argument" ); |
| 155 |
|
| 156 |
if ( joint->flags & dJOINT_REVERSE ) { |
| 157 |
if (joint->node[1].body) |
| 158 |
dBodyGetPosRelPoint(joint->node[1].body, x, y, z, joint->anchor2); |
| 159 |
else { |
| 160 |
joint->anchor2[0] = x; |
| 161 |
joint->anchor2[1] = y; |
| 162 |
joint->anchor2[2] = z; |
| 163 |
} |
| 164 |
} else { |
| 165 |
if (joint->node[0].body) |
| 166 |
dBodyGetPosRelPoint(joint->node[0].body, x, y, z, joint->anchor1); |
| 167 |
else { |
| 168 |
joint->anchor1[0] = x; |
| 169 |
joint->anchor1[1] = y; |
| 170 |
joint->anchor1[2] = z; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
joint->updateTargetDistance(); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
void dJointSetDBallAnchor2( dJointID j, dReal x, dReal y, dReal z ) |
| 179 |
{ |
| 180 |
dxJointDBall* joint = static_cast<dxJointDBall*>(j); |
| 181 |
dUASSERT( joint, "bad joint argument" ); |
| 182 |
|
| 183 |
|
| 184 |
if ( joint->flags & dJOINT_REVERSE ) { |
| 185 |
if (joint->node[0].body) |
| 186 |
dBodyGetPosRelPoint(joint->node[0].body, x, y, z, joint->anchor1); |
| 187 |
else { |
| 188 |
joint->anchor1[0] = x; |
| 189 |
joint->anchor1[1] = y; |
| 190 |
joint->anchor1[2] = z; |
| 191 |
} |
| 192 |
} else { |
| 193 |
if (joint->node[1].body) |
| 194 |
dBodyGetPosRelPoint(joint->node[1].body, x, y, z, joint->anchor2); |
| 195 |
else { |
| 196 |
joint->anchor2[0] = x; |
| 197 |
joint->anchor2[1] = y; |
| 198 |
joint->anchor2[2] = z; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
joint->updateTargetDistance(); |
| 203 |
} |
| 204 |
|
| 205 |
dReal dJointGetDBallDistance(dJointID j) |
| 206 |
{ |
| 207 |
dxJointDBall* joint = static_cast<dxJointDBall*>(j); |
| 208 |
dUASSERT( joint, "bad joint argument" ); |
| 209 |
|
| 210 |
return joint->targetDistance; |
| 211 |
} |
| 212 |
|
| 213 |
void dJointSetDBallDistance(dJointID j, dReal dist) |
| 214 |
{ |
| 215 |
dxJointDBall* joint = static_cast<dxJointDBall*>(j); |
| 216 |
dUASSERT( joint, "bad joint argument" ); |
| 217 |
dUASSERT( dist>=0, "target distance must be non-negative" ); |
| 218 |
|
| 219 |
joint->targetDistance = dist; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
void dJointGetDBallAnchor1( dJointID j, dVector3 result ) |
| 224 |
{ |
| 225 |
dxJointDBall* joint = static_cast<dxJointDBall*>(j); |
| 226 |
dUASSERT( joint, "bad joint argument" ); |
| 227 |
dUASSERT( result, "bad result argument" ); |
| 228 |
|
| 229 |
if ( joint->flags & dJOINT_REVERSE ) { |
| 230 |
if (joint->node[1].body) |
| 231 |
dBodyGetRelPointPos(joint->node[1].body, joint->anchor2[0], joint->anchor2[1], joint->anchor2[2], result); |
| 232 |
else |
| 233 |
dCopyVector3(result, joint->anchor2); |
| 234 |
} else { |
| 235 |
if (joint->node[0].body) |
| 236 |
dBodyGetRelPointPos(joint->node[0].body, joint->anchor1[0], joint->anchor1[1], joint->anchor1[2], result); |
| 237 |
else |
| 238 |
dCopyVector3(result, joint->anchor1); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
void dJointGetDBallAnchor2( dJointID j, dVector3 result ) |
| 244 |
{ |
| 245 |
dxJointDBall* joint = static_cast<dxJointDBall*>(j); |
| 246 |
dUASSERT( joint, "bad joint argument" ); |
| 247 |
dUASSERT( result, "bad result argument" ); |
| 248 |
|
| 249 |
if ( joint->flags & dJOINT_REVERSE ) { |
| 250 |
if (joint->node[0].body) |
| 251 |
dBodyGetRelPointPos(joint->node[0].body, joint->anchor1[0], joint->anchor1[1], joint->anchor1[2], result); |
| 252 |
else |
| 253 |
dCopyVector3(result, joint->anchor1); |
| 254 |
} else { |
| 255 |
if (joint->node[1].body) |
| 256 |
dBodyGetRelPointPos(joint->node[1].body, joint->anchor2[0], joint->anchor2[1], joint->anchor2[2], result); |
| 257 |
else |
| 258 |
dCopyVector3(result, joint->anchor2); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
void dJointSetDBallParam( dJointID j, int parameter, dReal value ) |
| 264 |
{ |
| 265 |
dxJointDBall* joint = static_cast<dxJointDBall*>(j); |
| 266 |
dUASSERT( joint, "bad joint argument" ); |
| 267 |
|
| 268 |
switch ( parameter ) { |
| 269 |
case dParamCFM: |
| 270 |
joint->cfm = value; |
| 271 |
break; |
| 272 |
case dParamERP: |
| 273 |
joint->erp = value; |
| 274 |
break; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
dReal dJointGetDBallParam( dJointID j, int parameter ) |
| 280 |
{ |
| 281 |
dxJointDBall* joint = static_cast<dxJointDBall*>(j); |
| 282 |
dUASSERT( joint, "bad joint argument" ); |
| 283 |
|
| 284 |
switch ( parameter ) { |
| 285 |
case dParamCFM: |
| 286 |
return joint->cfm; |
| 287 |
case dParamERP: |
| 288 |
return joint->erp; |
| 289 |
default: |
| 290 |
return 0; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
dJointType |
| 296 |
dxJointDBall::type() const |
| 297 |
{ |
| 298 |
return dJointTypeDBall; |
| 299 |
} |
| 300 |
|
| 301 |
size_t |
| 302 |
dxJointDBall::size() const |
| 303 |
{ |
| 304 |
return sizeof( *this ); |
| 305 |
} |
| 306 |
|
| 307 |
void |
| 308 |
dxJointDBall::setRelativeValues() |
| 309 |
{ |
| 310 |
updateTargetDistance(); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|