Constrain movement to zero on a axis

cobolt_dink
Posts: 72
Joined: Fri Apr 04, 2008 6:07 pm

Constrain movement to zero on a axis

Post by cobolt_dink »

As my ball moves and bounces around the level I would like its Y position to never change from where it started (ball radius). I thought this code would do it (modified from something I found here to make a object only fall straight down and not to the side):

Code: Select all

   btTransform identity = btTransform::getIdentity();
	
	btTransform transform;
	transform.setIdentity();
	transform.setOrigin(body->getWorldTransform().getOrigin());

	static btRigidBody s_fixed(0, 0, 0);
	btGeneric6DofConstraint *slider = new btGeneric6DofConstraint(*body, s_fixed, identity,
		                                                          transform, true);

	slider->setAngularLowerLimit(btVector3(-1, -1, -1));
	slider->setAngularUpperLimit(btVector3(1, 1, 1));

	slider->setLinearLowerLimit(btVector3(-1e30f, 0, -1e30f));
	slider->setLinearUpperLimit(btVector3(1e30f, 0, 1e30f));		

	m_dynamicsWorld->addConstraint(slider);

	body->addConstraintRef(slider);
But when logging collisions I can see the Y position is all over the place. I set linear velocity every update and the Y part is zero. So I'm not sure what I'm supposed to do to get that behavior.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Constrain movement to zero on a axis

Post by Erwin Coumans »

I would like its Y position to never change from where it started (ball radius).
It is recommended to restrict the motion using btRigidBody::setLinearFactor. For example:

Code: Select all

body->setLinearFactor(btVector3(1,0,1));
This can be combined with or without setAngularFactor to allow rotation around certain axis:

Code: Select all

body->setAngularFactor(btVector3(0,1,0));
The btGeneric6DofConstraint doesn't allow full motion on the Y-axis in combination with X or Z limits.

Hope this helps,
Erwin
cobolt_dink
Posts: 72
Joined: Fri Apr 04, 2008 6:07 pm

Re: Constrain movement to zero on a axis

Post by cobolt_dink »

setLinearFactor() doesn't seem to exist in 2.74. Is this a new feature for 2.75?

*edit*
I see its in 2.75 RC7, I will have to build that and see if it fixes the problem.
cobolt_dink
Posts: 72
Joined: Fri Apr 04, 2008 6:07 pm

Re: Constrain movement to zero on a axis

Post by cobolt_dink »

So I built 2.75 RC7 and used that and it seems to be working now. I do have a question though

Code: Select all

body->setLinearFactor(btVector3(1, 0, 1));
body->setLinearVelocity(btVector3(0, 10, 0));
That will still make a body go up. Does the linear factor only work on impulses given to a body and not setting the velocity directly?
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Constrain movement to zero on a axis

Post by Erwin Coumans »

Ok, glad it works fine now.

Using 'setPosition' or 'setVelocity' bypasses the constraint solver indeed. Why would you set the velocity in a direction that you don't want the object to move ;-)?

Thanks,
Erwin
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: Constrain movement to zero on a axis

Post by dphil »

Are the linear and angular factors handled in such a way that they provide more efficient motion updates? I ask because I have a simulation with many spheres, and their rotation is unimportant. So if I "turn off" rotation by setting the angular factor to 0, will that mean *less* processing and greater efficiency? Or would disabling rotation essentially be like a constraint; ie yet another thing that needs to be processed, and should only be used if necessary?
almatea
Posts: 29
Joined: Mon Nov 02, 2009 10:15 am

Re: Constrain movement to zero on a axis

Post by almatea »

rigid_Body->setLinearFactor(btVector3(0,0,0)) - it is a way to animate activity of a body (static and active mode) - it is correct way?