| 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 "common.h" |
| 27 |
#include "amotor.h" |
| 28 |
#include "joint_internal.h" |
| 29 |
#include "odeou.h" |
| 30 |
|
| 31 |
|
| 32 |
/*extern */ |
| 33 |
void dJointSetAMotorNumAxes(dJointID j, int num) |
| 34 |
{ |
| 35 |
dxJointAMotor* joint = (dxJointAMotor*)j; |
| 36 |
dAASSERT(joint != NULL); |
| 37 |
dAASSERT(dIN_RANGE(num, dSA__MIN, dSA__MAX + 1)); |
| 38 |
checktype(joint, AMotor); |
| 39 |
|
| 40 |
num = dCLAMP(num, dSA__MIN, dSA__MAX); |
| 41 |
|
| 42 |
joint->setNumAxes(num); |
| 43 |
} |
| 44 |
|
| 45 |
/*extern */ |
| 46 |
void dJointSetAMotorAxis(dJointID j, int anum, int rel/*=dJointBodyRelativity*/, |
| 47 |
dReal x, dReal y, dReal z) |
| 48 |
{ |
| 49 |
dxJointAMotor* joint = (dxJointAMotor*)j; |
| 50 |
dAASSERT(joint != NULL); |
| 51 |
dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX)); |
| 52 |
dAASSERT(dIN_RANGE(rel, dJBR__MIN, dJBR__MAX)); |
| 53 |
checktype(joint, AMotor); |
| 54 |
|
| 55 |
anum = dCLAMP(anum, dSA__MIN, dSA__MAX - 1); |
| 56 |
|
| 57 |
joint->setAxisValue(anum, (dJointBodyRelativity)rel, x, y, z); |
| 58 |
} |
| 59 |
|
| 60 |
/*extern */ |
| 61 |
void dJointSetAMotorAngle(dJointID j, int anum, dReal angle) |
| 62 |
{ |
| 63 |
dxJointAMotor* joint = (dxJointAMotor*)j; |
| 64 |
dAASSERT(joint != NULL); |
| 65 |
dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX)); |
| 66 |
checktype(joint, AMotor); |
| 67 |
|
| 68 |
anum = dCLAMP(anum, dSA__MIN, dSA__MAX - 1); |
| 69 |
|
| 70 |
joint->setAngleValue(anum, angle); |
| 71 |
} |
| 72 |
|
| 73 |
/*extern */ |
| 74 |
void dJointSetAMotorParam(dJointID j, int parameter, dReal value) |
| 75 |
{ |
| 76 |
dxJointAMotor* joint = (dxJointAMotor*)j; |
| 77 |
dAASSERT(joint != NULL); |
| 78 |
checktype(joint, AMotor); |
| 79 |
|
| 80 |
int anum = parameter >> 8; |
| 81 |
dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX)); |
| 82 |
|
| 83 |
anum = dCLAMP(anum, dSA__MIN, dSA__MAX - 1); |
| 84 |
|
| 85 |
int limotParam = parameter & 0xff; |
| 86 |
joint->setLimotParameter(anum, limotParam, value); |
| 87 |
} |
| 88 |
|
| 89 |
/*extern */ |
| 90 |
void dJointSetAMotorMode(dJointID j, int mode) |
| 91 |
{ |
| 92 |
dxJointAMotor* joint = (dxJointAMotor*)j; |
| 93 |
dAASSERT(joint != NULL); |
| 94 |
checktype(joint, AMotor); |
| 95 |
|
| 96 |
joint->setOperationMode(mode); |
| 97 |
} |
| 98 |
|
| 99 |
/*extern */ |
| 100 |
int dJointGetAMotorNumAxes(dJointID j) |
| 101 |
{ |
| 102 |
dxJointAMotor* joint = (dxJointAMotor*)j; |
| 103 |
dAASSERT(joint != NULL); |
| 104 |
checktype(joint, AMotor); |
| 105 |
|
| 106 |
return joint->getNumAxes(); |
| 107 |
} |
| 108 |
|
| 109 |
/*extern */ |
| 110 |
void dJointGetAMotorAxis(dJointID j, int anum, dVector3 result) |
| 111 |
{ |
| 112 |
dxJointAMotor* joint = (dxJointAMotor*)j; |
| 113 |
dAASSERT(joint != NULL); |
| 114 |
dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX)); |
| 115 |
checktype(joint, AMotor); |
| 116 |
|
| 117 |
anum = dCLAMP(anum, dSA__MIN, dSA__MAX - 1); |
| 118 |
|
| 119 |
joint->getAxisValue(result, anum); |
| 120 |
} |
| 121 |
|
| 122 |
/*extern */ |
| 123 |
int dJointGetAMotorAxisRel(dJointID j, int anum) |
| 124 |
{ |
| 125 |
dxJointAMotor* joint = (dxJointAMotor*)j; |
| 126 |
dAASSERT(joint != NULL); |
| 127 |
dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX)); |
| 128 |
checktype(joint, AMotor); |
| 129 |
|
| 130 |
anum = dCLAMP(anum, dSA__MIN, dSA__MAX - 1); |
| 131 |
|
| 132 |
int result = joint->getAxisBodyRelativity(anum); |
| 133 |
return result; |
| 134 |
} |
| 135 |
|
| 136 |
/*extern */ |
| 137 |
dReal dJointGetAMotorAngle(dJointID j, int anum) |
| 138 |
{ |
| 139 |
dxJointAMotor* joint = (dxJointAMotor*)j; |
| 140 |
dAASSERT(joint != NULL); |
| 141 |
dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX)); |
| 142 |
checktype(joint, AMotor); |
| 143 |
|
| 144 |
anum = dCLAMP(anum, dSA__MIN, dSA__MAX - 1); |
| 145 |
|
| 146 |
dReal result = joint->getAngleValue(anum); |
| 147 |
return result; |
| 148 |
} |
| 149 |
|
| 150 |
/*extern */ |
| 151 |
dReal dJointGetAMotorAngleRate(dJointID j, int anum) |
| 152 |
{ |
| 153 |
dxJointAMotor* joint = (dxJointAMotor*)j; |
| 154 |
dAASSERT(joint != NULL); |
| 155 |
dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX)); |
| 156 |
checktype(joint, AMotor); |
| 157 |
|
| 158 |
anum = dCLAMP(anum, dSA__MIN, dSA__MAX - 1); |
| 159 |
|
| 160 |
dReal result = joint->calculateAngleRate(anum); |
| 161 |
return result; |
| 162 |
} |
| 163 |
|
| 164 |
/*extern */ |
| 165 |
dReal dJointGetAMotorParam(dJointID j, int parameter) |
| 166 |
{ |
| 167 |
dxJointAMotor* joint = (dxJointAMotor*)j; |
| 168 |
dAASSERT(joint != NULL); |
| 169 |
checktype(joint, AMotor); |
| 170 |
|
| 171 |
int anum = parameter >> 8; |
| 172 |
dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX)); |
| 173 |
|
| 174 |
anum = dCLAMP(anum, dSA__MIN, dSA__MAX - 1); |
| 175 |
|
| 176 |
int limotParam = parameter & 0xff; |
| 177 |
dReal result = joint->getLimotParameter(anum, limotParam); |
| 178 |
return result; |
| 179 |
} |
| 180 |
|
| 181 |
/*extern */ |
| 182 |
int dJointGetAMotorMode(dJointID j) |
| 183 |
{ |
| 184 |
dxJointAMotor* joint = (dxJointAMotor*)j; |
| 185 |
dAASSERT(joint != NULL); |
| 186 |
checktype(joint, AMotor); |
| 187 |
|
| 188 |
int result = joint->getOperationMode(); |
| 189 |
return result; |
| 190 |
} |
| 191 |
|
| 192 |
/*extern */ |
| 193 |
void dJointAddAMotorTorques(dJointID j, dReal torque1, dReal torque2, dReal torque3) |
| 194 |
{ |
| 195 |
dxJointAMotor* joint = (dxJointAMotor*)j; |
| 196 |
dAASSERT(joint != NULL); |
| 197 |
checktype(joint, AMotor); |
| 198 |
|
| 199 |
joint->addTorques(torque1, torque2, torque3); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
//**************************************************************************** |
| 204 |
|
| 205 |
BEGIN_NAMESPACE_OU(); |
| 206 |
template<> |
| 207 |
const dJointBodyRelativity CEnumUnsortedElementArray<dSpaceAxis, dSA__MAX, dJointBodyRelativity, 0x160703D5>::m_aetElementArray[] = |
| 208 |
{ |
| 209 |
dJBR_BODY1, // dSA_X, |
| 210 |
dJBR_GLOBAL, // dSA_Y, |
| 211 |
dJBR_BODY2, // dSA_Z, |
| 212 |
}; |
| 213 |
END_NAMESPACE_OU(); |
| 214 |
static const CEnumUnsortedElementArray<dSpaceAxis, dSA__MAX, dJointBodyRelativity, 0x160703D5> g_abrEulerAxisAllowedBodyRelativities; |
| 215 |
|
| 216 |
static inline |
| 217 |
dSpaceAxis EncodeJointConnectedBodyEulerAxis(dJointConnectedBody cbBodyIndex) |
| 218 |
{ |
| 219 |
dSASSERT(dJCB__MAX == 2); |
| 220 |
|
| 221 |
return cbBodyIndex == dJCB_FIRST_BODY ? dSA_X : dSA_Z; |
| 222 |
} |
| 223 |
|
| 224 |
static inline |
| 225 |
dSpaceAxis EncodeOtherEulerAxis(dSpaceAxis saOneAxis) |
| 226 |
{ |
| 227 |
dIASSERT(saOneAxis == EncodeJointConnectedBodyEulerAxis(dJCB_FIRST_BODY) || saOneAxis == EncodeJointConnectedBodyEulerAxis(dJCB_SECOND_BODY)); |
| 228 |
dSASSERT(dJCB__MAX == 2); |
| 229 |
|
| 230 |
return (dSpaceAxis)(dSA_X + dSA_Z - saOneAxis); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
//**************************************************************************** |
| 235 |
// angular motor |
| 236 |
|
| 237 |
dxJointAMotor::dxJointAMotor(dxWorld *w) : |
| 238 |
dxJointAMotor_Parent(w), |
| 239 |
m_mode(dAMotorUser), |
| 240 |
m_num(0) |
| 241 |
{ |
| 242 |
std::fill(m_rel, m_rel + dARRAY_SIZE(m_rel), dJBR__DEFAULT); |
| 243 |
{ for (int i = 0; i != dARRAY_SIZE(m_axis); ++i) { dZeroVector3(m_axis[i]); } } |
| 244 |
{ for (int i = 0; i != dARRAY_SIZE(m_references); ++i) { dZeroVector3(m_references[i]); } } |
| 245 |
std::fill(m_angle, m_angle + dARRAY_SIZE(m_angle), REAL(0.0)); |
| 246 |
{ for (int i = 0; i != dARRAY_SIZE(m_limot); ++i) { m_limot[i].init(w); } } |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
/*virtual */ |
| 251 |
dxJointAMotor::~dxJointAMotor() |
| 252 |
{ |
| 253 |
// The virtual destructor |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
/*virtual */ |
| 258 |
void dxJointAMotor::getSureMaxInfo(SureMaxInfo* info) |
| 259 |
{ |
| 260 |
info->max_m = m_num; |
| 261 |
} |
| 262 |
|
| 263 |
/*virtual */ |
| 264 |
void dxJointAMotor::getInfo1(dxJoint::Info1 *info) |
| 265 |
{ |
| 266 |
info->m = 0; |
| 267 |
info->nub = 0; |
| 268 |
|
| 269 |
// compute the axes and angles, if in Euler mode |
| 270 |
if (m_mode == dAMotorEuler) |
| 271 |
{ |
| 272 |
dVector3 ax[dSA__MAX]; |
| 273 |
computeGlobalAxes(ax); |
| 274 |
computeEulerAngles(ax); |
| 275 |
} |
| 276 |
|
| 277 |
// see if we're powered or at a joint limit for each axis |
| 278 |
const unsigned num = m_num; |
| 279 |
for (unsigned i = 0; i != num; ++i) |
| 280 |
{ |
| 281 |
if (m_limot[i].testRotationalLimit(m_angle[i]) |
| 282 |
|| m_limot[i].fmax > 0) |
| 283 |
{ |
| 284 |
info->m++; |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
/*virtual */ |
| 290 |
void dxJointAMotor::getInfo2(dReal worldFPS, dReal /*worldERP*/, |
| 291 |
int rowskip, dReal *J1, dReal *J2, |
| 292 |
int pairskip, dReal *pairRhsCfm, dReal *pairLoHi, |
| 293 |
int *findex) |
| 294 |
{ |
| 295 |
// compute the axes (if not global) |
| 296 |
dVector3 ax[dSA__MAX]; |
| 297 |
computeGlobalAxes(ax); |
| 298 |
|
| 299 |
// in Euler angle mode we do not actually constrain the angular velocity |
| 300 |
// along the axes axis[0] and axis[2] (although we do use axis[1]) : |
| 301 |
// |
| 302 |
// to get constrain w2-w1 along ...not |
| 303 |
// ------ --------------------- ------ |
| 304 |
// d(angle[0])/dt = 0 ax[1] x ax[2] ax[0] |
| 305 |
// d(angle[1])/dt = 0 ax[1] |
| 306 |
// d(angle[2])/dt = 0 ax[0] x ax[1] ax[2] |
| 307 |
// |
| 308 |
// constraining w2-w1 along an axis 'a' means that a'*(w2-w1)=0. |
| 309 |
// to prove the result for angle[0], write the expression for angle[0] from |
| 310 |
// GetInfo1 then take the derivative. to prove this for angle[2] it is |
| 311 |
// easier to take the Euler rate expression for d(angle[2])/dt with respect |
| 312 |
// to the components of w and set that to 0. |
| 313 |
|
| 314 |
dVector3 *axptr[dSA__MAX]; |
| 315 |
for (int j = dSA__MIN; j != dSA__MAX; ++j) { axptr[j] = &ax[j]; } |
| 316 |
|
| 317 |
dVector3 ax0_cross_ax1; |
| 318 |
dVector3 ax1_cross_ax2; |
| 319 |
|
| 320 |
if (m_mode == dAMotorEuler) |
| 321 |
{ |
| 322 |
dCalcVectorCross3(ax0_cross_ax1, ax[dSA_X], ax[dSA_Y]); |
| 323 |
axptr[dSA_Z] = &ax0_cross_ax1; |
| 324 |
dCalcVectorCross3(ax1_cross_ax2, ax[dSA_Y], ax[dSA_Z]); |
| 325 |
axptr[dSA_X] = &ax1_cross_ax2; |
| 326 |
} |
| 327 |
|
| 328 |
size_t rowTotalSkip = 0, pairTotalSkip = 0; |
| 329 |
|
| 330 |
const unsigned num = m_num; |
| 331 |
for (unsigned i = 0; i != num; ++i) |
| 332 |
{ |
| 333 |
if (m_limot[i].addLimot(this, worldFPS, J1 + rowTotalSkip, J2 + rowTotalSkip, pairRhsCfm + pairTotalSkip, pairLoHi + pairTotalSkip, *(axptr[i]), 1)) |
| 334 |
{ |
| 335 |
rowTotalSkip += rowskip; |
| 336 |
pairTotalSkip += pairskip; |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
/*virtual */ |
| 342 |
dJointType dxJointAMotor::type() const |
| 343 |
{ |
| 344 |
return dJointTypeAMotor; |
| 345 |
} |
| 346 |
|
| 347 |
/*virtual */ |
| 348 |
size_t dxJointAMotor::size() const |
| 349 |
{ |
| 350 |
return sizeof(*this); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
void dxJointAMotor::setOperationMode(int mode) |
| 355 |
{ |
| 356 |
m_mode = mode; |
| 357 |
|
| 358 |
if (mode == dAMotorEuler) |
| 359 |
{ |
| 360 |
m_num = dSA__MAX; |
| 361 |
setEulerReferenceVectors(); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
void dxJointAMotor::setNumAxes(unsigned num) |
| 367 |
{ |
| 368 |
if (m_mode == dAMotorEuler) |
| 369 |
{ |
| 370 |
m_num = dSA__MAX; |
| 371 |
} |
| 372 |
else |
| 373 |
{ |
| 374 |
m_num = num; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
dJointBodyRelativity dxJointAMotor::getAxisBodyRelativity(unsigned anum) const |
| 380 |
{ |
| 381 |
dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX)); |
| 382 |
|
| 383 |
dJointBodyRelativity rel = m_rel[anum]; |
| 384 |
if (dJBREncodeBodyRelativityStatus(rel) && GetIsJointReverse()) |
| 385 |
{ |
| 386 |
rel = dJBRSwapBodyRelativity(rel); // turns 1 into 2, 2 into 1 |
| 387 |
} |
| 388 |
|
| 389 |
return rel; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
void dxJointAMotor::setAxisValue(unsigned anum, dJointBodyRelativity rel, |
| 394 |
dReal x, dReal y, dReal z) |
| 395 |
{ |
| 396 |
dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX)); |
| 397 |
dAASSERT(m_mode != dAMotorEuler || !dJBREncodeBodyRelativityStatus(rel) || rel == g_abrEulerAxisAllowedBodyRelativities.Encode((dSpaceAxis)anum)); |
| 398 |
|
| 399 |
// x,y,z is always in global coordinates regardless of rel, so we may have |
| 400 |
// to convert it to be relative to a body |
| 401 |
dVector3 r; |
| 402 |
dAssignVector3(r, x, y, z); |
| 403 |
|
| 404 |
// adjust rel to match the internal body order |
| 405 |
if (dJBREncodeBodyRelativityStatus(rel) && GetIsJointReverse()) |
| 406 |
{ |
| 407 |
rel = dJBRSwapBodyRelativity(rel); // turns 1 into 2, 2, into 1 |
| 408 |
} |
| 409 |
|
| 410 |
m_rel[anum] = rel; |
| 411 |
|
| 412 |
bool assigned = false; |
| 413 |
|
| 414 |
if (dJBREncodeBodyRelativityStatus(rel)) |
| 415 |
{ |
| 416 |
if (rel == dJBR_BODY1) |
| 417 |
{ |
| 418 |
dMultiply1_331(m_axis[anum], this->node[0].body->posr.R, r); |
| 419 |
assigned = true; |
| 420 |
} |
| 421 |
// rel == 2 |
| 422 |
else if (this->node[1].body != NULL) |
| 423 |
{ |
| 424 |
dIASSERT(rel == dJBR_BODY2); |
| 425 |
|
| 426 |
dMultiply1_331(m_axis[anum], this->node[1].body->posr.R, r); |
| 427 |
assigned = true; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
if (!assigned) |
| 432 |
{ |
| 433 |
dCopyVector3(m_axis[anum], r); |
| 434 |
} |
| 435 |
|
| 436 |
dNormalize3(m_axis[anum]); |
| 437 |
|
| 438 |
if (m_mode == dAMotorEuler) |
| 439 |
{ |
| 440 |
setEulerReferenceVectors(); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
void dxJointAMotor::getAxisValue(dVector3 result, unsigned anum) const |
| 445 |
{ |
| 446 |
dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX)); |
| 447 |
|
| 448 |
switch (m_mode) |
| 449 |
{ |
| 450 |
case dAMotorUser: |
| 451 |
{ |
| 452 |
doGetUserAxis(result, anum); |
| 453 |
break; |
| 454 |
} |
| 455 |
|
| 456 |
case dAMotorEuler: |
| 457 |
{ |
| 458 |
doGetEulerAxis(result, anum); |
| 459 |
break; |
| 460 |
} |
| 461 |
|
| 462 |
default: |
| 463 |
{ |
| 464 |
dIASSERT(false); |
| 465 |
break; |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
void dxJointAMotor::doGetUserAxis(dVector3 result, unsigned anum) const |
| 472 |
{ |
| 473 |
bool retrieved = false; |
| 474 |
|
| 475 |
if (dJBREncodeBodyRelativityStatus(m_rel[anum])) |
| 476 |
{ |
| 477 |
if (m_rel[anum] == dJBR_BODY1) |
| 478 |
{ |
| 479 |
dMultiply0_331(result, this->node[0].body->posr.R, m_axis[anum]); |
| 480 |
retrieved = true; |
| 481 |
} |
| 482 |
else if (this->node[1].body != NULL) |
| 483 |
{ |
| 484 |
dMultiply0_331(result, this->node[1].body->posr.R, m_axis[anum]); |
| 485 |
retrieved = true; |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
if (!retrieved) |
| 490 |
{ |
| 491 |
dCopyVector3(result, m_axis[anum]); |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
void dxJointAMotor::doGetEulerAxis(dVector3 result, unsigned anum) const |
| 496 |
{ |
| 497 |
// If we're in Euler mode, joint->axis[1] doesn't |
| 498 |
// have anything sensible in it. So don't just return |
| 499 |
// that, find the actual effective axis. |
| 500 |
// Likewise, the actual axis of rotation for the |
| 501 |
// the other axes is different from what's stored. |
| 502 |
dVector3 axes[dSA__MAX]; |
| 503 |
computeGlobalAxes(axes); |
| 504 |
|
| 505 |
if (anum == dSA_Y) |
| 506 |
{ |
| 507 |
dCopyVector3(result, axes[dSA_Y]); |
| 508 |
} |
| 509 |
else if (anum < dSA_Y) // Comparing against the same constant lets compiler reuse EFLAGS register for another conditional jump |
| 510 |
{ |
| 511 |
dSASSERT(dSA_X < dSA_Y); // Otherwise the condition above is incorrect |
| 512 |
dIASSERT(anum == dSA_X); |
| 513 |
|
| 514 |
// This won't be unit length in general, |
| 515 |
// but it's what's used in getInfo2 |
| 516 |
// This may be why things freak out as |
| 517 |
// the body-relative axes get close to each other. |
| 518 |
dCalcVectorCross3(result, axes[dSA_Y], axes[dSA_Z]); |
| 519 |
} |
| 520 |
else |
| 521 |
{ |
| 522 |
dSASSERT(dSA_Z > dSA_Y); // Otherwise the condition above is incorrect |
| 523 |
dIASSERT(anum == dSA_Z); |
| 524 |
|
| 525 |
// Same problem as above. |
| 526 |
dCalcVectorCross3(result, axes[dSA_X], axes[dSA_Y]); |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
void dxJointAMotor::setAngleValue(unsigned anum, dReal angle) |
| 532 |
{ |
| 533 |
dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX)); |
| 534 |
dAASSERT(m_mode == dAMotorUser); // This only works for the dAMotorUser |
| 535 |
|
| 536 |
if (m_mode == dAMotorUser) |
| 537 |
{ |
| 538 |
m_angle[anum] = angle; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
dReal dxJointAMotor::calculateAngleRate(unsigned anum) const |
| 544 |
{ |
| 545 |
dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX)); |
| 546 |
dAASSERT(this->node[0].body != NULL); // Don't call for angle rate before the joint is set up |
| 547 |
|
| 548 |
dVector3 axis; |
| 549 |
getAxisValue(axis, anum); |
| 550 |
|
| 551 |
// NOTE! |
| 552 |
// For reverse joints, the rate is negated at the function exit to create swapped bodies effect |
| 553 |
dReal rate = dDOT(axis, this->node[0].body->avel); |
| 554 |
|
| 555 |
if (this->node[1].body != NULL) |
| 556 |
{ |
| 557 |
rate -= dDOT(axis, this->node[1].body->avel); |
| 558 |
} |
| 559 |
|
| 560 |
// Negating the rate for reverse joints creates an effect of body swapping |
| 561 |
dReal result = !GetIsJointReverse() ? rate : -rate; |
| 562 |
return result; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
void dxJointAMotor::addTorques(dReal torque1, dReal torque2, dReal torque3) |
| 567 |
{ |
| 568 |
unsigned num = getNumAxes(); |
| 569 |
dAASSERT(dIN_RANGE(num, dSA__MIN, dSA__MAX + 1)); |
| 570 |
|
| 571 |
dVector3 sum; |
| 572 |
dVector3 torqueVector; |
| 573 |
dVector3 axes[dSA__MAX]; |
| 574 |
|
| 575 |
|
| 576 |
if (num != dSA__MIN) |
| 577 |
{ |
| 578 |
computeGlobalAxes(axes); |
| 579 |
|
| 580 |
if (!GetIsJointReverse()) |
| 581 |
{ |
| 582 |
dAssignVector3(torqueVector, torque1, torque2, torque3); |
| 583 |
} |
| 584 |
else |
| 585 |
{ |
| 586 |
// Negating torques creates an effect of swapped bodies later |
| 587 |
dAssignVector3(torqueVector, -torque1, -torque2, -torque3); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
switch (num) |
| 592 |
{ |
| 593 |
case dSA_Z + 1: |
| 594 |
{ |
| 595 |
dAddThreeScaledVectors3(sum, axes[dSA_Z], axes[dSA_Y], axes[dSA_X], torqueVector[dSA_Z], torqueVector[dSA_Y], torqueVector[dSA_X]); |
| 596 |
break; |
| 597 |
} |
| 598 |
|
| 599 |
case dSA_Y + 1: |
| 600 |
{ |
| 601 |
dAddScaledVectors3(sum, axes[dSA_Y], axes[dSA_X], torqueVector[dSA_Y], torqueVector[dSA_X]); |
| 602 |
break; |
| 603 |
} |
| 604 |
|
| 605 |
case dSA_X + 1: |
| 606 |
{ |
| 607 |
dCopyScaledVector3(sum, axes[dSA_X], torqueVector[dSA_X]); |
| 608 |
break; |
| 609 |
} |
| 610 |
|
| 611 |
default: |
| 612 |
{ |
| 613 |
dSASSERT(dSA_Z > dSA_Y); // Otherwise the addends order needs to be switched |
| 614 |
dSASSERT(dSA_Y > dSA_X); |
| 615 |
|
| 616 |
// Do nothing |
| 617 |
break; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
if (num != dSA__MIN) |
| 622 |
{ |
| 623 |
dAASSERT(this->node[0].body != NULL); // Don't add torques unless you set the joint up first! |
| 624 |
|
| 625 |
// NOTE! |
| 626 |
// For reverse joints, the torqueVector negated at function entry produces the effect of swapped bodies |
| 627 |
dBodyAddTorque(this->node[0].body, sum[dV3E_X], sum[dV3E_Y], sum[dV3E_Z]); |
| 628 |
|
| 629 |
if (this->node[1].body != NULL) |
| 630 |
{ |
| 631 |
dBodyAddTorque(this->node[1].body, -sum[dV3E_X], -sum[dV3E_Y], -sum[dV3E_Z]); |
| 632 |
} |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
// compute the 3 axes in global coordinates |
| 638 |
void dxJointAMotor::computeGlobalAxes(dVector3 ax[dSA__MAX]) const |
| 639 |
{ |
| 640 |
switch (m_mode) |
| 641 |
{ |
| 642 |
case dAMotorUser: |
| 643 |
{ |
| 644 |
doComputeGlobalUserAxes(ax); |
| 645 |
break; |
| 646 |
} |
| 647 |
|
| 648 |
case dAMotorEuler: |
| 649 |
{ |
| 650 |
doComputeGlobalEulerAxes(ax); |
| 651 |
break; |
| 652 |
} |
| 653 |
|
| 654 |
default: |
| 655 |
{ |
| 656 |
dIASSERT(false); |
| 657 |
break; |
| 658 |
} |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
void dxJointAMotor::doComputeGlobalUserAxes(dVector3 ax[dSA__MAX]) const |
| 663 |
{ |
| 664 |
unsigned num = m_num; |
| 665 |
for (unsigned i = 0; i != num; ++i) |
| 666 |
{ |
| 667 |
bool assigned = false; |
| 668 |
|
| 669 |
if (m_rel[i] == dJBR_BODY1) |
| 670 |
{ |
| 671 |
// relative to b1 |
| 672 |
dMultiply0_331(ax[i], this->node[0].body->posr.R, m_axis[i]); |
| 673 |
assigned = true; |
| 674 |
} |
| 675 |
else if (m_rel[i] == dJBR_BODY2) |
| 676 |
{ |
| 677 |
// relative to b2 |
| 678 |
if (this->node[1].body != NULL) |
| 679 |
{ |
| 680 |
dMultiply0_331(ax[i], this->node[1].body->posr.R, m_axis[i]); |
| 681 |
assigned = true; |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
if (!assigned) |
| 686 |
{ |
| 687 |
// global - just copy it |
| 688 |
dCopyVector3(ax[i], m_axis[i]); |
| 689 |
} |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
void dxJointAMotor::doComputeGlobalEulerAxes(dVector3 ax[dSA__MAX]) const |
| 694 |
{ |
| 695 |
// special handling for Euler mode |
| 696 |
|
| 697 |
dSpaceAxis firstBodyAxis = BuildFirstBodyEulerAxis(); |
| 698 |
dMultiply0_331(ax[firstBodyAxis], this->node[0].body->posr.R, m_axis[firstBodyAxis]); |
| 699 |
|
| 700 |
dSpaceAxis secondBodyAxis = EncodeOtherEulerAxis(firstBodyAxis); |
| 701 |
|
| 702 |
if (this->node[1].body != NULL) |
| 703 |
{ |
| 704 |
dMultiply0_331(ax[secondBodyAxis], this->node[1].body->posr.R, m_axis[secondBodyAxis]); |
| 705 |
} |
| 706 |
else |
| 707 |
{ |
| 708 |
dCopyVector3(ax[secondBodyAxis], m_axis[secondBodyAxis]); |
| 709 |
} |
| 710 |
|
| 711 |
dCalcVectorCross3(ax[dSA_Y], ax[dSA_Z], ax[dSA_X]); |
| 712 |
dNormalize3(ax[dSA_Y]); |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
void dxJointAMotor::computeEulerAngles(dVector3 ax[dSA__MAX]) |
| 717 |
{ |
| 718 |
// assumptions: |
| 719 |
// global axes already calculated --> ax |
| 720 |
// axis[0] is relative to body 1 --> global ax[0] |
| 721 |
// axis[2] is relative to body 2 --> global ax[2] |
| 722 |
// ax[1] = ax[2] x ax[0] |
| 723 |
// original ax[0] and ax[2] are perpendicular |
| 724 |
// reference1 is perpendicular to ax[0] (in body 1 frame) |
| 725 |
// reference2 is perpendicular to ax[2] (in body 2 frame) |
| 726 |
// all ax[] and reference vectors are unit length |
| 727 |
|
| 728 |
// calculate references in global frame |
| 729 |
dVector3 refs[dJCB__MAX]; |
| 730 |
dMultiply0_331(refs[dJCB_FIRST_BODY], this->node[0].body->posr.R, m_references[dJCB_FIRST_BODY]); |
| 731 |
|
| 732 |
if (this->node[1].body != NULL) |
| 733 |
{ |
| 734 |
dMultiply0_331(refs[dJCB_SECOND_BODY], this->node[1].body->posr.R, m_references[dJCB_SECOND_BODY]); |
| 735 |
} |
| 736 |
else |
| 737 |
{ |
| 738 |
dCopyVector3(refs[dJCB_SECOND_BODY], m_references[dJCB_SECOND_BODY]); |
| 739 |
} |
| 740 |
|
| 741 |
|
| 742 |
// get q perpendicular to both ax[0] and ref1, get first euler angle |
| 743 |
dVector3 q; |
| 744 |
dJointConnectedBody firstAxisBody = BuildFirstEulerAxisBody(); |
| 745 |
|
| 746 |
dCalcVectorCross3(q, ax[dSA_X], refs[firstAxisBody]); |
| 747 |
m_angle[dSA_X] = -dAtan2(dCalcVectorDot3(ax[dSA_Z], q), dCalcVectorDot3(ax[dSA_Z], refs[firstAxisBody])); |
| 748 |
|
| 749 |
// get q perpendicular to both ax[0] and ax[1], get second euler angle |
| 750 |
dCalcVectorCross3(q, ax[dSA_X], ax[dSA_Y]); |
| 751 |
m_angle[dSA_Y] = -dAtan2(dCalcVectorDot3(ax[dSA_Z], ax[dSA_X]), dCalcVectorDot3(ax[dSA_Z], q)); |
| 752 |
|
| 753 |
dJointConnectedBody secondAxisBody = EncodeJointOtherConnectedBody(firstAxisBody); |
| 754 |
|
| 755 |
// get q perpendicular to both ax[1] and ax[2], get third euler angle |
| 756 |
dCalcVectorCross3(q, ax[dSA_Y], ax[dSA_Z]); |
| 757 |
m_angle[dSA_Z] = -dAtan2(dCalcVectorDot3(refs[secondAxisBody], ax[dSA_Y]), dCalcVectorDot3(refs[secondAxisBody], q)); |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
// set the reference vectors as follows: |
| 762 |
// * reference1 = current axis[2] relative to body 1 |
| 763 |
// * reference2 = current axis[0] relative to body 2 |
| 764 |
// this assumes that: |
| 765 |
// * axis[0] is relative to body 1 |
| 766 |
// * axis[2] is relative to body 2 |
| 767 |
|
| 768 |
void dxJointAMotor::setEulerReferenceVectors() |
| 769 |
{ |
| 770 |
if (/*this->node[0].body != NULL && */this->node[1].body != NULL) |
| 771 |
{ |
| 772 |
dIASSERT(this->node[0].body != NULL); |
| 773 |
|
| 774 |
dVector3 r; // axis[2] and axis[0] in global coordinates |
| 775 |
|
| 776 |
dSpaceAxis firstBodyAxis = BuildFirstBodyEulerAxis(); |
| 777 |
dMultiply0_331(r, this->node[0].body->posr.R, m_axis[firstBodyAxis]); |
| 778 |
dMultiply1_331(m_references[dJCB_SECOND_BODY], this->node[1].body->posr.R, r); |
| 779 |
|
| 780 |
dSpaceAxis secondBodyAxis = EncodeOtherEulerAxis(firstBodyAxis); |
| 781 |
dMultiply0_331(r, this->node[1].body->posr.R, m_axis[secondBodyAxis]); |
| 782 |
dMultiply1_331(m_references[dJCB_FIRST_BODY], this->node[0].body->posr.R, r); |
| 783 |
} |
| 784 |
else |
| 785 |
{ |
| 786 |
// We want to handle angular motors attached to passive geoms |
| 787 |
// Replace missing node.R with identity |
| 788 |
if (this->node[0].body != NULL) |
| 789 |
{ |
| 790 |
dSpaceAxis firstBodyAxis = BuildFirstBodyEulerAxis(); |
| 791 |
dMultiply0_331(m_references[dJCB_SECOND_BODY], this->node[0].body->posr.R, m_axis[firstBodyAxis]); |
| 792 |
|
| 793 |
dSpaceAxis secondBodyAxis = EncodeOtherEulerAxis(firstBodyAxis); |
| 794 |
dMultiply1_331(m_references[dJCB_FIRST_BODY], this->node[0].body->posr.R, m_axis[secondBodyAxis]); |
| 795 |
} |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
/*inline */ |
| 800 |
dSpaceAxis dxJointAMotor::BuildFirstBodyEulerAxis() const |
| 801 |
{ |
| 802 |
return EncodeJointConnectedBodyEulerAxis(BuildFirstEulerAxisBody()); |
| 803 |
} |
| 804 |
|
| 805 |
/*inline */ |
| 806 |
dJointConnectedBody dxJointAMotor::BuildFirstEulerAxisBody() const |
| 807 |
{ |
| 808 |
return !GetIsJointReverse() ? dJCB_FIRST_BODY : dJCB_SECOND_BODY; |
| 809 |
} |
| 810 |
|