Note that there are some explanatory texts on larger screens.

plurals
  1. POBulletPhysics "setLinearVelocity" not moving
    primarykey
    data
    text
    <p>I'm trying to create an FPS player that has a RigidBody using OpenGL and BulletPhysics. The only problem is to make the box move. I tried it with setLinearForce, applyForce and many others but is just don't want to move. It still responds to collisions but if it is moving because of a collision (I'm throwing a ball to test its physics) and I press any movement button on my keyboard it just stops. And if it is still and i press the button to make it move it just won't react (stays static).</p> <p>Here is the class for the player.</p> <pre class="lang-cpp prettyprint-override"><code>#ifndef PLAYER_H #define PLAYER_H #include &lt;iostream&gt; #include &lt;vector&gt; //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-&gt;calculateLocalInertia(mass,inertia); motion=new btDefaultMotionState(t); btRigidBody::btRigidBodyConstructionInfo info(mass,motion,obj,inertia); body=new btRigidBody(info); body-&gt;forceActivationState(DISABLE_DEACTIVATION); body-&gt;setAngularFactor(0.0); body-&gt;setSleepingThresholds(0.0, 0.0); world-&gt;addRigidBody(body); } void Player::RefreshPosition(float x, float z) { body-&gt;getMotionState()-&gt;getWorldTransform(t); Uint8* state=SDL_GetKeyState(NULL); if(state[SDLK_w]) { body-&gt;setLinearVelocity(btVector3(x*20, body-&gt;getLinearVelocity().y(), z*20)); } body-&gt;getMotionState()-&gt;setWorldTransform(t); body-&gt;setCenterOfMassTransform(t); } void Player::Render() { if(body-&gt;getCollisionShape()-&gt;getShapeType()!=BOX_SHAPE_PROXYTYPE) return; glColor3f(0.0, 1.0, 0.0); btVector3 extent=((btBoxShape*)body-&gt;getCollisionShape())-&gt;getHalfExtentsWithMargin(); btTransform t; body-&gt;getMotionState()-&gt;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-&gt;removeCollisionObject(body); delete body-&gt;getMotionState(); delete body-&gt;getCollisionShape(); } #endif </code></pre> <p>Ok, this is the class. Note that the RefreshPosition takes two arguments: the x and z position (I don't want to change the y position). This two variables represent the direction (0 to 1) of the player and are multiplied by 20 to result the velocity (maximum value = 20). </p> <p>I hope I gave you sufficient information....</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload