对象的访问冲突

时间:2014-12-11 18:00:28

标签: c++ arrays oop pointers ogre

我有一个像这样的Entity.h:

使用命名空间physx;

class Entity
{

public:
    Entity(Ogre::Vector3 dims, Ogre::Vector3 pos, std::string mesh, std::string id);
    virtual ~Entity(void);
    virtual void update(Ogre::Real dt);
    virtual void init(Ogre::SceneManager* sceneMgr, PxPhysics* physics, PxScene* scene, PxVec3 velocity=PxVec3(0, 0, 0));
protected:
    Ogre::Entity*       mOgreEntity = NULL;
    Ogre::SceneNode*    mOgreNode = NULL;

    Ogre::Vector3       mPosition;
    Ogre::Vector3       mDimensions;
    std::string         mMesh;
    std::string         mId;


    PxRigidDynamic*     mActor;
    PxMaterial*         mMaterial;
};

这是我的实体来源:

#include "Entity.h"

Entity::Entity(Ogre::Vector3 dims, Ogre::Vector3 pos, std::string mesh, std::string id) 
{

    mDimensions = dims;
    mPosition   = pos;
    mMesh       = mesh;
    mId         = id;
    mActor      = NULL;
    mMaterial   = NULL;
}

Entity::~Entity(void)
{

}

void Entity::update(Ogre::Real dt)
{
    PxVec3 pos = mActor->getGlobalPose().p;
    Ogre::Real r = 0;
    mOgreNode->setPosition(Ogre::Vector3(pos.x + r, pos.y + r, pos.z + r));
}

void Entity::init(Ogre::SceneManager* sceneMgr, PxPhysics* physics, PxScene* scene, PxVec3 velocity)
{
    // Create an Entity
    mOgreEntity = sceneMgr->createEntity(mId, mMesh);
    mOgreEntity->setCastShadows(true);

    // Create a SceneNode and attach the Entity to it
    mOgreNode = sceneMgr->getRootSceneNode()->createChildSceneNode(mId + "Node");
    Ogre::AxisAlignedBox box = mOgreEntity->getBoundingBox();
    Ogre::Vector3 realSizes = box.getSize();

    mOgreNode->setPosition(mPosition);
    mOgreNode->attachObject(mOgreEntity);

    Ogre::Vector3 scaler = Ogre::Vector3(mDimensions.x / realSizes.x, mDimensions.y / realSizes.y, mDimensions.z / realSizes.z);
    mOgreNode->scale(scaler);


    mMaterial = physics->createMaterial(1.5f, 1.5f, 1.0f);
    PxGeometry* geometry = NULL;
    if(mMesh == "sphere.mesh")
    {
        PxGeometry g = PxSphereGeometry(mDimensions.x / 2); // Because it's a radius
        geometry = &g;
    } else {
        // geometry = NULL;
    }
    PxTransform transform = PxTransform(PxVec3(mPosition.x, mPosition.y, mPosition.z));
    mActor = PxCreateDynamic(*physics, transform, *geometry, *mMaterial, PxReal(.1));
    // if(!mActor) {
 //        MessageBox( NULL, "no actor", "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
    //  return;
    // }
    mActor->setLinearVelocity(velocity);
    // And add the actor to a scene:

    scene->addActor(*mActor);

}

现在,如果我创建一个单独的实体并初始化它就可以了。即使是第二个实体var也可以。现在有一个数组:

Entity *mEntities[20];
for(int i = 0 ; i < 20 ; i++ ){
    ostringstream nameStream;
    nameStream << "Sphere_" << i;
    string name = nameStream.str();
    Entity* sphere = new Entity(Ogre::Vector3(i*5, i*4.5, i*6), Ogre::Vector3(i*5, i*4.5, i*6), "sphere.mesh", name);
    sphere->init(mSceneMgr, mPhysics, gScene, PxVec3(-10.0f, 0, 0));
    mEntities[i] = sphere;

}

我得到了Access violation。对于即时调试器,结果是mActor为空以及mMaterial

修改

此代码也不起作用:

    mEntity = Entity(Ogre::Vector3(50.0f, 50.0f, 50.0f), Ogre::Vector3(50.0f, 40.5f, 60.0f), "sphere.mesh", "sphere");
    mEntity.init(mSceneMgr, mPhysics, gScene, PxVec3(-10.0f, 0, 0));

3 个答案:

答案 0 :(得分:1)

<强> 1)

Entity* sphere = new Entity(Ogre::Vector3(i*5, i*4.5, i*6), 
                            Ogre::Vector3(i*5, i*4.5, i*6), 
                            "sphere.mesh", 
                            "Sphere_"+i);

查看"Sphere_"+i 如果i大于“Sphere_”的长度,则表示您将指针传递给某个随机存储器。我假设你想在最后用i创建一个字符串。
请使用sprintfstd::string

<强> 2)
如果你将循环范围从20改为让3说它可能会起作用。问题是你的名字是:
Sphere_,phere_,here_
因为通过执行"Sphere_"+i,您不会在字符串中添加整数。
这是&#34;相等&#34;于:

char *string = "String";
string += 3;

3)
此代码将生成您需要的字符串:

std::ostringstream newStringStream;
newStringStream << "Sphere_" << i;
std::string newString = newStringStream.str();

答案 1 :(得分:1)

这是另一个问题:

PxGeometry* geometry = NULL;
if(mMesh == "sphere.mesh")
{
    geometry = &PxSphereGeometry(mDimensions.x / 2); // Because it's a radius
}

问题在于您要为geometry分配临时值的地址。完成该行代码后,该临时代码就消失了。

可能的解决办法是:

PxGeometry geometry;
if(mMesh == "sphere.mesh")
{
    geometry = PxSphereGeometry(mDimensions.x / 2); // Because it's a radius
}
//...
mActor = PxCreateDynamic(*physics, transform, geometry, *mMaterial, PxReal(.1));

现在geometry不再是指针,而是将几何分配给返回的值,(不是返回值的地址)。

我正在阅读文档:

http://docs.nvidia.com/gameworks/content/gameworkslibrary/physx/apireference/files/classPxSphereGeometry.html

所以PxSphereGeometry(x)是一个构造函数调用。因此,您需要将回复value分配给PxShpereGeometry,而不是PxSphereGeometry*

编辑:您的最新更改也没有达到预期的效果:

 if(mMesh == "sphere.mesh")
 {
     PxGeometry g = PxSphereGeometry(mDimensions.x / 2); // Because it's a radius
     geometry = &g;
  } 

gif()块的本地。您将此g的地址指定给geometry。然后,当该块退出时,g消失,现在您geometry指向不再存在的内容。

您编辑的代码与我给出的答案之间的区别在于我的答案assigns是现有对象的返回值。所以我创建了一个返回值的副本。你在编辑过的代码中所做的不是创建重复,而是指向一个本地对象,如上所述,它在离开作用域后将不存在。

因此,如果您要编写遵循已编辑代码模式的代码并使其有效,则更改将如下所示:

 PxGeometry geometry; 
 if(mMesh == "sphere.mesh")
 {
     PxGeometry g = PxSphereGeometry(mDimensions.x / 2); // Because it's a radius
     geometry = g;  
 } 

然而,这确实是无关紧要的工作。原来的答案就足够了。

答案 2 :(得分:0)

我尝试了另一种方法来创建一个僵硬的身体,它工作!!!!

mActor = physics->createRigidDynamic(PxTransform(PxVec3(mPosition.x, mPosition.y, mPosition.z)));
PxShape* shape = mActor->createShape(PxSphereGeometry(mDimensions.x / 2), *mMaterial);
PxRigidBodyExt::updateMassAndInertia(*mActor, 0.4f);