How to specify slider constraint axis

Post Reply
tomhhh
Posts: 17
Joined: Tue Jan 19, 2016 10:44 pm

How to specify slider constraint axis

Post by tomhhh »

Hi

I would like to dynamically create an axis for a btSliderConstraint at runtime between two rigid bodies.

I have two rigid bodies in world space, I take there centre of mass positions and subtract one from the other to generate the axis.

I am not quite sure how to create the reference frames for A and B to allow constraints to be limited to this axis.

// Need to calculate a reference frame for local_a and local_b based on world positions of rigid bodies
btTransform local_a = btTransform::getIdentity();
btTransform local_b = btTransform::getIdentity();

btSliderConstraint* slider_constraint = new btSliderConstraint( *rb_a, *rb_b, local_a, local_b, true );

I have tried putting rigid body B in the space of A and then using setBasis on the local_a by calculating a new coordinate space (so forward is the axis, and I calculate up and right by Gram–Schmidt) but this doesn't seem to work.

By default the constraint axis seems to exist in local_a xaxis (if local_a and local_b are identity). I can change the axis by using:

local_a.getBasis().setEulerZYX(0.0f,<angle>, 0.0f);

but I can't use this when wanting to create an axis based on the world space positions.

Any information on this would be greatly appreciated!! I am afraid I don't fully understand exactly what frameA and frameB of the btSliderConstraint want.

Thank you very much for your help!!


Tom
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: How to specify slider constraint axis

Post by Basroil »

frames are basically just definitions of the transformation axis in the local coordinates of the bodies that make up that joint. For example, you have translation across the z axis for a pair of squares of length 1 starting with the two bodies perfectly stacked along the world z axis. The resulting transformations will be an identity rotation and then a translation component of 0,0,0.5 for body A, and 0,0,-0.5 for body B (with body b above a). The reason you can redefine the transformations is so that you can define the current offset as a non-zero starting point (i.e. the bodies start at the middle point of the slider rather than an end)
tomhhh
Posts: 17
Joined: Tue Jan 19, 2016 10:44 pm

Re: How to specify slider constraint axis

Post by tomhhh »

Hi Basroil

Thank you very much for getting back to me and for your explanation of frames,

I understand how you can set an offset but what I still don't quite understand is how you specify the axis. In your post you mention the Z axis, but how did you set that. If say you wanted the X or Y axis for your slider, how would you specify those, or an arbitrary axis, which is what I'm interested in.

In the Forklift Bullet example an angle of PI_2 is used to rotate the slider axis so that it moves up and down by setting the basis of the frame transform. Is there another way of doing this?

Thanks again!


Tom
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: How to specify slider constraint axis

Post by Basroil »

tomhhh wrote:I understand how you can set an offset but what I still don't quite understand is how you specify the axis. In your post you mention the Z axis, but how did you set that. If say you wanted the X or Y axis for your slider, how would you specify those, or an arbitrary axis, which is what I'm interested in.
A btTranform object has full transform data in it, so just specify the axis you want in the local coordinates of the bodies. Depending on the constraint type you'll be able to rotate or translate about one of the axes, though for certain types the axis is hard coded and can't be changed without re-defining the constraint equations. In 100% of cases though, there's no reason to do that since you can just redefine your joint axis instead (want a rotation about the x axis in hinge? just rotate your axis by 90 degrees about the y axis!)
amatic
Posts: 13
Joined: Sun Oct 25, 2015 10:17 am

Re: How to specify slider constraint axis

Post by amatic »

tomhhh wrote:Hi

I would like to dynamically create an axis for a btSliderConstraint at runtime between two rigid bodies.

I have two rigid bodies in world space, I take there centre of mass positions and subtract one from the other to generate the axis.
I think I might have a similar case, let me know if it works for you, I'm not sure it is entirely correct.

Code: Select all

btSliderConstraint * add_slider_motor(btRigidBody &a, btRigidBody &b) {
		btVector3 dif = b.getCenterOfMassPosition() - a.getCenterOfMassPosition();

		btScalar rZ = atan2f(dif.getY(), dif.getX());
		btScalar rY = atan2f(dif.getZ(), dif.getX());

		btTransform tra, trb;
		tra.setIdentity();
		tra.getBasis().setEulerZYX(0, rY, rZ);
		trb.setIdentity();
		btSliderConstraint * motor = new btSliderConstraint(a, b, tra, trb, true);

		motor->setPoweredLinMotor(true);
		motor->setMaxLinMotorForce(10000);
		motor->setTargetLinMotorVelocity(0.0f);

		m_dynamicsWorld->addConstraint(motor);

		return motor;
	}
tomhhh
Posts: 17
Joined: Tue Jan 19, 2016 10:44 pm

Re: How to specify slider constraint axis

Post by tomhhh »

Okay I think I understand, and in honesty I think that's what I was trying to do, but I could not get that behaviour.

Specifically with the btSliderConstraint - if I do not specify an axis when creating the constraint, the axis defaults to the local X axis (if I set one object to be static, and the other to be dynamic, and create this constraint between them, I see the dynamic object ping into the local X axis of the static object)

So say I have two objects A and B. (A and B are their world transforms)

I first put B in the space of A by doing:

B' = inv A * B

B' is B in the local space of A

The position of B' gives me the axis I want in terms of A (B' - A, but A is the origin, so it's just B.

I then normalize this vector to give me this axis, I'll refer to this as V.

I want to then pass this into btSliderConstraint somehow.

I have tried something like this:

GetAcrossAndUp( V, &across, &up ) // calculate new basis
btTransform local_a = btTransform::getIdentity();
local_a.setBasis( across, up, V )

But this doesn't get the behaviour I want where the two objects slide along this axis.

I am really sorry I'm not getting this, does what I'm doing make sense? And what step am I missing?

Thank you again for your time! I really appreciate it!


Tom
tomhhh
Posts: 17
Joined: Tue Jan 19, 2016 10:44 pm

Re: How to specify slider constraint axis

Post by tomhhh »

Hi Amatic,

Thank you for the example code. I'll give that a go but I think the vector you're calculating might be wrong as it's in World Space not Local Space for the first object.

I'll try and give that a go and see if I have any luck

Thanks!


Tom
tomhhh
Posts: 17
Joined: Tue Jan 19, 2016 10:44 pm

Re: How to specify slider constraint axis

Post by tomhhh »

I finally got this to work - The process I outlined above does work by calculating a new reference frame (make sure you calculate the vector in local space not world space)

What was catching me out was the coordinate space I'm working in and bullet is different, so what I was treating as a forward vector (Y in my case - we treat Z as up) was actually X in Bullet.

After switching these around it all worked. Thank you for your help everyone!

Best,


Tom
Post Reply