void BulletManager::createPlayerBody() { btCollisionShape* bodyShape = new btCapsuleShape(.3f,2.0f); WorldState *worldState = (WorldState *) GameState::GAMESTATE; Camera *camera = worldState->getPhysicsManager()->getWorldCameras()->getPlayerCamera(); btTransform bodyTransform; bodyTransform.setIdentity(); float *translate = camera->getEye(); bodyTransform.setOrigin(btVector3(translate[0],translate[1]-0.79f,translate[2])); btScalar mass(10.); btVector3 localInertia(0,0,0); bodyShape->calculateLocalInertia(mass,localInertia); btDefaultMotionState* myMotionState = new btDefaultMotionState(bodyTransform); btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,bodyShape,localInertia); m_playerBody = new btRigidBody(rbInfo); m_playerBody->setAngularFactor(0); m_dynamicsWorld->addRigidBody(m_playerBody); }
void BulletManager::updateDynamicsWorld(float nSpeed) { Profiler::getInstance()->startProfile("Update Dynamics World"); // move camera WorldState *worldState = (WorldState *) GameState::GAMESTATE; WorldCamera *camera = worldState->getPhysicsManager()->getWorldCameras()->getPlayerCamera(); float nOldEye[3]; nOldEye[0] = camera->getEyeX(); nOldEye[1] = camera->getEyeY(); nOldEye[2] = camera->getEyeZ(); if (InputManager::getInstance()->isKeyDown('w')) { camera->moveForward(nSpeed*0.1f); } if (InputManager::getInstance()->isKeyDown('s')) { camera->moveBackward(nSpeed*0.1f); } if (InputManager::getInstance()->isKeyDown('a')) { camera->moveLeft(nSpeed*0.1f); } if (InputManager::getInstance()->isKeyDown('d')) { camera->moveRight(nSpeed*0.1f); } float *nNewEye = camera->getEye(); m_playerBody->activate(true); m_playerBody->setLinearVelocity(100.0f*btVector3(nNewEye[0]-nOldEye[0],-0.04f,nNewEye[2]-nOldEye[2])); // check physics m_dynamicsWorld->stepSimulation(1.f/60.f,10); // update camera btVector3 camPos = m_playerBody->getWorldTransform().getOrigin(); camera->setPosition(camPos[0],camPos[1]+0.8,camPos[2]); Profiler::getInstance()->endProfile(); }