Inverted Pendulum using hinge and slider constraint HELP

JDTR75
Posts: 6
Joined: Tue May 22, 2012 12:35 am

Inverted Pendulum using hinge and slider constraint HELP

Post by JDTR75 »

Hi guys I'm trying to set up this Inverted Pendulum that I found made on Blender:
screenshot1.png
That's the wire-view so you can see everything clearly. This example runs perfectly, it has a Hinge constraint on the palette that is attached to the cylinder and a 6DOF constraint on the box that is actually working as a Slider constraint. Well here goes my doubts:

I viewed the SliderConstraintDemo to understand how to use it, and of course I also viewed the ConstraintDemo. I have this on my code so far:

Code: Select all

btRigidBody *base = addBox(btVector3(0, -1, 0), 44.f, 2.f, 2.f, 0.f);
btRigidBody *car = addBox(btVector3(0, 0, 0), 2.f, 2.f, 2.f, 1.f);
The method addBox is like this:

Code: Select all

btRigidBody* InvertedPendulum::addBox(btVector3 origin, float width, float height, float depth, float mass) {
	btTransform t;
	t.setIdentity();
	t.setOrigin(origin);
	btBoxShape* box = new btBoxShape(btVector3(width / 2.0, height / 2.0, depth / 2.0));
	collisionShapes.push_back(box);
	btVector3 inertia(0, 0, 0);
	if (mass != 0.0)
		box->calculateLocalInertia(mass, inertia);

	btMotionState* motion = new btDefaultMotionState(t);
	btRigidBody::btRigidBodyConstructionInfo info(mass, motion, box, inertia);
	btRigidBody* body = new btRigidBody(info);
	world->addRigidBody(body);
	return body;
}
So quiet simple, I'm adding the static body called 'base' and the rigid body called 'car'. For the slider constraint of the car, I want to move it across the x-axis and I'm trying something like this:

Code: Select all

   btTransform frameInA, frameInB;
	frameInA = btTransform::getIdentity();
	frameInB = btTransform::getIdentity();
	frameInA.setOrigin(btVector3(0.5, 0, 0));
	frameInB.setOrigin(btVector3(0.5, 0, 0));

	spSlider = new btSliderConstraint(*base, *car, frameInA, frameInB, true);
	spSlider->setLowerLinLimit(loSliderLimit);
	spSlider->setUpperLinLimit(hiSliderLimit);

	world->addConstraint(spSlider,true);
I want the car to move between -20 and 20 across the x-axis, in the image above this is easier because the car crashes with the borders of the base so that it doesn't fall. I'm having troubles with this because I really don't understand what it means the frameInA and frameInB parameters (sorry if this is something dumb, but I'm kind of new using bullet and it's an entirely different thing coding than using Blender)

So that's it guys. I hope you can give me a hand with this.
You do not have the required permissions to view the files attached to this post.
Ateocinico
Posts: 12
Joined: Sat Jun 29, 2013 5:55 pm

Re: Inverted Pendulum using hinge and slider constraint HELP

Post by Ateocinico »

In the bullet constraint classes those variables represent the constraint insertion position relative to the center of mass of the respective bodies.

In the 6dof constraints class you find that:
btTransform m_frameInA;//!< the constraint space w.r.t body A
btTransform m_frameInB;//!< the constraint space w.r.t body B

And in the linear constraint class there is something revealing. For a constructor with only "b" body and no "a" body:

///not providing rigidbody A means implicitly using worldspace for body A
m_frameInA = rbB.getCenterOfMassTransform() * m_frameInB;
JDTR75
Posts: 6
Joined: Tue May 22, 2012 12:35 am

Re: Inverted Pendulum using hinge and slider constraint HELP

Post by JDTR75 »

Thank you for your quick response Ateocinico, I'll give it a try soon. So a quick question regarding what you just said
In the bullet constraint classes those variables represent the constraint insertion position relative to the center of mass of the respective bodies.
, being said that, if I make something like this:

Code: Select all

   btTransform frameInA, frameInB;
   frameInA = base->getCenterOfMassTransform();
   frameInB = car->getCenterOfMassTransform();
   
   spSlider = new btSliderConstraint(*base, *car, frameInA, frameInB, true);
   spSlider->setLowerLinLimit(loSliderLimit);
   spSlider->setUpperLinLimit(hiSliderLimit);

   world->addConstraint(spSlider,true);
should it work?
Ateocinico
Posts: 12
Joined: Sat Jun 29, 2013 5:55 pm

Re: Inverted Pendulum using hinge and slider constraint HELP

Post by Ateocinico »

I would add the relative displacement of the anchor point relative to the center of mass. Something like this:

btVector3 fca(2,0,0); // Position vector from the center of mass of A to the anchor restriction point
frameInA = base->getCenterOfMassTransform() + fca;

And really a better documentation is needed for this part.
JDTR75
Posts: 6
Joined: Tue May 22, 2012 12:35 am

Re: Inverted Pendulum using hinge and slider constraint HELP

Post by JDTR75 »

Thanks again for your comments, I'll give it a try and see if it works (and post a reply when it works :D).

There's another thing that I'd like to know, as you can see in the image, the cylinder is penetrating the box, being said that, in code this isn't possible, of course when I do that the objects push away from each other, I know this must be pretty dumb but how can I avoid this from happening? I know that this must be some option in the Collision but I just want to avoid the collision between the cylinder and the box but I don't want to avoid the collision between the box and the base. I hope I was explicit enough.

And YES I agree with you, I think in general the engine needs A LOT more documentation.