[Solved] btRigidBody uncontrollable spin after setAngularVelocity()

Post Reply
pirogronian
Posts: 1
Joined: Fri Jan 29, 2021 7:31 pm

[Solved] btRigidBody uncontrollable spin after setAngularVelocity()

Post by pirogronian »

Well.. I'm in the middle of creating simple game... So it's hard for now to create clean code example recreating the problem. But if anyone have a wish, here is the code: https://github.com/pirogronian/IOSP.
Here is test scene setup: https://github.com/pirogronian/IOSP/blo ... tScene.cpp
Here is dumping test rigidbody ang. velocity and transform matrix basis: https://github.com/pirogronian/IOSP/blo ... lPanel.cpp (inside function update()).
I haven't expect any problems, as linear motion works fine from the beginning. But when I tried applying torque, it didn't work, just no impact on angular velocity. But when set angular velocity manually, I got uncontrollable spin, unrelated to applied velocity. The same is after set non-zero inertia with setMassProps function. I can see from console output angular velocity seems to behave reasonable then, but rotation matrix and visual appearance behave like a mad. When DISABLE_DEACTIVATION flag is unset, rotation stops after few seconds. Playing with max substeps seems to have no impact also.
I checked it with both system library and bundled within repo. System version is 3.08, I believe bundled is the same. System is Archlinux, up to date. Did I miss something? I cannot find anything similar over the forum and over the Internet.

Edit:
Just tested with gravity and rotated static collider. Without setMassProps test cube doesnt rotate on collision, while with set to non zero inertia, start spinning with the same mad way. This spinning seems to have no impact on linear motion.

Edit2:
Just prepared test example, but cannot reproduce the issue - box is slowly increasing its rotation, as expected:

Code: Select all

#include <irrlicht.h>
#include <btBulletDynamicsCommon.h>
#include <BulletDebugDrawer.h>

using namespace irr;

int main()
{
    auto *dev = irr::createDevice(
        irr::video::EDT_OPENGL,
        irr::core::dimension2d<irr::u32>(800, 600),
        16, false, true, true, 0);

    auto *smgr = dev->getSceneManager();
    auto *drv = dev->getVideoDriver();
    
    IOSP::BulletDebugDrawer drawer(drv);
    drawer.setDebugMode(btIDebugDraw::DBG_DrawWireframe|btIDebugDraw::DBG_DrawAabb);
    btDefaultCollisionConfiguration m_config;
    btCollisionDispatcher m_dispatcher{&m_config};
    btDbvtBroadphase m_broadphase;
    btSequentialImpulseConstraintSolver m_solver;
    btDiscreteDynamicsWorld m_world{&m_dispatcher, &m_broadphase, &m_solver, &m_config};
    
    m_world.setDebugDrawer(&drawer);
    m_world.setGravity(btVector3(0, 0, 0));
    auto *cam = smgr->addCameraSceneNodeMaya();
    cam->setPosition(core::vector3df(0, 0, 20));
    cam->setTarget(core::vector3df(0, 0, 0));
    
    btDefaultMotionState state;
    btBoxShape box(btVector3(5, 5, 5));
    btRigidBody rigid(1, 0, &box);
    rigid.setActivationState(DISABLE_DEACTIVATION);
    rigid.setMassProps(1, btVector3(1, 1, 1));
    m_world.addRigidBody(&rigid);
//     rigid.setAngularVelocity(btVector3(0, 0, 0.1));
    
//     auto *cube = smgr->addCubeSceneNode(10, smgr->getRootSceneNode());
//     cube->setMaterialFlag(video::EMF_LIGHTING, false);
    
    while(dev->run())
    {
        drv->beginScene(true, true, irr::video::SColor(0,50,50,50));
        smgr->drawAll();
        rigid.applyTorqueImpulse(btVector3(0, 0, 0.001));
        m_world.stepSimulation(0.017, 0, 1./60);
        drv->setTransform(irr::video::ETS_WORLD, irr::core::matrix4());
        m_world.debugDrawWorld();
        drv->endScene();
    }
}
Edit 3:
Ok, I think I got it. It's about my MotionState implementation - if rigid body has no motion state or Bullet's default one, everythink goes well. When has set my implementation, madness start.

Edit 4:
Solved! Of course, problem wasnt't with Bullet, but my MotionState class also was'nt a problem (at least not setWorldTransform()). I implemented setPosition and setRotation functions to automatically sync Irrlicht node transform with rigid body. There is a obvious problem with syncing function, but this is another, unrelated to Bullet, problem.
Post Reply