| 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 "contact.h" |
| 27 |
#include "joint_internal.h" |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
//**************************************************************************** |
| 32 |
// contact |
| 33 |
|
| 34 |
dxJointContact::dxJointContact( dxWorld *w ) : |
| 35 |
dxJoint( w ) |
| 36 |
{ |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
void |
| 41 |
dxJointContact::getSureMaxInfo( SureMaxInfo* info ) |
| 42 |
{ |
| 43 |
// ...as the actual m is very likely to hit the maximum |
| 44 |
info->max_m = (contact.surface.mode&dContactRolling)?6:3; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
void |
| 49 |
dxJointContact::getInfo1( dxJoint::Info1 *info ) |
| 50 |
{ |
| 51 |
// make sure mu's >= 0, then calculate number of constraint rows and number |
| 52 |
// of unbounded rows. |
| 53 |
int m = 1, nub = 0; |
| 54 |
int roll = (contact.surface.mode&dContactRolling)!=0; |
| 55 |
|
| 56 |
if ( contact.surface.mu < 0 ) contact.surface.mu = 0; |
| 57 |
|
| 58 |
// Anisotropic sliding and rolling and spinning friction |
| 59 |
if ( contact.surface.mode & dContactAxisDep ) |
| 60 |
{ |
| 61 |
if ( contact.surface.mu2 < 0 ) contact.surface.mu2 = 0; |
| 62 |
if ( contact.surface.mu > 0 ) m++; |
| 63 |
if ( contact.surface.mu2 > 0 ) m++; |
| 64 |
if ( contact.surface.mu == dInfinity ) nub ++; |
| 65 |
if ( contact.surface.mu2 == dInfinity ) nub ++; |
| 66 |
if (roll) { |
| 67 |
if ( contact.surface.rho < 0 ) contact.surface.rho = 0; |
| 68 |
else m++; |
| 69 |
if ( contact.surface.rho2 < 0 ) contact.surface.rho2 = 0; |
| 70 |
else m++; |
| 71 |
if ( contact.surface.rhoN < 0 ) contact.surface.rhoN = 0; |
| 72 |
else m++; |
| 73 |
|
| 74 |
if ( contact.surface.rho == dInfinity ) nub++; |
| 75 |
if ( contact.surface.rho2 == dInfinity ) nub++; |
| 76 |
if ( contact.surface.rhoN == dInfinity ) nub++; |
| 77 |
} |
| 78 |
} |
| 79 |
else |
| 80 |
{ |
| 81 |
if ( contact.surface.mu > 0 ) m += 2; |
| 82 |
if ( contact.surface.mu == dInfinity ) nub += 2; |
| 83 |
if (roll) { |
| 84 |
if ( contact.surface.rho < 0 ) contact.surface.rho = 0; |
| 85 |
else m+=3; |
| 86 |
if ( contact.surface.rho == dInfinity ) nub += 3; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
the_m = m; |
| 91 |
info->m = m; |
| 92 |
info->nub = nub; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
void |
| 97 |
dxJointContact::getInfo2( dReal worldFPS, dReal worldERP, |
| 98 |
int rowskip, dReal *J1, dReal *J2, |
| 99 |
int pairskip, dReal *pairRhsCfm, dReal *pairLoHi, |
| 100 |
int *findex) |
| 101 |
{ |
| 102 |
enum { |
| 103 |
ROW_NORMAL, |
| 104 |
|
| 105 |
ROW__OPTIONAL_MIN, |
| 106 |
}; |
| 107 |
|
| 108 |
// get normal, with sign adjusted for body1/body2 polarity |
| 109 |
dVector3 normal; |
| 110 |
if ( (flags & dJOINT_REVERSE) != 0 ) { |
| 111 |
dCopyNegatedVector3( normal, contact.geom.normal ); |
| 112 |
} else { |
| 113 |
dCopyVector3( normal, contact.geom.normal ); |
| 114 |
} |
| 115 |
|
| 116 |
// c1,c2 = contact points with respect to body PORs |
| 117 |
dVector3 c1, c2 = {0,0,0}; |
| 118 |
|
| 119 |
dxBody *b0 = node[0].body; |
| 120 |
dSubtractVectors3( c1, contact.geom.pos, b0->posr.pos ); |
| 121 |
|
| 122 |
// set Jacobian for normal |
| 123 |
dCopyVector3( J1 + ROW_NORMAL * rowskip + GI2__JL_MIN, normal ); |
| 124 |
dCalcVectorCross3( J1 + ROW_NORMAL * rowskip + GI2__JA_MIN, c1, normal ); |
| 125 |
|
| 126 |
dxBody *b1 = node[1].body; |
| 127 |
if ( b1 ) { |
| 128 |
dSubtractVectors3(c2, contact.geom.pos, b1->posr.pos); |
| 129 |
dCopyNegatedVector3(J2 + ROW_NORMAL * rowskip + GI2__JL_MIN, normal); |
| 130 |
dCalcVectorCross3( J2 + ROW_NORMAL * rowskip + GI2__JA_MIN, normal, c2 ); //== dCalcVectorCross3( J2 + GI2__JA_MIN, c2, normal ); dNegateVector3( J2 + GI2__JA_MIN ); |
| 131 |
} |
| 132 |
|
| 133 |
const int surface_mode = contact.surface.mode; |
| 134 |
// set right hand side and cfm value for normal |
| 135 |
dReal erp = (surface_mode & dContactSoftERP) != 0 ? contact.surface.soft_erp : worldERP; |
| 136 |
dReal k = worldFPS * erp; |
| 137 |
|
| 138 |
dReal depth = contact.geom.depth - world->contactp.min_depth; |
| 139 |
if ( depth < 0 ) depth = 0; |
| 140 |
|
| 141 |
dReal motionN = (surface_mode & dContactMotionN) != 0 ? contact.surface.motionN : REAL(0.0); |
| 142 |
const dReal pushout = k * depth + motionN; |
| 143 |
// note: this cap should not limit bounce velocity |
| 144 |
const dReal maxvel = world->contactp.max_vel; |
| 145 |
dReal c = pushout > maxvel ? maxvel : pushout; |
| 146 |
|
| 147 |
// deal with bounce |
| 148 |
if ( (surface_mode & dContactBounce) != 0 ) { |
| 149 |
// calculate outgoing velocity (-ve for incoming contact) |
| 150 |
dReal outgoing = dCalcVectorDot3( J1 + ROW_NORMAL * rowskip + GI2__JL_MIN, node[0].body->lvel ) |
| 151 |
+ dCalcVectorDot3( J1 + ROW_NORMAL * rowskip + GI2__JA_MIN, node[0].body->avel ); |
| 152 |
|
| 153 |
if ( b1 ) { |
| 154 |
outgoing += dCalcVectorDot3( J2 + ROW_NORMAL * rowskip + GI2__JL_MIN, node[1].body->lvel ) |
| 155 |
+ dCalcVectorDot3( J2 + ROW_NORMAL * rowskip + GI2__JA_MIN, node[1].body->avel ); |
| 156 |
} |
| 157 |
|
| 158 |
outgoing -= motionN; |
| 159 |
// only apply bounce if the outgoing velocity is greater than the |
| 160 |
// threshold, and if the resulting c[rowNormal] exceeds what we already have. |
| 161 |
if ( contact.surface.bounce_vel >= 0 && |
| 162 |
( -outgoing ) > contact.surface.bounce_vel ) { |
| 163 |
const dReal newc = - contact.surface.bounce * outgoing + motionN; |
| 164 |
if ( newc > c ) c = newc; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
pairRhsCfm[ROW_NORMAL * pairskip + GI2_RHS] = c; |
| 169 |
|
| 170 |
if ( (surface_mode & dContactSoftCFM) != 0 ) { |
| 171 |
pairRhsCfm[ROW_NORMAL * pairskip + GI2_CFM] = contact.surface.soft_cfm; |
| 172 |
} |
| 173 |
|
| 174 |
// set LCP limits for normal |
| 175 |
pairLoHi[ROW_NORMAL * pairskip + GI2_LO] = 0; |
| 176 |
pairLoHi[ROW_NORMAL * pairskip + GI2_HI] = dInfinity; |
| 177 |
|
| 178 |
|
| 179 |
if ( the_m > 1 ) { // if no friction, there is nothing else to do |
| 180 |
// now do jacobian for tangential forces |
| 181 |
dVector3 t1, t2; // two vectors tangential to normal |
| 182 |
|
| 183 |
if ( (surface_mode & dContactFDir1) != 0 ) { // use fdir1 ? |
| 184 |
dCopyVector3( t1, contact.fdir1 ); |
| 185 |
dCalcVectorCross3( t2, normal, t1 ); |
| 186 |
} else { |
| 187 |
dPlaneSpace( normal, t1, t2 ); |
| 188 |
} |
| 189 |
|
| 190 |
int row = ROW__OPTIONAL_MIN; |
| 191 |
int currRowSkip = row * rowskip, currPairSkip = row * pairskip; |
| 192 |
|
| 193 |
// first friction direction |
| 194 |
if ( contact.surface.mu > 0 ) { |
| 195 |
dCopyVector3( J1 + currRowSkip + GI2__JL_MIN, t1 ); |
| 196 |
dCalcVectorCross3( J1 + currRowSkip + GI2__JA_MIN, c1, t1 ); |
| 197 |
|
| 198 |
if ( node[1].body ) { |
| 199 |
dCopyNegatedVector3( J2 + currRowSkip + GI2__JL_MIN, t1); |
| 200 |
dCalcVectorCross3( J2 + currRowSkip + GI2__JA_MIN, t1, c2 ); //== dCalcVectorCross3( J2 + rowskip + GI2__JA_MIN, c2, t1 ); dNegateVector3( J2 + rowskip + GI2__JA_MIN ); |
| 201 |
} |
| 202 |
|
| 203 |
// set right hand side |
| 204 |
if ( (surface_mode & dContactMotion1) != 0 ) { |
| 205 |
pairRhsCfm[currPairSkip + GI2_RHS] = contact.surface.motion1; |
| 206 |
} |
| 207 |
// set slip (constraint force mixing) |
| 208 |
if ( (surface_mode & dContactSlip1) != 0 ) { |
| 209 |
pairRhsCfm[currPairSkip + GI2_CFM] = contact.surface.slip1; |
| 210 |
} |
| 211 |
|
| 212 |
// set LCP bounds and friction index. this depends on the approximation |
| 213 |
// mode |
| 214 |
pairLoHi[currPairSkip + GI2_LO] = -contact.surface.mu; |
| 215 |
pairLoHi[currPairSkip + GI2_HI] = contact.surface.mu; |
| 216 |
|
| 217 |
if ( (surface_mode & dContactApprox1_1) != 0 ) { |
| 218 |
findex[row] = 0; |
| 219 |
} |
| 220 |
|
| 221 |
++row; |
| 222 |
currRowSkip += rowskip; currPairSkip += pairskip; |
| 223 |
} |
| 224 |
|
| 225 |
const dReal mu2 = (surface_mode & dContactMu2) != 0 ? contact.surface.mu2 : contact.surface.mu; |
| 226 |
|
| 227 |
// second friction direction |
| 228 |
if ( mu2 > 0 ) { |
| 229 |
dCopyVector3( J1 + currRowSkip + GI2__JL_MIN, t2 ); |
| 230 |
dCalcVectorCross3( J1 + currRowSkip + GI2__JA_MIN, c1, t2 ); |
| 231 |
|
| 232 |
if ( node[1].body ) { |
| 233 |
dCopyNegatedVector3( J2 + currRowSkip + GI2__JL_MIN, t2 ); |
| 234 |
dCalcVectorCross3( J2 + currRowSkip + GI2__JA_MIN, t2, c2 ); //== dCalcVectorCross3( J2 + currRowSkip + GI2__JA_MIN, c2, t2 ); dNegateVector3( J2 + currRowSkip + GI2__JA_MIN ); |
| 235 |
} |
| 236 |
|
| 237 |
// set right hand side |
| 238 |
if ( (surface_mode & dContactMotion2) != 0 ) { |
| 239 |
pairRhsCfm[currPairSkip + GI2_RHS] = contact.surface.motion2; |
| 240 |
} |
| 241 |
// set slip (constraint force mixing) |
| 242 |
if ( (surface_mode & dContactSlip2) != 0 ) { |
| 243 |
pairRhsCfm[currPairSkip + GI2_CFM] = contact.surface.slip2; |
| 244 |
} |
| 245 |
|
| 246 |
// set LCP bounds and friction index. this depends on the approximation |
| 247 |
// mode |
| 248 |
pairLoHi[currPairSkip + GI2_LO] = -mu2; |
| 249 |
pairLoHi[currPairSkip + GI2_HI] = mu2; |
| 250 |
|
| 251 |
if ( (surface_mode & dContactApprox1_2) != 0 ) { |
| 252 |
findex[row] = 0; |
| 253 |
} |
| 254 |
|
| 255 |
++row; |
| 256 |
currRowSkip += rowskip; currPairSkip += pairskip; |
| 257 |
} |
| 258 |
|
| 259 |
// Handle rolling/spinning friction |
| 260 |
if ( (surface_mode & dContactRolling) != 0 ) { |
| 261 |
// Get the coefficients |
| 262 |
dReal rho[3]; |
| 263 |
rho[0] = contact.surface.rho; |
| 264 |
if ( (surface_mode & dContactAxisDep) != 0 ) { |
| 265 |
rho[1] = contact.surface.rho2; |
| 266 |
rho[2] = contact.surface.rhoN; |
| 267 |
} else { |
| 268 |
rho[1] = rho[0]; |
| 269 |
rho[2] = rho[0]; |
| 270 |
} |
| 271 |
|
| 272 |
const dReal* ax[3]; |
| 273 |
ax[0] = t1; // Rolling around t1 creates movement parallel to t2 |
| 274 |
ax[1] = t2; |
| 275 |
ax[2] = normal; // Spinning axis |
| 276 |
|
| 277 |
// Should we use proportional force? |
| 278 |
bool approx[3]; |
| 279 |
approx[0] = (surface_mode & dContactApprox1_1) != 0; |
| 280 |
approx[1] = (surface_mode & dContactApprox1_2) != 0; |
| 281 |
approx[2] = (surface_mode & dContactApprox1_N) != 0; |
| 282 |
|
| 283 |
for (int i = 0; i != 3; ++i) { |
| 284 |
if (rho[i] > 0) { |
| 285 |
// Set the angular axis |
| 286 |
dCopyVector3(J1 + currRowSkip + GI2__JA_MIN, ax[i]); |
| 287 |
|
| 288 |
if ( b1 ) { |
| 289 |
dCopyNegatedVector3(J2 + currRowSkip + GI2__JA_MIN, ax[i]); |
| 290 |
} |
| 291 |
|
| 292 |
// Set the lcp limits |
| 293 |
pairLoHi[currPairSkip + GI2_LO] = -rho[i]; |
| 294 |
pairLoHi[currPairSkip + GI2_HI] = rho[i]; |
| 295 |
|
| 296 |
// Make limits proportional to normal force |
| 297 |
if (approx[i]) { |
| 298 |
findex[row] = 0; |
| 299 |
} |
| 300 |
|
| 301 |
++row; |
| 302 |
currRowSkip += rowskip; currPairSkip += pairskip; |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
dJointType |
| 310 |
dxJointContact::type() const |
| 311 |
{ |
| 312 |
return dJointTypeContact; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
size_t |
| 317 |
dxJointContact::size() const |
| 318 |
{ |
| 319 |
return sizeof( *this ); |
| 320 |
} |
| 321 |
|