Influencing a RigidBody with applyForce, setLinearVelocity or applyImpulse does not work
Posted: Sat Mar 31, 2018 7:32 pm
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:
I then call this code if a certain key is pressed:
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
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);
}
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);
I wonder how I can apply a force or something similar.
Thanks for any hints,
Torsten