0

我正在搞乱我在网上找到的一些代码,添加了一个静态属性,现在它无法编译(尽管没有任何语法错误)。

这是代码的重要部分:

public:
    static SceneNode* currentNode;
    MyMotionState(const btTransform & initialPos, SceneNode * node)
    {
        currentNode = 0;
        mVisibleObj = node;
        mTransform = initialPos;
        //mCOM = btTransform::getIdentity();
    }

我收到了这个错误:

未定义对“MyMotionState::currentNode”的引用

我也试过了MyMotionState::currentNode,并且currentNode没有提到类本身。

我听说这是因为 Eclipse 的解析器已经过时了。我试图重建和刷新索引,但我仍然有同样的错误。

这是完整的代码:

#ifndef _PHYSICS_H_
#define _PHYSICS_H_

using namespace Ogre;

#include <sstream>
#include "TextRenderer.h"


class MyMotionState : public btDefaultMotionState
{
    protected:
        SceneNode * mVisibleObj;
        btTransform mTransform;
        //btTransform mCOM;

    public:
        static SceneNode* currentNode;
        MyMotionState(const btTransform & initialPos, SceneNode * node)
        {
            currentNode = 0;
            mVisibleObj = node;
            mTransform = initialPos;
            //mCOM = btTransform::getIdentity();
        }

        MyMotionState(const btTransform & initialPos)
        {
            mVisibleObj = NULL;
            mTransform = initialPos;
        }

        ~MyMotionState() {}

        void setNode(SceneNode * node)
        {
            mVisibleObj = node;
        }

        btTransform getWorldTransform() const
        {
            return  mTransform;
        }

        void getWorldTransform(btTransform & worldTrans) const
        {
            worldTrans = mTransform;
        }

        void setWorldTransform(const btTransform & worldTrans)
        {
            if (mVisibleObj == NULL)
                return;

            if (MyMotionState::currentNode != 0)
            {
                TextRenderer::getSingleton().printf("nigger", "Y: %f\ny: %f\nx: %f", MyMotionState::currentNode->getPosition().x, MyMotionState::currentNode->getPosition().y, MyMotionState::currentNode->getPosition().y);
            }

            mTransform = worldTrans;
            btTransform transform = mTransform;
            btQuaternion rot = transform.getRotation();
            btVector3 pos = transform.getOrigin();
            mVisibleObj->setOrientation(rot.w(), rot.x(), rot.y(), rot.z());
            mVisibleObj->setPosition(pos.x(), pos.y(), pos.z());
        }
};

class MyPhysics
{
    private:
        btAlignedObjectArray<btCollisionShape*> mCollisionShapes;
        btBroadphaseInterface * mBroadphase;
        btCollisionDispatcher * mDispatcher;
        btConstraintSolver * mSolver;
        btDefaultCollisionConfiguration * mColConfig;
        btDiscreteDynamicsWorld * mWorld;
        SceneNode * mRootSceneNode;

        int counter;

    public:
        MyPhysics()
        {
            counter = 1;
            mColConfig = new btDefaultCollisionConfiguration;
            mDispatcher = new btCollisionDispatcher(mColConfig);
            mBroadphase = new btDbvtBroadphase;
            mSolver = new btSequentialImpulseConstraintSolver;
            mWorld = new btDiscreteDynamicsWorld(mDispatcher, mBroadphase, mSolver, mColConfig);

            mWorld->setGravity(btVector3(0, -100000000000, 0));
            mRootSceneNode = 0;
        }

        ~MyPhysics()
        {
            for (int i = mWorld->getNumCollisionObjects() - 1; i >= 0; i--)
            {
                btCollisionObject * obj = mWorld->getCollisionObjectArray()[i];
                btRigidBody * body = btRigidBody::upcast(obj);

                if (body && body->getMotionState())
                    delete body->getMotionState();

                mWorld->removeCollisionObject(obj);

                delete obj;
            }

            delete mWorld;
            delete mSolver;
            delete mBroadphase;
            delete mDispatcher;
            delete mColConfig;
        }

        void addStaticPlane(SceneNode * node)
        {
            btCollisionShape * groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 50);
            mCollisionShapes.push_back(groundShape);
            btTransform groundTransform;
            groundTransform.setIdentity();
            groundTransform.setOrigin(btVector3(0, -50, 0));

            MyMotionState * motionState = new MyMotionState(groundTransform, node);
            btRigidBody::btRigidBodyConstructionInfo rbInfo(0, motionState, groundShape, btVector3(0, 0, 0));
            btRigidBody * body = new btRigidBody(rbInfo);

            mWorld->addRigidBody(body);
        }

        void addStaticPlane2(SceneNode * node)
        {
            btCollisionShape * groundShape = new btBoxShape(btVector3(5, 0, 5));
            mCollisionShapes.push_back(groundShape);
            btTransform groundTransform;
            groundTransform.setIdentity();
            groundTransform.setOrigin(btVector3(0, 3, 0));

            MyMotionState * motionState = new MyMotionState(groundTransform, node);
            btRigidBody::btRigidBodyConstructionInfo rbInfo(0, motionState, groundShape, btVector3(0, 0, 0));
            btRigidBody * body = new btRigidBody(rbInfo);

            mWorld->addRigidBody(body);
        }

        btRigidBody * addDynamicBox(SceneNode * node, float m = 1.0f)
        {
            btCollisionShape * colShape = new btBoxShape(btVector3(1, 1, 1));
            mCollisionShapes.push_back(colShape);
            btTransform boxTransform;
            boxTransform.setIdentity();

            btScalar mass(m);
            btVector3 localInertia(0, 0, 0);

            colShape->calculateLocalInertia(mass, localInertia);

            boxTransform.setOrigin(btVector3(node->getPosition().x, node->getPosition().y,node->getPosition().z));

            MyMotionState * motionState = new MyMotionState(boxTransform, node);
            btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, motionState, colShape, localInertia);
            btRigidBody * body = new btRigidBody(rbInfo);

            mWorld->addRigidBody(body);

            return body;
        }

        btRigidBody * addRigidBody(btTransform transform, btCollisionShape * shape, btScalar mass, SceneNode * node = NULL)
        {
            mCollisionShapes.push_back(shape);
            btVector3 localInertia(0, 0, 0);

            shape->calculateLocalInertia(mass, localInertia);
            MyMotionState * motionState = new MyMotionState(transform, node);
            btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, motionState, shape, localInertia);
            btRigidBody * body = new btRigidBody(rbInfo);

            mWorld->addRigidBody(body);

            return body;
        }

        void addCollisionShape(btCollisionShape * colShape)
        {
            mCollisionShapes.push_back(colShape);
        }

        btDiscreteDynamicsWorld * getDynamicsWorld()
        {
            return mWorld;
        }

        btCollisionWorld * getCollisionWorld()
        {
            return mWorld->getCollisionWorld();
        }

        btBroadphaseInterface * getBroadphase()
        {
            return mBroadphase;
        }

        void setRootSceneNode(SceneNode * node)
        {
            mRootSceneNode = node;
        }

        btVector3 toBullet(const Vector3 & vec) const
        {
            return btVector3(vec.x, vec.y, vec.z);
        }

        void shootBox(const Vector3 & camPosition)
        {
            if (mRootSceneNode)
            {

                std::stringstream ss;
                ss << counter++;
                SceneNode * node = mRootSceneNode->createChildSceneNode(camPosition);
                MyMotionState::currentNode = node;
                Entity * entity = Root::getSingleton().getSceneManager("nigger-manager")->createEntity("Head"+ ss.str(), "ogrehead.mesh");
                node->scale(0.2, 0.2, 0.2);
                node->attachObject(entity);
                btRigidBody * box = addDynamicBox(node);
                box->applyCentralImpulse(btVector3(10, 0, 0));
            }
        }

        static void debugBtVector3(const btVector3 & vec, const char * str = 0)
        {
            std::cout << str << " x: " << vec.x() << "; y: " << vec.y() << "; z: " << vec.z() << std::endl;
        }
};

#include "physics_debugdraw.h"
#endif
4

2 回答 2

2

您已声明“有一个名为MyMotionState::currentNode”的变量,但您还需要在某处定义该变量:

SceneNode *MyMotionState::currentNode; 

在一些 .cpp 文件中。这是因为您可能希望该变量“在某个特定的地方”(在特定的 .cpp 文件中),并且编译器不能将其放置在任意位置 - 如果您在 11 个不同的位置包含“physics.h”,编译器如何知道它应该在哪里?你当然不想要其中的 11 个……;)

于 2013-06-16T21:23:30.297 回答
2

尝试将这样的内容添加到您的 .cpp 文件中。

SceneNode* MyMotionState::currentNode = nullptr;
于 2013-06-16T21:25:17.033 回答