BulletPhysics "setLinearVelocity" not moving

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 "BulletsrcbtBulletDynamicsCommon.h"

//SDL and OpenGL
#include "LinkGLUTincludeglut.h"
#include "LinkGLUTincludeGL.h"
#include "LinkGLUTincludeGLU.h"
#include "LinkSDLincludeSDL.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....


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.

链接地址: http://www.djcxy.com/p/66164.html

上一篇: 用64位双精度编译BulletPhysics

下一篇: BulletPhysics“setLinearVelocity”不动