4

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).

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 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).

I hope I gave you sufficient information....

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Tiago Salzmann
  • 184
  • 2
  • 14
  • Can you give a more concise snippet that represents the error, or remove the referenced elements not included in the code so we can test it more easily? – BoppreH Nov 23 '12 at 00:47
  • the only problem is that it does not work (setLinearVelocity). Sometimes i run the program and it works for just 30 seconds at the beginning and then it stops and don't respond to the keyboard – Tiago Salzmann Nov 23 '12 at 18:03
  • I know this is a very old thread but I am having the exact same issue where when I move the object, it moves but the moment it stops I cant move the object anymore. I want to know how you solved this issue. Thanks – Gasim Dec 06 '13 at 13:10

1 Answers1

4

Perhaps the object becomes deactivated and then it doesn't move anymore. Then you have to activate it again (search in the documentation how to activate a rigid body with the setActivationState() method)

Objects become deactivated when they become almost stopped. Then they don't activate again until another object applies them a force or you do it programmatically.

yombo
  • 334
  • 2
  • 5