Help with Hinges

Epileftric
Posts: 10
Joined: Mon Sep 09, 2013 4:54 pm
Location: Buenos Aires, Argentina

Help with Hinges

Post by Epileftric »

Hello! I'm new here!
I've been doing some stuff with the library it's great! I'm new to C++, but i've have programmed in C my hole life, I tell you that because maybe there's something I'm not doing properly!

The project I'm working on is an Hexapod (hexapod hexample).

So I've started with a basic leg which is 3DOF. This is what it should look like IRL:
Image

And this is how I code it:

Code: Select all

	btRigidBody *boxF = new btRigidBody ( 100.0 , //dat mass
										  new btDefaultMotionState( btTransform(btQuaternion(0,0,0,1),btVector3(0,0,5))),
										  new btBoxShape( btVector3(1,1,1)) ,
										  btVector3 (0,0,0) );
	
	
	btRigidBody *box0 = new btRigidBody ( 1.0 , //dat mass
										  new btDefaultMotionState( btTransform(btQuaternion(0,0,0,1),btVector3(0,0,10))),
										  new btSphereShape( (1)) , //technically not a box
										  btVector3 (0,0,0) );	
	
	btRigidBody *box1 = new btRigidBody ( 1.0, //dat mass
										  new btDefaultMotionState( btTransform(btQuaternion(0,0,0,1),btVector3(-5,0,10))),
										  new btBoxShape( btVector3(2.5,0.75,0.75)) ,
										  btVector3 (0,0,0) );
	
	btRigidBody *box2 = new btRigidBody ( 1.0 , //dat mass
										  new btDefaultMotionState( btTransform(btQuaternion(0,0,0,1),btVector3(-19,0,10))),
										  new btBoxShape( btVector3(10,0.25,0.25)) ,
										  btVector3 (0,0,0) );

	btHingeConstraint *q0 = new btHingeConstraint ( *box0, btVector3(0,0,0) , btVector3(0,0,1) ) ;
	box0->addConstraintRef(q0);
	
	btHingeConstraint *q1 = new btHingeConstraint ( *box0, *box1,
											btVector3(-1.5,0,0) , btVector3(3,0,0),
											btVector3(0,1,0)  , btVector3(0,1,0) );
	box0->addConstraintRef(q1);
	box1->addConstraintRef(q1);
	
	btHingeConstraint *q2 = new btHingeConstraint( *box1, *box2, 
											btVector3(-3,0,0) , btVector3(10.5,0,0),
											btVector3(0,1,0)  , btVector3(0,1,0) );
	box1->addConstraintRef(q2);
	box2->addConstraintRef(q2);
	
	dynamicsWorld->addRigidBody(box0);
	dynamicsWorld->addRigidBody(box1);
	dynamicsWorld->addRigidBody(box2);
	dynamicsWorld->addConstraint(q0);
	dynamicsWorld->addConstraint(q1);
	dynamicsWorld->addConstraint(q2);
    
    dynamicsWorld->addRigidBody(boxF);

The problem I have is that the Hinges look rigid like this:
Image

And I've tried not adding the first hinge (q0) and this is what happens:
Image

Is there anything I'm doing wrong? I don't get it... why doesn't the leg "falls" and bends in the hinges?
Epileftric
Posts: 10
Joined: Mon Sep 09, 2013 4:54 pm
Location: Buenos Aires, Argentina

Re: Help with Hinges

Post by Epileftric »

Other stuff I've tried is to add angular velocity to the first body:

Code: Select all

box0->setAngularVelocity(btVector3(0,0,1));
But the rest of the leg doesn't turn around the axis in Q0 like it should, it does this:
Image

It moves like the transmission on the wheels of a train!
Epileftric
Posts: 10
Joined: Mon Sep 09, 2013 4:54 pm
Location: Buenos Aires, Argentina

Re: Help with Hinges

Post by Epileftric »

I still don't get it... the black circle in the joint is the "available" section or the "unavailable" ?
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: Help with Hinges

Post by Basroil »

:EDIT:
My mistake, the third one is a different view from the others, and I forgot why I put 90 degrees in my own code :oops:
Last edited by Basroil on Thu Sep 12, 2013 4:45 pm, edited 2 times in total.
Epileftric
Posts: 10
Joined: Mon Sep 09, 2013 4:54 pm
Location: Buenos Aires, Argentina

Re: Help with Hinges

Post by Epileftric »

Basroil wrote:Your directions are off, rather than the standard z direction axis bullet uses the x direction, so just rotate your axis by 90 degrees and you'll be just fine.
So are you saying I should rotate the bodies? because the hinge looks fine in the picture, I mean, the black circles are normal to the axis I want in each articulation


EDIT:
I found the problem, I wasn't creating the rigid bodies correctly.

Code: Select all

btRigidBody *box0 = new btRigidBody ( 1.0 , //dat mass
                                new btDefaultMotionState( btTransform(btQuaternion(0,0,0,1),btVector3(0,0,10))),
                                new btSphereShape( (1)) , //technically not a box
                                btVector3 (0,0,0) );   
This kind of construction doesn't work. You have to go through this, like from the examples:

Code: Select all

btRigidBody* CreateRigidBody (btScalar mass, const btTransform& startTransform, btCollisionShape* shape)
{
        bool isDynamic = (mass != 0.f);

        btVector3 localInertia(0,0,0);
        if (isDynamic)
                shape->calculateLocalInertia(mass,localInertia);

        btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
        btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,shape,localInertia);
        rbInfo.m_additionalDamping = true;
        btRigidBody* body = new btRigidBody(rbInfo);
		
		return body;
}