Physics Simulation Forum

 

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Apr 24, 2012 5:15 pm 
Offline

Joined: Tue Apr 24, 2012 5:01 pm
Posts: 2
I'm working on the physics for my game, but I can't seem to get started. Here's the relevant code that triggers the problem (actually my game uses Ammo.js, but Bullet, which Ammo is based on, exhibits this behaviour too):
Code:
// Enter these commands, followed by <Return>, to control the player:
// "l" - Left
// "r" - Right
// "u" - Up

#include <btBulletDynamicsCommon.h>
#include <stdio.h>
#include <time.h>
#include <boost/timer/timer.hpp>
#include <boost/thread.hpp>

boost::mutex playerBodyLock;

void reader(btRigidBody *body);

int main() {
   btDefaultCollisionConfiguration config;
   btCollisionDispatcher dispatcher(&config);
   btDbvtBroadphase broadphase;
   btSequentialImpulseConstraintSolver solver;
   btDiscreteDynamicsWorld world(&dispatcher, &broadphase, &solver, &config);
   world.setGravity(btVector3(0, -10, 0));

   btStaticPlaneShape environmentShape(btVector3(0.0f, 1.0f, 0.0f), -3);
   btDefaultMotionState environmentMotionState;
   btCapsuleShape playerShape(1.0f, 2.0f);
   btDefaultMotionState playerMotionState;
   btVector3 playerInertia;
   playerShape.calculateLocalInertia(30, playerInertia);

   btRigidBody::btRigidBodyConstructionInfo environmentBodyCI(0.0f, &environmentMotionState, &environmentShape);
   btRigidBody environmentBody(environmentBodyCI);
   btRigidBody::btRigidBodyConstructionInfo playerBodyCI(30.0f, &playerMotionState, &playerShape, playerInertia);
   btRigidBody playerBody(playerBodyCI);

   playerBody.setAngularFactor(btVector3(0.0f, 0.0f, 0.0f));
   playerBody.setLinearFactor(btVector3(1.0f, 1.0f, 0.0f));
   world.addRigidBody(&environmentBody);
   world.addRigidBody(&playerBody);

   boost::thread readThread(reader, &playerBody);
   boost::timer::cpu_timer timer;
   timer.start();
   float lastUpdate = timer.elapsed().wall / 1e9;
   btTransform playerTransform;
   while(true) {
      {
         boost::lock_guard<boost::mutex> lock(playerBodyLock);
         float now = timer.elapsed().wall / 1e9;
         world.stepSimulation(now - lastUpdate, 5);

         playerBody.getMotionState()->getWorldTransform(playerTransform);
         printf("%f\t%f\t%f\n", playerTransform.getOrigin().x(), playerTransform.getOrigin().y(), playerTransform.getOrigin().z());
         lastUpdate = now;
      }
      boost::this_thread::sleep(boost::posix_time::millisec(40));
   }
}

void reader(btRigidBody *body)
{
   while(true) {
      char command[5];
      fgets(command, sizeof(command), stdin);
      switch(command[0]) {
      case 'l': {
         boost::lock_guard<boost::mutex> lock(playerBodyLock);
         body->applyCentralImpulse(btVector3(-30, 0, 0));
         break;
      }
      case 'r': {
         boost::lock_guard<boost::mutex> lock(playerBodyLock);
         body->applyCentralImpulse(btVector3(30, 0, 0));
         break;
      }
      case 'u': {
         boost::lock_guard<boost::mutex> lock(playerBodyLock);
         body->applyCentralImpulse(btVector3(0, 100, 0));
         break;
      }
      }
   }
}


First, the player capsule falls for a while. Then, if you enter "u<Return>" it moves up and comes down again. However, if you wait ~5 seconds after the body comes to a halt, it can't be moved again ever so slightly. Here's the code I've written using Ammo: https://gist.github.com/2481565. Both the C++ version and the JS version have the problem, and I can't figure out what I'm doing wrong.

My operating system is Windows 7 x64. I use Bullet 2.80-rev2531 in the "Release" configuration, compiled with Visual Studio 2010. My processor is an Intel Core i7 Q820 @ 1.73 GHz. As for the JS version, I've tested it with the latest Chrome (on the stable update channel) and Firefox 12 (Beta) on the same system.

- Raphael


Top
 Profile  
 
PostPosted: Wed Apr 25, 2012 1:12 pm 
Offline

Joined: Wed Jan 25, 2012 6:32 pm
Posts: 16
It's deactivation. After some time being inactive body goes to "sleep". Use body->activate(true) before applying any forces to make sure it's not sleeping.


Top
 Profile  
 
PostPosted: Wed Apr 25, 2012 7:13 pm 
Offline

Joined: Tue Apr 24, 2012 5:01 pm
Posts: 2
Thanks, that was the problem.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: cloud and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group