Source

KODE / src / joints / Contact.cpp

Full commit
  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
/*
  This file is part of the KODE.

    KODE Physics Library
    Copyright (C) 2013-2014  Daniel Kohler Osmari

    KODE is free software: you can redistribute it and/or modify it
    under the terms of EITHER:

        * the GNU Lesser General Public License as published by the
          Free Software Foundation, either version 3 of the License,
          or (at your option) any later version.

        * the Apache License, Version 2.0.

    This program 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
    GNU Lesser General Public License and the Apache License for more
    details.

    You should have received a copy of the GNU Lesser General Public
    License along with this program.  If not, see
    <http://www.gnu.org/licenses/>.

    You may obtain a copy of the Apache License at
       http://www.apache.org/licenses/LICENSE-2.0
*/


/*************************************************************************
 * Note: parts of this code is derived from ODE.                         *
 *                                                                       *
 * 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 <algorithm>
#include <iostream>
#include <iomanip>
#include <utility>
#include <tuple>

#include <kode/joints/Contact.hpp>
#include <kode/Body.hpp>
#include <kode/World.hpp>

using std::clog;
using std::endl;

using std::move;
using std::tie;


namespace kode {

    Contact::Contact(Body* b1, Body* b2, const ContactPoint& cpoint) :
        Joint{b1, b2},
        point(cpoint)
    {}


    Contact::Contact(Body& b, const ContactPoint& cpoint) :
        Contact{&b, nullptr, cpoint}
    {}


    Contact::Contact(Body& b1, Body& b2, const ContactPoint& cpoint) :
        Contact{&b1, &b2, cpoint}
    {}


    void
    Contact::setLinearVel(const Vector3& vel) noexcept
    {
        linearVel = vel;
    }


    void
    Contact::setMu(Real mu)
    {
        if (mu < 0)
            throw std::domain_error{"friction coefficient can't be negative"};
        mu1 = mu2 = mu;
    }


    void
    Contact::setMu(Real m1, Real m2)
    {
        if (m1 < 0)
            throw std::domain_error{"first friction coefficient can't be negative"};
        if (m2 < 0)
            throw std::domain_error{"second friction coefficient can't be negative"};

        mu1 = m1;
        mu2 = m2;
    }


    Real
    Contact::getMu1() const noexcept
    {
        return mu1;
    }


    Real
    Contact::getMu2() const noexcept
    {
        return mu2;
    }


    void
    Contact::setRho(Real rho)
    {
        setRho(rho, rho, rho);
    }


    void
    Contact::setRho(Real r, Real rN)
    {
        setRho(r, r, rN);
    }


    void
    Contact::setRho(Real r1, Real r2, Real rN)
    {
        if (r1 < 0)
            throw std::domain_error{"first rolling friction coefficient can't be negative"};
        if (r2 < 0)
            throw std::domain_error{"second rolling friction coefficient can't be negative"};
        if (rN < 0)
            throw std::domain_error{"spinning rolling friction coefficient can't be negative"};
        rho1 = r1;
        rho2 = r2;
        rhoN = rN;
    }


    Real
    Contact::getRho1() const noexcept
    {
        return rho1;
    }


    Real
    Contact::getRho2() const noexcept
    {
        return rho2;
    }


    Real
    Contact::getRhoN() const noexcept
    {
        return rhoN;
    }


    void
    Contact::setSoftERP(Real e)
    {
        if (e < 0)
            throw std::domain_error{"soft erp can't be negative"};
        softERP = e;
        useSoftERP = true;
    }


    void
    Contact::unsetSoftERP() noexcept
    {
        useSoftERP = false;
    }


    Real
    Contact::getSoftERP(const World& world) const noexcept
    {
        return useSoftERP ? softERP : getERP(world);
    }


    void
    Contact::setSoftCFM(Real c)
    {
        if (c < 0)
            throw std::domain_error{"soft cfm can't be negative"};

        softCFM = c;
        useSoftCFM = true;
    }


    void
    Contact::unsetSoftCFM() noexcept
    {
        useSoftCFM = false;
    }


    Real
    Contact::getSoftCFM(const World& world) const noexcept
    {
        return useSoftCFM ? softCFM : world.getCFM();
    }


    void
    Contact::setBounciness(Real ratio)
    {
        // TODO: what could be a domain error for this?
        bounciness = ratio;
    }


    void
    Contact::setMinBounceVel(Real vel)
    {
        if (vel < 0)
            throw std::domain_error{"minimum bounce velocity can't be negative"};
        minBounceVel = vel;
    }


    void
    Contact::setFDir1(const Vector3& dir) noexcept
    {
        fdir1 = dir;
        useFDir1 = true;
    }


    void
    Contact::unsetFDir1() noexcept
    {
        useFDir1 = false;
    }


    void
    Contact::enablePyramidApprox() noexcept
    {
        usePyramid = true;
    }


    void
    Contact::disablePyramidApprox() noexcept
    {
        usePyramid = false;
    }


    void
    Contact::enableSpinPyramidApprox() noexcept
    {
        useSpinPyramid = true;
    }


    void
    Contact::disableSpinPyramidApprox() noexcept
    {
        useSpinPyramid = false;
    }


    void
    Contact::setSlip1(Real s1)
    {
        if (s1 < 0)
            throw std::domain_error{"slip factor 1 should not be negative"};
        useSlip1 = true;
        slip1 = s1;
    }


    void
    Contact::setSlip2(Real s2)
    {
        if (s2 < 0)
            throw std::domain_error{"slip factor 2 should not be negative"};
        useSlip2 = true;
        slip2 = s2;
    }


    void
    Contact::setSlip(Real s)
    {
        setSlip(s, s);
    }


    void
    Contact::setSlip(Real s1, Real s2)
    {
        setSlip1(s1);
        setSlip2(s2);
    }


    Real
    Contact::getSlip1(const World& world) const noexcept
    {
        return useSlip1 ? slip1 : getCFM(world);
    }


    Real
    Contact::getSlip2(const World& world) const noexcept
    {
        return useSlip2 ? slip2 : getCFM(world);
    }


    void
    Contact::setContactPoint(const ContactPoint& cpoint) noexcept
    {
        point = cpoint;
    }


    const ContactPoint&
    Contact::getContactPoint() const noexcept
    {
        return point;
    }


    void
    Contact::updateConstraints(World& world) noexcept
    {
        // at least the non-penetration constraint, which is bounded
        numConstraints = 1;

        if (mu1 > 0)
            numConstraints++;
        if (mu2 > 0)
            numConstraints++;
        if (rho1 > 0)
            numConstraints++;
        if (rho2 > 0)
            numConstraints++;
        if (rhoN > 0)
            numConstraints++;

        clearConstraints();

#if 0
        if (usePyramid) {
            if (mu1 < Math::Infinity)
                numBounded++;
            if (mu2 < Math::Infinity)
                numBounded++;
            if (rho1 < Math::Infinity)
                numBounded++;
            if (rho2 < Math::Infinity)
                numBounded++;
        }
        if (useSpinPyramid) {
            if (rhoN < Math::Infinity)
                numBounded++;
        }
#endif

        // computations are based on bodies' center of mass
        const Vector3 center1 = body1 ? body1->getCoM() : Vector3::Zero();
        const Vector3 center2 = body2 ? body2->getCoM() : Vector3::Zero();

        const Vector3 relAnchor1 = body1 ? point.position - center1 : Vector3::Zero();
        const Vector3 relAnchor2 = body2 ? point.position - center2 : Vector3::Zero();

        Constraint& normCon = constraints[0];

        if (body1) {
            normCon.lin1 = point.normal;
            normCon.ang1 = cross(relAnchor1, point.normal);
        }

        if (body2) {
            normCon.lin2 = -point.normal;
            normCon.ang2 = cross(point.normal, relAnchor2); // note the negation of the result
        }

        normCon.cfm = getSoftCFM(world);

        const Real k = world.getFPS() * getSoftERP(world);

        const Real depthError = std::max<Real>(point.depth - world.getMinContactDepth(), 0);
        //clog << "depth error: " << depthError << endl;

        // TODO: incorporate angular motion into this
        const Real normalMotion = -dot(linearVel, point.normal);

        const Real pushout = std::min<Real>(k * depthError + normalMotion, world.getMaxContactVel());
        //clog << "pushout: " << pushout << endl;

        Real bounce = 0;
        if (bounciness > 0) {
            // TODO: rewrite those
            const Real out1 = body1 ?
                              dot(point.normal, body1->getLinearVel())
                              +
                              dot(normCon.ang1, body1->getAngularVel())
                              : 0;
            const Real out2 = body2 ?
                              -dot(point.normal, body2->getLinearVel())
                              +
                              dot(normCon.ang2, body2->getAngularVel())
                              : 0;

            const Real ingoing = normalMotion - out1 - out2;
            //clog << "ingoing: " << ingoing << endl;
            if (ingoing > minBounceVel)
                bounce = bounciness * ingoing + normalMotion;
            //clog << "bounce: " << bounce << endl;
        }

        normCon.c = std::max(pushout, bounce);

        normCon.low = 0;
        normCon.high = Math::Infinity;
        normCon.findex = 0;
        normCon.lambda = 0; // never warm-start


        if (numConstraints == 1)
            return;

        Vector3 tangent1, tangent2;
        if (useFDir1) {
            tangent1 = fdir1;
            tangent2 = cross(point.normal, fdir1);
        } else {
            // TODO: what is a reasonable guess for fdir1?
            tie(tangent1, tangent2) = planeSpace(point.normal);
        }

        // now handle tangent constraints

        // first friction direction (constraint[1])
        unsigned thisRow = 1;

        if (mu1 > 0) {
            Constraint& tan1Con = constraints[thisRow];

            if (body1) {
                tan1Con.lin1 = tangent1;
                tan1Con.ang1 = cross(relAnchor1, tangent1);
            }

            if (body2) {
                tan1Con.lin2 = -tangent1;
                tan1Con.ang2 = cross(relAnchor2, -tangent1);
            }

            // set right hand side
            tan1Con.c = dot(linearVel, tangent1);

            // set LCP bounds and friction index. this depends on the approximation
            // mode
            tan1Con.low  = -mu1;
            tan1Con.high =  mu1;

            tan1Con.findex = usePyramid ? thisRow : 0;
            tan1Con.cfm = getSlip1(world);
            tan1Con.lambda = 0;
            ++thisRow;
        }

        // second friction direction (constraint[thisRow])
        if (mu2 > 0) {
            Constraint& tan2Con = constraints[thisRow];

            if (body1) {
                tan2Con.lin1 = tangent2;
                tan2Con.ang1 = cross(relAnchor1, tangent2);
            }

            if (body2) {
                tan2Con.lin2 = -tangent2;
                tan2Con.ang2 = cross(relAnchor2, -tangent2);
            }

            // set right hand side
            tan2Con.c = dot(linearVel, tangent2);

            // set LCP bounds and friction index. this depends on the approximation
            // mode
            tan2Con.low  = -mu2;
            tan2Con.high =  mu2;

            tan2Con.findex = usePyramid ? thisRow : 0;
            tan2Con.cfm = getSlip2(world);
            tan2Con.lambda = 0;
            ++thisRow;
        }

        // Handle rolling/spinning friction
        if (rho1 > 0) {
            Constraint& con = constraints[thisRow];
            // Set the angular axis
            if (body1)
                con.ang1 =  tangent2;
            if (body2)
                con.ang2 = -tangent2;

            con.c = 0;
            // Set the lcp limits
            con.low = -rho1;
            con.high = rho1;
            con.findex = usePyramid ? thisRow : 0;
            con.cfm = getCFM(world);
            con.lambda = 0;
            ++thisRow;
        }
        if (rho2 > 0) {
            Constraint& con = constraints[thisRow];
            // Set the angular axis
            if (body1)
                con.ang1 =  tangent1;
            if (body2)
                con.ang2 = -tangent1;

            con.c = 0;
            // Set the lcp limits
            con.low = -rho2;
            con.high = rho2;
            con.findex = usePyramid ? thisRow : 0;
            con.cfm = getCFM(world);
            con.lambda = 0;
            ++thisRow;
        }
        if (rhoN > 0) {
            Constraint& con = constraints[thisRow];
            // Set the angular axis
            if (body1)
                con.ang1 =  point.normal;
            if (body2)
                con.ang2 = -point.normal;

            con.c = 0;
            // Set the lcp limits
            con.low = -rhoN;
            con.high = rhoN;
            con.findex = useSpinPyramid ? thisRow : 0;
            con.cfm = getCFM(world);
            con.lambda = 0;
            ++thisRow;
        }
    }

}