I am trying to create an FPS player with RigidBody using OpenGL and BulletPhysics. The only problem is making the box move. I tried this with setLinearForce, applyForce and many others, but just don't want to move. He still responds to collisions, but if he moves due to a collision (I throw the ball to check his physics) and I press any movement button on my keyboard, it just stops. And if he is still and I press the button to make him move, he simply will not react (remains static).
Here is the class for the player.
#ifndef PLAYER_H #define PLAYER_H #include <iostream> #include <vector> //BulletPhysics #include "Bullet\src\btBulletDynamicsCommon.h" //SDL and OpenGL #include "Link\GLUT\include\glut.h" #include "Link\GLUT\include\GL.h" #include "Link\GLUT\include\GLU.h" #include "Link\SDL\include\SDL.h" using namespace std; class Player { public: btRigidBody* body; btTransform t; btMotionState* motion; Player(); ~Player(); void init(btDynamicsWorld* world, float x, float y, float z, float mass); void RefreshPosition(float x, float z); void Render(); void FreeMemory(btDynamicsWorld* world); }; Player::Player(){} Player::~Player(){} void Player::init(btDynamicsWorld* world, float x, float y, float z, float mass) { t.setIdentity(); t.setOrigin(btVector3(x,y,z)); btBoxShape* obj=new btBoxShape(btVector3(3/2.0, 7/2.0, 3/2.0)); btVector3 inertia(0,0,0); if(mass!=0.0) obj->calculateLocalInertia(mass,inertia); motion=new btDefaultMotionState(t); btRigidBody::btRigidBodyConstructionInfo info(mass,motion,obj,inertia); body=new btRigidBody(info); body->forceActivationState(DISABLE_DEACTIVATION); body->setAngularFactor(0.0); body->setSleepingThresholds(0.0, 0.0); world->addRigidBody(body); } void Player::RefreshPosition(float x, float z) { body->getMotionState()->getWorldTransform(t); Uint8* state=SDL_GetKeyState(NULL); if(state[SDLK_w]) { body->setLinearVelocity(btVector3(x*20, body->getLinearVelocity().y(), z*20)); } body->getMotionState()->setWorldTransform(t); body->setCenterOfMassTransform(t); } void Player::Render() { if(body->getCollisionShape()->getShapeType()!=BOX_SHAPE_PROXYTYPE) return; glColor3f(0.0, 1.0, 0.0); btVector3 extent=((btBoxShape*)body->getCollisionShape())->getHalfExtentsWithMargin(); btTransform t; body->getMotionState()->getWorldTransform(t); float mat[16]; t.getOpenGLMatrix(mat); glPushMatrix(); glMultMatrixf(mat); glBegin(GL_QUADS); glVertex3f(-extent.x(), extent.y(), -extent.z()); glVertex3f(-extent.x(), -extent.y(), -extent.z()); glVertex3f(-extent.x(), -extent.y(), extent.z()); glVertex3f(-extent.x(), extent.y(), extent.z()); glEnd(); glBegin(GL_QUADS); glVertex3f(extent.x(), extent.y(), -extent.z()); glVertex3f(extent.x(), -extent.y(), -extent.z()); glVertex3f(extent.x(), -extent.y(), extent.z()); glVertex3f(extent.x(), extent.y(), extent.z()); glEnd(); glBegin(GL_QUADS); glVertex3f(-extent.x(), -extent.y(), -extent.z()); glVertex3f(extent.x(), -extent.y(), -extent.z()); glVertex3f(extent.x(), -extent.y(), extent.z()); glVertex3f(-extent.x(), -extent.y(), extent.z()); glEnd(); glBegin(GL_QUADS); glVertex3f(-extent.x(), extent.y(), -extent.z()); glVertex3f(extent.x(), extent.y(), -extent.z()); glVertex3f(extent.x(), extent.y(), extent.z()); glVertex3f(-extent.x(), extent.y(), extent.z()); glEnd(); glBegin(GL_QUADS); glVertex3f(-extent.x(), extent.y(), -extent.z()); glVertex3f(-extent.x(), -extent.y(), -extent.z()); glVertex3f(extent.x(), -extent.y(), -extent.z()); glVertex3f(extent.x(), extent.y(), -extent.z()); glEnd(); glBegin(GL_QUADS); glVertex3f(-extent.x(), extent.y(), extent.z()); glVertex3f(-extent.x(), -extent.y(), extent.z()); glVertex3f(extent.x(), -extent.y(), extent.z()); glVertex3f(extent.x(), extent.y(), extent.z()); glEnd(); glPopMatrix(); } void Player::FreeMemory(btDynamicsWorld* world) { world->removeCollisionObject(body); delete body->getMotionState(); delete body->getCollisionShape(); } #endif
Ok, this is class. Note that RefreshPosition takes two arguments: the position of x and z (I don't want to change the position of y). These two variables represent the direction (from 0 to 1) of the player and are multiplied by 20 to get the speed (maximum value = 20).
Hope I gave you enough information ....