Why won't my object move after this?

termhn
Posts: 11
Joined: Wed Jun 29, 2011 3:49 pm

Why won't my object move after this?

Post by termhn »

Hello all, I have a problem. I do this every frame:

Code: Select all

btVector3 angvel = fallRigidBody->getAngularVelocity();
	btVector3 linvel = fallRigidBody->getLinearVelocity();
	if ((angvel.getX() < 0.1 && angvel.getY() < 0.1 && angvel.getZ() < 0.1) && (linvel.getX() < 0.1 && linvel.getY() < 0.1 && linvel.getZ() < 0.1) && !reset) {
		fallRigidBody->getMotionState()->setWorldTransform(btTransform(btQuaternion(0,0,0,1), btVector3(0,20,0)));
		fallRigidBody->setLinearVelocity(btVector3(0,0,0));
		fallRigidBody->setAngularVelocity(btVector3(0,0,0));
		fallRigidBody->applyForce(btVector3(1,0,0), btVector3(0,20,0));
		reset = true;
	} else {
		if (steps > 10) {
			steps = 0;
			reset = false;
		} else {
			steps ++;
		}
	}
however, it resets, but then it just sits there, not doing anything. why is that?
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: Why won't my object move after this?

Post by dphil »

First, you could try increasing the magnitude of the applied force to a very large number. If the force is too small, it may produce an unnoticeable result over the 10 frames before you reset it again. Second, if an object's speed over a certain period of time is less than a certain threshold, bullet puts it to "sleep", meaning it is more or less ignored by the engine until something collides with it. That means manually applied forces (body->applyForce(...)) won't work unless you "wake" the body up first. So right before the line where you apply a force, try adding:

Code: Select all

fallRigidBody->activate();
On a side note, in the first "if" statement are you just trying to check if the speed of the object is less than a certain threshold? If so, just do:

Code: Select all

if (angvel.length() < 0.1 && linvel.length() < 0.1 && !reset)
{
    ...
}
or use (length2() < 0.01) if you want to avoid square root calculations.
termhn
Posts: 11
Joined: Wed Jun 29, 2011 3:49 pm

Re: Why won't my object move after this?

Post by termhn »

Thanks for the reply :D
it's working now :)