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));
}
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());
}
My physics is stepped by:
Code: Select all
void step() {
float us=(float)timer->getDelta_us();
m_dynamicsWorld->stepSimulation(us/1000000.f);
}
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();
}
};
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);
}