summaryrefslogtreecommitdiff
path: root/libs/ode-0.16.1/ode/src/joints/amotor.cpp
blob: aa30c7643c655eba5064ee30c87a9e92f0774ee8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/*************************************************************************
 *                                                                       *
 * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith.       *
 * All rights reserved.  Email: russ@q12.org   Web: www.q12.org          *
 *                                                                       *
 * This library is free software; you can redistribute it and/or         *
 * modify it under the terms of EITHER:                                  *
 *   (1) The GNU Lesser General Public License as published by the Free  *
 *       Software Foundation; either version 2.1 of the License, or (at  *
 *       your option) any later version. The text of the GNU Lesser      *
 *       General Public License is included with this library in the     *
 *       file LICENSE.TXT.                                               *
 *   (2) The BSD-style license that is included with this library in     *
 *       the file LICENSE-BSD.TXT.                                       *
 *                                                                       *
 * This library is distributed in the hope that it will be useful,       *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
 * LICENSE.TXT and LICENSE-BSD.TXT for more details.                     *
 *                                                                       *
 *************************************************************************/


#include <ode/odeconfig.h>
#include "config.h"
#include "common.h"
#include "amotor.h"
#include "joint_internal.h"
#include "odeou.h"


/*extern */
void dJointSetAMotorNumAxes(dJointID j, int num)
{
    dxJointAMotor* joint = (dxJointAMotor*)j;
    dAASSERT(joint != NULL);
    dAASSERT(dIN_RANGE(num, dSA__MIN, dSA__MAX + 1));
    checktype(joint, AMotor);

    num = dCLAMP(num, dSA__MIN, dSA__MAX);

    joint->setNumAxes(num);
}

/*extern */
void dJointSetAMotorAxis(dJointID j, int anum, int rel/*=dJointBodyRelativity*/, 
    dReal x, dReal y, dReal z)
{
    dxJointAMotor* joint = (dxJointAMotor*)j;
    dAASSERT(joint != NULL);
    dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX));
    dAASSERT(dIN_RANGE(rel, dJBR__MIN, dJBR__MAX));
    checktype(joint, AMotor);

    anum = dCLAMP(anum, dSA__MIN, dSA__MAX - 1);

    joint->setAxisValue(anum, (dJointBodyRelativity)rel, x, y, z);
}

/*extern */
void dJointSetAMotorAngle(dJointID j, int anum, dReal angle)
{
    dxJointAMotor* joint = (dxJointAMotor*)j;
    dAASSERT(joint != NULL);
    dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX));
    checktype(joint, AMotor);

    anum = dCLAMP(anum, dSA__MIN, dSA__MAX - 1);

    joint->setAngleValue(anum, angle);
}

/*extern */
void dJointSetAMotorParam(dJointID j, int parameter, dReal value)
{
    dxJointAMotor* joint = (dxJointAMotor*)j;
    dAASSERT(joint != NULL);
    checktype(joint, AMotor);

    int anum = parameter >> 8;
    dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX));

    anum = dCLAMP(anum, dSA__MIN, dSA__MAX - 1);

    int limotParam = parameter & 0xff;
    joint->setLimotParameter(anum, limotParam, value);
}

/*extern */
void dJointSetAMotorMode(dJointID j, int mode)
{
    dxJointAMotor* joint = (dxJointAMotor*)j;
    dAASSERT(joint != NULL);
    checktype(joint, AMotor);

    joint->setOperationMode(mode);
}

/*extern */
int dJointGetAMotorNumAxes(dJointID j)
{
    dxJointAMotor* joint = (dxJointAMotor*)j;
    dAASSERT(joint != NULL);
    checktype(joint, AMotor);

    return joint->getNumAxes();
}

/*extern */
void dJointGetAMotorAxis(dJointID j, int anum, dVector3 result)
{
    dxJointAMotor* joint = (dxJointAMotor*)j;
    dAASSERT(joint != NULL);
    dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX));
    checktype(joint, AMotor);

    anum = dCLAMP(anum, dSA__MIN, dSA__MAX - 1);

    joint->getAxisValue(result, anum);
}

/*extern */
int dJointGetAMotorAxisRel(dJointID j, int anum)
{
    dxJointAMotor* joint = (dxJointAMotor*)j;
    dAASSERT(joint != NULL);
    dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX));
    checktype(joint, AMotor);

    anum = dCLAMP(anum, dSA__MIN, dSA__MAX - 1);

    int result = joint->getAxisBodyRelativity(anum);
    return result;
}

/*extern */
dReal dJointGetAMotorAngle(dJointID j, int anum)
{
    dxJointAMotor* joint = (dxJointAMotor*)j;
    dAASSERT(joint != NULL);
    dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX));
    checktype(joint, AMotor);

    anum = dCLAMP(anum, dSA__MIN, dSA__MAX - 1);

    dReal result = joint->getAngleValue(anum);
    return result;
}

/*extern */
dReal dJointGetAMotorAngleRate(dJointID j, int anum)
{
    dxJointAMotor* joint = (dxJointAMotor*)j;
    dAASSERT(joint != NULL);
    dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX));
    checktype(joint, AMotor);

    anum = dCLAMP(anum, dSA__MIN, dSA__MAX - 1);

    dReal result = joint->calculateAngleRate(anum);
    return result;
}

/*extern */
dReal dJointGetAMotorParam(dJointID j, int parameter)
{
    dxJointAMotor* joint = (dxJointAMotor*)j;
    dAASSERT(joint != NULL);
    checktype(joint, AMotor);

    int anum = parameter >> 8;
    dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX));

    anum = dCLAMP(anum, dSA__MIN, dSA__MAX - 1);

    int limotParam = parameter & 0xff;
    dReal result = joint->getLimotParameter(anum, limotParam);
    return result;
}

/*extern */
int dJointGetAMotorMode(dJointID j)
{
    dxJointAMotor* joint = (dxJointAMotor*)j;
    dAASSERT(joint != NULL);
    checktype(joint, AMotor);

    int result = joint->getOperationMode();
    return result;
}

/*extern */
void dJointAddAMotorTorques(dJointID j, dReal torque1, dReal torque2, dReal torque3)
{
    dxJointAMotor* joint = (dxJointAMotor*)j;
    dAASSERT(joint != NULL);
    checktype(joint, AMotor);

    joint->addTorques(torque1, torque2, torque3);
}


//****************************************************************************

BEGIN_NAMESPACE_OU();
template<>
const dJointBodyRelativity CEnumUnsortedElementArray<dSpaceAxis, dSA__MAX, dJointBodyRelativity, 0x160703D5>::m_aetElementArray[] =
{
    dJBR_BODY1, // dSA_X,
    dJBR_GLOBAL, // dSA_Y,
    dJBR_BODY2, // dSA_Z,
};
END_NAMESPACE_OU();
static const CEnumUnsortedElementArray<dSpaceAxis, dSA__MAX, dJointBodyRelativity, 0x160703D5> g_abrEulerAxisAllowedBodyRelativities;

static inline 
dSpaceAxis EncodeJointConnectedBodyEulerAxis(dJointConnectedBody cbBodyIndex)
{
    dSASSERT(dJCB__MAX == 2); 
    
    return cbBodyIndex == dJCB_FIRST_BODY ? dSA_X : dSA_Z;
}

static inline 
dSpaceAxis EncodeOtherEulerAxis(dSpaceAxis saOneAxis)
{
    dIASSERT(saOneAxis == EncodeJointConnectedBodyEulerAxis(dJCB_FIRST_BODY) || saOneAxis == EncodeJointConnectedBodyEulerAxis(dJCB_SECOND_BODY)); 
    dSASSERT(dJCB__MAX == 2); 
    
    return (dSpaceAxis)(dSA_X + dSA_Z - saOneAxis);
}


//****************************************************************************
// angular motor

dxJointAMotor::dxJointAMotor(dxWorld *w) :
    dxJointAMotor_Parent(w),
    m_mode(dAMotorUser),
    m_num(0)
{
    std::fill(m_rel, m_rel + dARRAY_SIZE(m_rel), dJBR__DEFAULT);
    { for (int i = 0; i != dARRAY_SIZE(m_axis); ++i) { dZeroVector3(m_axis[i]); } }
    { for (int i = 0; i != dARRAY_SIZE(m_references); ++i) { dZeroVector3(m_references[i]); } }
    std::fill(m_angle, m_angle + dARRAY_SIZE(m_angle), REAL(0.0));
    { for (int i = 0; i != dARRAY_SIZE(m_limot); ++i) { m_limot[i].init(w); } }
}


/*virtual */
dxJointAMotor::~dxJointAMotor()
{
    // The virtual destructor
}


/*virtual */
void dxJointAMotor::getSureMaxInfo(SureMaxInfo* info)
{
    info->max_m = m_num;
}

/*virtual */
void dxJointAMotor::getInfo1(dxJoint::Info1 *info)
{
    info->m = 0;
    info->nub = 0;

    // compute the axes and angles, if in Euler mode
    if (m_mode == dAMotorEuler)
    {
        dVector3 ax[dSA__MAX];
        computeGlobalAxes(ax);
        computeEulerAngles(ax);
    }

    // see if we're powered or at a joint limit for each axis
    const unsigned num = m_num;
    for (unsigned i = 0; i != num; ++i)
    {
        if (m_limot[i].testRotationalLimit(m_angle[i]) 
            || m_limot[i].fmax > 0)
        {
            info->m++;
        }
    }
}

/*virtual */
void dxJointAMotor::getInfo2(dReal worldFPS, dReal /*worldERP*/, 
    int rowskip, dReal *J1, dReal *J2,
    int pairskip, dReal *pairRhsCfm, dReal *pairLoHi, 
    int *findex)
{
    // compute the axes (if not global)
    dVector3 ax[dSA__MAX];
    computeGlobalAxes(ax);

    // in Euler angle mode we do not actually constrain the angular velocity
    // along the axes axis[0] and axis[2] (although we do use axis[1]) :
    //
    //    to get   constrain w2-w1 along  ...not
    //    ------   ---------------------  ------
    //    d(angle[0])/dt = 0 ax[1] x ax[2]   ax[0]
    //    d(angle[1])/dt = 0 ax[1]
    //    d(angle[2])/dt = 0 ax[0] x ax[1]   ax[2]
    //
    // constraining w2-w1 along an axis 'a' means that a'*(w2-w1)=0.
    // to prove the result for angle[0], write the expression for angle[0] from
    // GetInfo1 then take the derivative. to prove this for angle[2] it is
    // easier to take the Euler rate expression for d(angle[2])/dt with respect
    // to the components of w and set that to 0.

    dVector3 *axptr[dSA__MAX];
    for (int j = dSA__MIN; j != dSA__MAX; ++j) { axptr[j] = &ax[j]; }

    dVector3 ax0_cross_ax1;
    dVector3 ax1_cross_ax2;
    
    if (m_mode == dAMotorEuler) 
    {
        dCalcVectorCross3(ax0_cross_ax1, ax[dSA_X], ax[dSA_Y]);
        axptr[dSA_Z] = &ax0_cross_ax1;
        dCalcVectorCross3(ax1_cross_ax2, ax[dSA_Y], ax[dSA_Z]);
        axptr[dSA_X] = &ax1_cross_ax2;
    }

    sizeint rowTotalSkip = 0, pairTotalSkip = 0;
    
    const unsigned num = m_num;
    for (unsigned i = 0; i != num; ++i) 
    {
        if (m_limot[i].addLimot(this, worldFPS, J1 + rowTotalSkip, J2 + rowTotalSkip, pairRhsCfm + pairTotalSkip, pairLoHi + pairTotalSkip, *(axptr[i]), 1)) 
        {
            rowTotalSkip += rowskip;
            pairTotalSkip += pairskip;
        }
    }
}

/*virtual */
dJointType dxJointAMotor::type() const
{
    return dJointTypeAMotor;
}

/*virtual */
sizeint dxJointAMotor::size() const
{
    return sizeof(*this);
}


void dxJointAMotor::setOperationMode(int mode)
{
    m_mode = mode;

    if (mode == dAMotorEuler)
    {
        m_num = dSA__MAX;
        setEulerReferenceVectors();
    }
}


void dxJointAMotor::setNumAxes(unsigned num)
{
    if (m_mode == dAMotorEuler)
    {
        m_num = dSA__MAX;
    }
    else
    {
        m_num = num;
    }
}


dJointBodyRelativity dxJointAMotor::getAxisBodyRelativity(unsigned anum) const
{
    dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX));

    dJointBodyRelativity rel = m_rel[anum];
    if (dJBREncodeBodyRelativityStatus(rel) && GetIsJointReverse())
    {
        rel = dJBRSwapBodyRelativity(rel); // turns 1 into 2, 2 into 1
    }

    return rel;
}


void dxJointAMotor::setAxisValue(unsigned anum, dJointBodyRelativity rel, 
    dReal x, dReal y, dReal z)
{
    dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX));
    dAASSERT(m_mode != dAMotorEuler || !dJBREncodeBodyRelativityStatus(rel) || rel == g_abrEulerAxisAllowedBodyRelativities.Encode((dSpaceAxis)anum));

    // x,y,z is always in global coordinates regardless of rel, so we may have
    // to convert it to be relative to a body
    dVector3 r;
    dAssignVector3(r, x, y, z);

    // adjust rel to match the internal body order
    if (dJBREncodeBodyRelativityStatus(rel) && GetIsJointReverse())
    {
        rel = dJBRSwapBodyRelativity(rel); // turns 1 into 2, 2, into 1
    }

    m_rel[anum] = rel;

    bool assigned = false;

    if (dJBREncodeBodyRelativityStatus(rel))
    {
        if (rel == dJBR_BODY1)
        {
            dMultiply1_331(m_axis[anum], this->node[0].body->posr.R, r);
            assigned = true;
        }
        // rel == 2
        else if (this->node[1].body != NULL)
        {
            dIASSERT(rel == dJBR_BODY2);

            dMultiply1_331(m_axis[anum], this->node[1].body->posr.R, r);
            assigned = true;
        }
    }
    
    if (!assigned)
    {
        dCopyVector3(m_axis[anum], r); 
    }
    
    dNormalize3(m_axis[anum]);
    
    if (m_mode == dAMotorEuler) 
    {
        setEulerReferenceVectors();
    }
}

void dxJointAMotor::getAxisValue(dVector3 result, unsigned anum) const
{
    dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX));

    switch (m_mode)
    {
        case dAMotorUser:
        {
            doGetUserAxis(result, anum);
            break;
        }

        case dAMotorEuler:
        {
            doGetEulerAxis(result, anum);
            break;
        }

        default:
        {
            dIASSERT(false);
            break;
        }
    } 
}


void dxJointAMotor::doGetUserAxis(dVector3 result, unsigned anum) const
{
    bool retrieved = false;

    if (dJBREncodeBodyRelativityStatus(m_rel[anum])) 
    {
        if (m_rel[anum] == dJBR_BODY1)
        {
            dMultiply0_331(result, this->node[0].body->posr.R, m_axis[anum]);
            retrieved = true;
        }
        else if (this->node[1].body != NULL)
        {
            dMultiply0_331(result, this->node[1].body->posr.R, m_axis[anum]);
            retrieved = true;
        }
    }

    if (!retrieved)
    {
        dCopyVector3(result, m_axis[anum]);
    }
}

void dxJointAMotor::doGetEulerAxis(dVector3 result, unsigned anum) const
{
    // If we're in Euler mode, joint->axis[1] doesn't
    // have anything sensible in it.  So don't just return
    // that, find the actual effective axis.
    // Likewise, the actual axis of rotation for the
    // the other axes is different from what's stored.
    dVector3 axes[dSA__MAX];
    computeGlobalAxes(axes);

    if (anum == dSA_Y) 
    {
        dCopyVector3(result, axes[dSA_Y]);
    } 
    else if (anum < dSA_Y) // Comparing against the same constant lets compiler reuse EFLAGS register for another conditional jump
    {
        dSASSERT(dSA_X < dSA_Y); // Otherwise the condition above is incorrect
        dIASSERT(anum == dSA_X);

        // This won't be unit length in general,
        // but it's what's used in getInfo2
        // This may be why things freak out as
        // the body-relative axes get close to each other.
        dCalcVectorCross3(result, axes[dSA_Y], axes[dSA_Z]);
    } 
    else 
    {
        dSASSERT(dSA_Z > dSA_Y); // Otherwise the condition above is incorrect
        dIASSERT(anum == dSA_Z);

        // Same problem as above.
        dCalcVectorCross3(result, axes[dSA_X], axes[dSA_Y]);
    }
}


void dxJointAMotor::setAngleValue(unsigned anum, dReal angle)
{
    dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX));
    dAASSERT(m_mode == dAMotorUser); // This only works for the dAMotorUser

    if (m_mode == dAMotorUser)
    {
        m_angle[anum] = angle;
    }
}


dReal dxJointAMotor::calculateAngleRate(unsigned anum) const
{
    dAASSERT(dIN_RANGE(anum, dSA__MIN, dSA__MAX));
    dAASSERT(this->node[0].body != NULL); // Don't call for angle rate before the joint is set up

    dVector3 axis;
    getAxisValue(axis, anum);

    // NOTE!
    // For reverse joints, the rate is negated at the function exit to create swapped bodies effect
    dReal rate = dDOT(axis, this->node[0].body->avel);

    if (this->node[1].body != NULL) 
    {
        rate -= dDOT(axis, this->node[1].body->avel);
    }

    // Negating the rate for reverse joints creates an effect of body swapping
    dReal result = !GetIsJointReverse() ? rate : -rate;
    return result;
}


void dxJointAMotor::addTorques(dReal torque1, dReal torque2, dReal torque3)
{
    unsigned num = getNumAxes();
    dAASSERT(dIN_RANGE(num, dSA__MIN, dSA__MAX + 1));

    dVector3 sum;
    dVector3 torqueVector;
    dVector3 axes[dSA__MAX];


    if (num != dSA__MIN)
    {
        computeGlobalAxes(axes);

        if (!GetIsJointReverse())
        {
            dAssignVector3(torqueVector, torque1, torque2, torque3);
        }
        else
        {
            // Negating torques creates an effect of swapped bodies later
            dAssignVector3(torqueVector, -torque1, -torque2, -torque3);
        }
    }

    switch (num)
    {
        case dSA_Z + 1:
        {
            dAddThreeScaledVectors3(sum, axes[dSA_Z], axes[dSA_Y], axes[dSA_X], torqueVector[dSA_Z], torqueVector[dSA_Y], torqueVector[dSA_X]);
            break;
        }

        case dSA_Y + 1:
        {
            dAddScaledVectors3(sum, axes[dSA_Y], axes[dSA_X], torqueVector[dSA_Y], torqueVector[dSA_X]);
            break;
        }

        case dSA_X + 1:
        {
            dCopyScaledVector3(sum, axes[dSA_X], torqueVector[dSA_X]);
            break;
        }
        
        default:
        {
            dSASSERT(dSA_Z > dSA_Y); // Otherwise the addends order needs to be switched
            dSASSERT(dSA_Y > dSA_X);
            
            // Do nothing
            break;
        }
    }

    if (num != dSA__MIN)
    {
        dAASSERT(this->node[0].body != NULL); // Don't add torques unless you set the joint up first!

        // NOTE!
        // For reverse joints, the torqueVector negated at function entry produces the effect of swapped bodies
        dBodyAddTorque(this->node[0].body, sum[dV3E_X], sum[dV3E_Y], sum[dV3E_Z]);
        
        if (this->node[1].body != NULL)
        {
            dBodyAddTorque(this->node[1].body, -sum[dV3E_X], -sum[dV3E_Y], -sum[dV3E_Z]);
        }
    }
}


// compute the 3 axes in global coordinates
void dxJointAMotor::computeGlobalAxes(dVector3 ax[dSA__MAX]) const
{
    switch (m_mode)
    {
        case dAMotorUser:
        {
            doComputeGlobalUserAxes(ax);
            break;
        }

        case dAMotorEuler:
        {
            doComputeGlobalEulerAxes(ax);
            break;
        }

        default:
        {
            dIASSERT(false);
            break;
        }
    } 
}

void dxJointAMotor::doComputeGlobalUserAxes(dVector3 ax[dSA__MAX]) const
{
    unsigned num = m_num;
    for (unsigned i = 0; i != num; ++i)
    {
        bool assigned = false;

        if (m_rel[i] == dJBR_BODY1)
        {
            // relative to b1
            dMultiply0_331(ax[i], this->node[0].body->posr.R, m_axis[i]);
            assigned = true;
        }
        else if (m_rel[i] == dJBR_BODY2)
        {
            // relative to b2
            if (this->node[1].body != NULL)
            {
                dMultiply0_331(ax[i], this->node[1].body->posr.R, m_axis[i]);
                assigned = true;
            }
        }

        if (!assigned)
        {
            // global - just copy it
            dCopyVector3(ax[i], m_axis[i]);
        }
    }
}

void dxJointAMotor::doComputeGlobalEulerAxes(dVector3 ax[dSA__MAX]) const
{
    // special handling for Euler mode
    
    dSpaceAxis firstBodyAxis = BuildFirstBodyEulerAxis();
    dMultiply0_331(ax[firstBodyAxis], this->node[0].body->posr.R, m_axis[firstBodyAxis]);

    dSpaceAxis secondBodyAxis = EncodeOtherEulerAxis(firstBodyAxis);

    if (this->node[1].body != NULL)
    {
        dMultiply0_331(ax[secondBodyAxis], this->node[1].body->posr.R, m_axis[secondBodyAxis]);
    }
    else
    {
        dCopyVector3(ax[secondBodyAxis], m_axis[secondBodyAxis]);
    }

    dCalcVectorCross3(ax[dSA_Y], ax[dSA_Z], ax[dSA_X]);
    dNormalize3(ax[dSA_Y]);
}


void dxJointAMotor::computeEulerAngles(dVector3 ax[dSA__MAX])
{
    // assumptions:
    //   global axes already calculated --> ax
    //   axis[0] is relative to body 1 --> global ax[0]
    //   axis[2] is relative to body 2 --> global ax[2]
    //   ax[1] = ax[2] x ax[0]
    //   original ax[0] and ax[2] are perpendicular
    //   reference1 is perpendicular to ax[0] (in body 1 frame)
    //   reference2 is perpendicular to ax[2] (in body 2 frame)
    //   all ax[] and reference vectors are unit length

    // calculate references in global frame
    dVector3 refs[dJCB__MAX];
    dMultiply0_331(refs[dJCB_FIRST_BODY], this->node[0].body->posr.R, m_references[dJCB_FIRST_BODY]);

    if (this->node[1].body != NULL)
    {
        dMultiply0_331(refs[dJCB_SECOND_BODY], this->node[1].body->posr.R, m_references[dJCB_SECOND_BODY]);
    }
    else
    {
        dCopyVector3(refs[dJCB_SECOND_BODY], m_references[dJCB_SECOND_BODY]);
    }


    // get q perpendicular to both ax[0] and ref1, get first euler angle
    dVector3 q;
    dJointConnectedBody firstAxisBody = BuildFirstEulerAxisBody();

    dCalcVectorCross3(q, ax[dSA_X], refs[firstAxisBody]);
    m_angle[dSA_X] = -dAtan2(dCalcVectorDot3(ax[dSA_Z], q), dCalcVectorDot3(ax[dSA_Z], refs[firstAxisBody]));

    // get q perpendicular to both ax[0] and ax[1], get second euler angle
    dCalcVectorCross3(q, ax[dSA_X], ax[dSA_Y]);
    m_angle[dSA_Y] = -dAtan2(dCalcVectorDot3(ax[dSA_Z], ax[dSA_X]), dCalcVectorDot3(ax[dSA_Z], q));

    dJointConnectedBody secondAxisBody = EncodeJointOtherConnectedBody(firstAxisBody);

    // get q perpendicular to both ax[1] and ax[2], get third euler angle
    dCalcVectorCross3(q, ax[dSA_Y], ax[dSA_Z]);
    m_angle[dSA_Z] = -dAtan2(dCalcVectorDot3(refs[secondAxisBody], ax[dSA_Y]), dCalcVectorDot3(refs[secondAxisBody], q));
}


// set the reference vectors as follows:
//   * reference1 = current axis[2] relative to body 1
//   * reference2 = current axis[0] relative to body 2
// this assumes that:
//    * axis[0] is relative to body 1
//    * axis[2] is relative to body 2

void dxJointAMotor::setEulerReferenceVectors()
{
    if (/*this->node[0].body != NULL && */this->node[1].body != NULL)
    {
        dIASSERT(this->node[0].body != NULL);

        dVector3 r;  // axis[2] and axis[0] in global coordinates

        dSpaceAxis firstBodyAxis = BuildFirstBodyEulerAxis();
        dMultiply0_331(r, this->node[0].body->posr.R, m_axis[firstBodyAxis]);
        dMultiply1_331(m_references[dJCB_SECOND_BODY], this->node[1].body->posr.R, r);

        dSpaceAxis secondBodyAxis = EncodeOtherEulerAxis(firstBodyAxis);
        dMultiply0_331(r, this->node[1].body->posr.R, m_axis[secondBodyAxis]);
        dMultiply1_331(m_references[dJCB_FIRST_BODY], this->node[0].body->posr.R, r);
    } 
    else 
    {
        // We want to handle angular motors attached to passive geoms
        // Replace missing node.R with identity
        if (this->node[0].body != NULL) 
        {
            dSpaceAxis firstBodyAxis = BuildFirstBodyEulerAxis();
            dMultiply0_331(m_references[dJCB_SECOND_BODY], this->node[0].body->posr.R, m_axis[firstBodyAxis]);

            dSpaceAxis secondBodyAxis = EncodeOtherEulerAxis(firstBodyAxis);
            dMultiply1_331(m_references[dJCB_FIRST_BODY], this->node[0].body->posr.R, m_axis[secondBodyAxis]);
        } 
    }
}

/*inline */
dSpaceAxis dxJointAMotor::BuildFirstBodyEulerAxis() const
{
    return EncodeJointConnectedBodyEulerAxis(BuildFirstEulerAxisBody());
}

/*inline */
dJointConnectedBody dxJointAMotor::BuildFirstEulerAxisBody() const
{
    return !GetIsJointReverse() ? dJCB_FIRST_BODY : dJCB_SECOND_BODY;
}