I'm trying to integrate Bullet Physics in double precision into Unreal Engine for my project and I ran into a strange issue. I set object into motion by applying bunch of torque and then let it spin - it's in space so there's no gravity or anything like that. At speeds of about 2-3 rad/s the simulation starts to quickly accumulate some numeric error (at least that's how it looks like) and my object flips around. I'm not really sure what could I be doing wrong, it's a simple setup, nothing much is happening, and the values aren't big either.
Here's a video to illustrate my problem. Sorry about the quality.
https://www.youtube.com/watch?v=1c9Fc5sjF5Y
Here's the initialization code:
Code: Select all
void UBoxShapeComponent::InitializeComponent()
{
check(!Shape && !MotionState && !RigidBody);
Shape = new btBoxShape(btVector3(BoxExtent.X / 100.0, BoxExtent.Y / 100.0, BoxExtent.Z / 100.0));
auto WorldLoc = GetComponentLocation();
auto WorldQuat = GetComponentQuat();
auto Rotation = btQuaternion{ WorldQuat.X, WorldQuat.Y, WorldQuat.Z, WorldQuat.W };
auto Position = btVector3{ WorldLoc.X, WorldLoc.Y, WorldLoc.Z };
MotionState = new btDefaultMotionState(btTransform(Rotation, Position));
btVector3 bodyInertia;
Shape->calculateLocalInertia(Mass, bodyInertia);
auto BodyCI = btRigidBody::btRigidBodyConstructionInfo(Mass, MotionState, Shape, bodyInertia);
RigidBody = new btRigidBody(BodyCI);
RigidBody->setActivationState(DISABLE_DEACTIVATION);
Super::InitializeComponent();
}
Code: Select all
void UBoxShapeComponent::AddScaledTorqueInDegrees(FVector Torque)
{
if (Torque.IsNearlyZero()) {
return;
}
check(RigidBody);
LOG("Angular input %s", *Torque.ToString());
auto ScaledTorque = btVector3{ Torque.X, Torque.Y, Torque.Z };
ScaledTorque *= 100.0; // tmp
RigidBody->applyTorque(ScaledTorque);
}