How to remove unwanted angular motion from constraints?

Kadesh
Posts: 4
Joined: Fri Aug 08, 2014 7:34 pm

How to remove unwanted angular motion from constraints?

Post by Kadesh »

I have an issue where I'm using a hinge constraint to connect two objects together (pretty much like a turret's attached to a tank) and it's preserving the parent's motion, as in, if I activate the constraint's angular motor while the parent object's rotating, the target object rotates slower than if the parent was stationary.

I'm creating the constraint using the following code:

Code: Select all

btVector3 axis{ 0.f, 1.f, 0.f };
btVector3 pivot{ 0.f, 0.f, -5.f };

auto* constraint = new btHingeConstraint(parentBody, targetBody, pivot, btVector3(0.f, 0.f, 0.f), axis, axis);
constraint->setLimit(-0.78f, 0.78f); // roughly [-45;45] degrees
constraint->enableAngularMotor(true, 0.f, 0.8f); // lock the motor initially
.....
// Later on, when I want it to rotate, called on every tick
constraint->setMotorTarget(btRadians(targetAngle), 0.1f);
Is there any way to make the constraint motor rotate at the same speed, independent of the parent's rotation? I've thought about negating the rotation from the parent in the constraint but I imagine there's a cleaner way to do this.
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: How to remove unwanted angular motion from constraints?

Post by Basroil »

Don't use setMotorTarget unless you don't care about angular velocity (since internally it just divides the target error by the dt parameter you put in) and just want to reach the target angle in exactly the dt you want. If you are looking to control angular velocity, use enableAngularMotor(true, DesiredAngularVelocity, MotorStrength) instead.

On another note, are you sure that the issue is a velocity change due to angular momentum rather than just having very weak motors? 0.8 is pretty low if you have unit mass and unit lever arms. Upping the number of joint iterations and increasing the motor strength should help if that's the case.
Kadesh
Posts: 4
Joined: Fri Aug 08, 2014 7:34 pm

Re: How to remove unwanted angular motion from constraints?

Post by Kadesh »

Thanks, you were right about the motor being extremely weak, using enableAngularMotor to control the motor and increasing the strength quite a bit worked perfectly. :D