|
| 1 | +/* |
| 2 | + * Copyright (c) 2011-2025, The DART development contributors |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * The list of contributors can be found at: |
| 6 | + * https://github.com/dartsim/dart/blob/main/LICENSE |
| 7 | + * |
| 8 | + * This file is provided under the following "BSD-style" License: |
| 9 | + * Redistribution and use in source and binary forms, with or |
| 10 | + * without modification, are permitted provided that the following |
| 11 | + * conditions are met: |
| 12 | + * * Redistributions of source code must retain the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer. |
| 14 | + * * Redistributions in binary form must reproduce the above |
| 15 | + * copyright notice, this list of conditions and the following |
| 16 | + * disclaimer in the documentation and/or other materials provided |
| 17 | + * with the distribution. |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 19 | + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 20 | + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 23 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 26 | + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 27 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 29 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | + * POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | + |
| 33 | +#include "helpers/dynamics_helpers.hpp" |
| 34 | + |
| 35 | +#include <dart/simulation/World.hpp> |
| 36 | + |
| 37 | +#include <dart/constraint/BoxedLcpConstraintSolver.hpp> |
| 38 | +#include <dart/constraint/DantzigBoxedLcpSolver.hpp> |
| 39 | + |
| 40 | +#include <dart/collision/bullet/BulletCollisionDetector.hpp> |
| 41 | + |
| 42 | +#include <dart/dynamics/Skeleton.hpp> |
| 43 | + |
| 44 | +#include <gtest/gtest.h> |
| 45 | + |
| 46 | +#include <limits> |
| 47 | +#include <memory> |
| 48 | +#include <vector> |
| 49 | + |
| 50 | +using namespace dart; |
| 51 | +using dart::simulation::World; |
| 52 | + |
| 53 | +namespace { |
| 54 | + |
| 55 | +constexpr double kBoxSide = 0.4; |
| 56 | +constexpr double kBoxHeight = 0.5; |
| 57 | +constexpr std::size_t kNumBoxes = 10; |
| 58 | +constexpr double kTimeStep = 0.001; |
| 59 | +constexpr int kNumSteps = 2000; |
| 60 | + |
| 61 | +//============================================================================== |
| 62 | +std::vector<dynamics::SkeletonPtr> createStack(World& world) |
| 63 | +{ |
| 64 | + // Ground |
| 65 | + auto ground = createBox({5.0, 5.0, 0.4}, {0.0, 0.0, -0.2}); |
| 66 | + ground->setMobile(false); |
| 67 | + world.addSkeleton(ground); |
| 68 | + |
| 69 | + // Boxes |
| 70 | + std::vector<dynamics::SkeletonPtr> boxes; |
| 71 | + boxes.reserve(kNumBoxes); |
| 72 | + |
| 73 | + for (std::size_t i = 0; i < kNumBoxes; ++i) { |
| 74 | + const double centerZ |
| 75 | + = kBoxHeight * 0.5 + static_cast<double>(i) * kBoxHeight; |
| 76 | + auto box = createBox({kBoxSide, kBoxSide, kBoxHeight}, {0.0, 0.0, centerZ}); |
| 77 | + world.addSkeleton(box); |
| 78 | + boxes.push_back(std::move(box)); |
| 79 | + } |
| 80 | + |
| 81 | + return boxes; |
| 82 | +} |
| 83 | + |
| 84 | +} // namespace |
| 85 | + |
| 86 | +//============================================================================== |
| 87 | +TEST(Issue867, BulletBoxStackingStaysStable) |
| 88 | +{ |
| 89 | + auto world = World::create(); |
| 90 | + world->setTimeStep(kTimeStep); |
| 91 | + |
| 92 | + auto lcpSolver = std::make_shared<constraint::DantzigBoxedLcpSolver>(); |
| 93 | + auto solver |
| 94 | + = std::make_unique<constraint::BoxedLcpConstraintSolver>(lcpSolver); |
| 95 | + solver->setCollisionDetector(collision::BulletCollisionDetector::create()); |
| 96 | + world->setConstraintSolver(std::move(solver)); |
| 97 | + |
| 98 | + auto boxes = createStack(*world); |
| 99 | + ASSERT_EQ(boxes.size(), kNumBoxes); |
| 100 | + |
| 101 | + const double expectedTopCenter |
| 102 | + = kBoxHeight * (static_cast<double>(kNumBoxes) - 0.5); |
| 103 | + double maxObservedTopZ = -std::numeric_limits<double>::infinity(); |
| 104 | + double finalKineticEnergy = 0.0; |
| 105 | + |
| 106 | + for (int step = 0; step < kNumSteps; ++step) { |
| 107 | + world->step(); |
| 108 | + |
| 109 | + // Ensure bodies remain finite to catch explosions/NANs early. |
| 110 | + for (const auto& box : boxes) { |
| 111 | + const auto pos = box->getBodyNode(0)->getWorldTransform().translation(); |
| 112 | + ASSERT_TRUE(pos.array().allFinite()); |
| 113 | + } |
| 114 | + |
| 115 | + const auto topPos |
| 116 | + = boxes.back()->getBodyNode(0)->getWorldTransform().translation(); |
| 117 | + maxObservedTopZ = std::max(maxObservedTopZ, topPos.z()); |
| 118 | + |
| 119 | + if (step == kNumSteps - 1) { |
| 120 | + for (const auto& box : boxes) |
| 121 | + finalKineticEnergy += box->computeKineticEnergy(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + EXPECT_LT(maxObservedTopZ, expectedTopCenter + 2.0); |
| 126 | + EXPECT_GT( |
| 127 | + boxes.back()->getBodyNode(0)->getWorldTransform().translation().z(), |
| 128 | + expectedTopCenter - 0.5); |
| 129 | + EXPECT_LT(finalKineticEnergy, 5.0); |
| 130 | +} |
0 commit comments