Hello all… first post here.
I am trying to build a legged creature using constraints to set angles between leg segments. This works but the results are kind of floppy in the physics simulation. I am trying to have stiff leg joints set at exactly the angle I set the hinge too.
Any help would be most appreciated.
Here’s code for a leg segment:
Node* Hexapod::
CreateSegment( Node* aParentSegNode,
const Vector3 aParentConstraintOffset,
double aRotFromParent,
const String& aName,
double aLen,
const Vector3 aAxis,
double aAngle )
{
auto ballRestitution = 0.75f;
auto NewSegNode = aParentSegNode->CreateChild(aName);
NewSegNode->SetName(aName);
//NewSegNode->Roll(180.0);
//NewSegNode->SetPosition( Vector3(0.5 * aLen, 0.0f, 0.f));
Vector3 xoffs;
if( aParentConstraintOffset.x_ > 0.0 )
xoffs = Vector3(aLen / 2.0, 0, 0);
else
{
xoffs = Vector3(-aLen / 2.0, 0, 0);
}
NewSegNode->SetPosition( aParentConstraintOffset + xoffs);
NewSegNode->SetRotation(Quaternion(0., aRotFromParent, 0.));
auto shape = NewSegNode->CreateComponent<CollisionShape>();
// set the collision shape slightly smaller than the bone so ends don't collide.
shape->SetBox(Vector3( 0.9 * aLen, .1, .3));
auto body = NewSegNode->CreateComponent<RigidBody>();
// Gravity override
body->SetGravityOverride(Vector3(0.0f, -1.0f, 0.0f));
//body->SetRestitution(ballRestitution);
body->SetRestitution(0.); // legs shouldn't bounce
// Set mass to make movable
body->SetMass(1.0f);
body->SetFriction(2.0f);
// Set damping parameters to smooth out the motion
body->SetLinearDamping(0.05f);
body->SetAngularDamping(0.85f);
// Set rest thresholds to ensure the rigid bodies come to rest to not consume CPU endlessly
body->SetLinearRestThreshold(1.5f);
body->SetAngularRestThreshold(2.5f);
auto* constraint = NewSegNode->CreateComponent<Constraint>();
constraint->SetConstraintType(CONSTRAINT_HINGE);
// Most of the constraints will work better when the connected bodies don't collide against each other
constraint->SetDisableCollision(true);
// The connected body must be specified before setting the world position
constraint->SetOtherBody(aParentSegNode->GetComponent<RigidBody>());
constraint->SetOtherPosition(aParentConstraintOffset);
constraint->SetPosition(Vector3(-0.5 * aLen,0,0));
//constraint->SetWorldPosition(aParentSegNode->GetWorldPosition() + aParentConstraintOffset);
constraint->SetCFM( 0 );
constraint->SetERP( 0.8 );
//std::cerr << "CFM *** " << constraint->GetCFM() << " ERP " << constraint->GetERP() << std::endl;
constraint->SetAxis(aAxis);
constraint->SetOtherAxis(aAxis);
constraint->SetHighLimit(Vector2(aAngle,0.f));
constraint->SetLowLimit(Vector2(aAngle,0.f));
return NewSegNode;
}