我正在搞乱我在网上找到的一些代码,添加了一个静态属性,现在它没有编译(虽然没有任何语法错误)。
以下是代码的重要部分:
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
答案 0 :(得分:2)
您已声明“有一个名为MyMotionState::currentNode
的变量”,但您还需要在某处定义变量:
SceneNode *MyMotionState::currentNode;
在一些.cpp文件中。这是因为您可能希望该变量“特定于某个地方”(在特定的.cpp文件中),并且编译器不能将它放在任意位置 - 如果在11个不同的地方包含“physics.h”,编译器如何知道应该在哪里?你当然不会想要其中的11个......;)
答案 1 :(得分:2)
尝试将这样的内容添加到.cpp文件中。
SceneNode* MyMotionState::currentNode = nullptr;