Influencing a RigidBody with applyForce, setLinearVelocity or applyImpulse does not work

Post Reply
tmohr
Posts: 8
Joined: Sat Mar 31, 2018 6:30 pm

Influencing a RigidBody with applyForce, setLinearVelocity or applyImpulse does not work

Post by tmohr »

Hello,

the attached file is an example of SDL2 and Bullet-2.81. In there a cube falls on a ground plane and then stays there.
I'd like to influence the cube and tried three ways that all did not have any effect at all.

Here is the relevant initialisation / setup and the cyclic code, please note that the Z vector points upwards, not the Y vector:

Code: Select all

void init_bullet(void) {
  broadphase = new btDbvtBroadphase();

  collisionConfiguration = new btDefaultCollisionConfiguration();
  dispatcher = new btCollisionDispatcher(collisionConfiguration);

  solver = new btSequentialImpulseConstraintSolver;

  dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
  dynamicsWorld->setGravity(btVector3(0, 0, -10));

  groundShape = new btStaticPlaneShape(btVector3(0, 0, 1), 1);
  //  fallShape = new btSphereShape(1);
  fallShape = new btBoxShape(btVector3(0.5, 0.5, 0.5));

  groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, -1)));

  //btRigidBody::btRigidBodyConstructionInfo
  //            groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0, 0, 0));

  groundRigidBodyCI = new btRigidBody::btRigidBodyConstructionInfo(0, groundMotionState, groundShape, btVector3(0, 0, 0));
  groundRigidBody = new btRigidBody(*groundRigidBodyCI);


  dynamicsWorld->addRigidBody(groundRigidBody);

  fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 5)));

  btScalar mass = 1;
  btVector3 fallInertia(0, 0, 0);
  fallShape->calculateLocalInertia(mass, fallInertia);

  fallRigidBodyCI = new btRigidBody::btRigidBodyConstructionInfo(mass, fallMotionState, fallShape, fallInertia);
  fallRigidBody = new btRigidBody(*fallRigidBodyCI);
  dynamicsWorld->addRigidBody(fallRigidBody);
}

void bullet_cyclic(Uint32 d) {
  if(d > 50) {
    d = 50;
  }
  dynamicsWorld->stepSimulation(1.0f / d, 500);
}
I then call this code if a certain key is pressed:

Code: Select all

#define IMP  500000
    if(spec & 1) { // "a" is pressed
      fallRigidBody->applyForce(btVector3(IMP, 0, 0), btVector3(0, 0, 0));
      fallRigidBody->setLinearVelocity(btVector3(IMP, 0, IMP));
    }
    if(spec & 2) { // "b" is pressed
      fallRigidBody->applyImpulse(btVector3(0, IMP, 0), btVector3(0, 0, 0));
    }
    if(spec & 4) { // "c" is pressed
      fallRigidBody->applyImpulse(btVector3(0, 0, IMP), btVector3(0, 0, 0));
    }

    bullet_cyclic(d);
    draw(d);
The code does not have any visible effect at all. The relevant ode part is not just executed once when the key is pressed but in each cycle while the key is pressed.

I wonder how I can apply a force or something similar.


Thanks for any hints,
Torsten
Attachments
main.cpp
SDL2 / Bullet-2.81 example
(12.94 KiB) Downloaded 206 times
tmohr
Posts: 8
Joined: Sat Mar 31, 2018 6:30 pm

Re: Influencing a RigidBody with applyForce, setLinearVelocity or applyImpulse does not work

Post by tmohr »

Ok, found it. In stepSimulation it should be d / 1000.0f, not 1.0f/d.
Post Reply