Slow falling?

lawl_rock
Posts: 2
Joined: Sat Jun 04, 2011 3:38 pm

Slow falling?

Post by lawl_rock »

Hello, I recently started using Bullet physics, and I'm experiencing a problem in which my object falls extremely slowly through the otherwise empty world.
Here is how I initiate my physics:

Code: Select all

void Physics::init() {
	boost::shared_ptr<btDefaultCollisionConfiguration> tempConfig(new btDefaultCollisionConfiguration());
	m_collisionConfiguration.swap(tempConfig);
	
	boost::shared_ptr<btCollisionDispatcher> tempDispatch(new btCollisionDispatcher(m_collisionConfiguration.get()));
	m_dispatcher.swap(tempDispatch);
	
	boost::shared_ptr<btBroadphaseInterface> tempBroadphase(new btDbvtBroadphase());
	m_broadphase.swap(tempBroadphase);

	boost::shared_ptr<btSequentialImpulseConstraintSolver> sol(new btSequentialImpulseConstraintSolver);
	m_solver.swap(boost::shared_ptr<btConstraintSolver>(sol));

	boost::shared_ptr<btDynamicsWorld> tempWorld
		(new btDiscreteDynamicsWorld(m_dispatcher.get(), m_broadphase.get(),
		m_solver.get(), m_collisionConfiguration.get()));
	m_dynamicsWorld.swap(tempWorld);
	m_dynamicsWorld->setGravity(btVector3(0, -10, 0));
}
How I initiate my object:

Code: Select all

kinematicController::kinematicController(PhysicsEngine* aEngine, btVector3 &pos, btVector3 &dimensions) {
	engine=aEngine;
	
	boost::shared_ptr<btCapsuleShape> tempShape
		(new btCapsuleShape(dimensions.getX(), dimensions.getY()));
	shape.swap(tempShape);
	btScalar mass(10.f);
	btVector3 localInertia(0, 0, 0);
	shape->calculateLocalInertia(mass, localInertia);
	
	btTransform startTransform;
	startTransform.setIdentity();
	startTransform.setOrigin(pos);
	
	boost::shared_ptr<btDefaultMotionState> tempState(new btDefaultMotionState(startTransform));
	motionState.swap(tempState);
	
	btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, motionState.get(), shape.get(), localInertia);
	boost::shared_ptr<btRigidBody> tempBody(new btRigidBody(rbInfo));
	body.swap(tempBody);
	body->setFriction(0.0);
	body->setSleepingThresholds(0.0, 0.0);
	body->setAngularFactor(0.0);
	body->setDamping(0.0, 0.0);
	body->setRestitution(0.0);
	engine->addRigidBody(body.get());
}
* Note the dimensions being passed are 0.3 by 0.5

My physics is stepped by:

Code: Select all

void step() {
	float us=(float)timer->getDelta_us();
	m_dynamicsWorld->stepSimulation(us/1000000.f);
}
The timer is defined as below and is restarted every frame:

Code: Select all

class BulletTimer {
	private:
	btClock clock;
	
	public:
	BulletTimer() {clock.reset();}
	inline void start() {
		clock.reset();
	}
	inline unsigned long int getDelta_ms() {
		return clock.getTimeMilliseconds();
	}
	inline unsigned long int getDelta_us() {
		return clock.getTimeMicroseconds();
	}
};
So, like I said before, my object is falling extremely slowly. Also worth noting that when I apply set velocity with a keypress, it doesn't move automatically nor does it stop automatically. Corresponding code is shown here:

Code: Select all

void Player::physics(Uint32 ticks) {
	// Forward/backward movement and strafing
	GLdouble forceX=0.0;
	GLdouble forceZ=0.0;
	if (key_W||key_A||key_S||key_D) { //Sine and cosine are expensive. Put these in an if statement
		GLdouble radY=rotY*RADIANS;
		GLdouble radX=rotX*RADIANS;
		GLdouble sinY=sin(radY);
		GLdouble cosY=cos(radY);
		forceX=((sinY*GLdouble(key_W))-(sinY*GLdouble(key_S))+(cosY*GLdouble(key_D))-(cosY*GLdouble(key_A)));
		forceZ=((sinY*GLdouble(key_D))-(sinY*GLdouble(key_A))-(cosY*GLdouble(key_W))+(cosY*GLdouble(key_S)));
	}
	btVector3 moveDirection(btScalar(forceX), 0, btScalar(forceZ));
	controller->setMoveDirection(moveDirection);
	
	btVector3 pos=controller->getCurrentPosition();
	cameraX=x=pos.getX();
	cameraY=(y=pos.getY())+1;
	cameraZ=z=pos.getZ();
}

void kinematicController::setMoveDirection(const btVector3 &direction) {
	btVector3 newDir(direction);
	newDir.setX(direction.getX()*10.0);
	newDir.setY(body->getLinearVelocity().getY());
	newDir.setZ(direction.getZ()*10.0);
	body->setLinearVelocity(newDir);
}
I apologize for the long post, I just wanted to make sure that I covered all the bases. Any help would be greatly appreciated, thank you in advance.
xexuxjy
Posts: 225
Joined: Wed Jan 07, 2009 11:43 am
Location: London

Re: Slow falling?

Post by xexuxjy »

I couldn't see anything obviously wrong in any of your setup code. What happens if you change your step value? does the speed change?, are the values going into the stepSimulation call in DiscreteDynamicsWorld what you'd expect? the default simulation frequency (fixedTimeStep) is 60Hz , again is this what you'd expect or were you using a variable timestep.
I'll and re-create this at home tonight and see if I get the same slowness.
XMight
Posts: 32
Joined: Tue Feb 22, 2011 1:00 pm

Re: Slow falling?

Post by XMight »

Change:

Code: Select all

   m_dynamicsWorld->stepSimulation(us/1000000.f);
to:

Code: Select all

m_dynamicsWorld->stepSimulation(us/60.f); or m_dynamicsWorld->stepSimulation(1.f/60.f);
This shall correct you issue I think. Otherwise, if you need such a simulation step though, I don't know.
lawl_rock
Posts: 2
Joined: Sat Jun 04, 2011 3:38 pm

Re: Slow falling?

Post by lawl_rock »

Thank you to both of you. I simply made a change in the time-step (effectively multiplying the delta time by 10), and this made things run more reasonably. I don't understand why this would be necessary, though. The timer is in microseconds, and I pass seconds to stepSimulation.
Kanttori
Posts: 38
Joined: Thu Jul 08, 2010 12:52 am

Re: Slow falling?

Post by Kanttori »

Hi!

I'd look into the time that's being passed to stepSimulation, make sure that it accounts for the whole time spent on the frame; ideally you'd read it and reset it directly afterwards.

When frames take longer to simulate it's worth also noting that you will lose time when the time- parameter you supply to stepSimulation is greater than maxSubSteps*fixedTimeStep (with default values it's anything over 1/60)