1

I am currently trying to make two arms on a character and use NxRevoluteJoint for their movement. I have these working perfectly in another program that has been given as an example and I have used the same code in this new project however I am getting an error (the one in the title) and I am struggling how to fix it. I understand that the pointers is reference to NULL at some place but I can't see how to sort it out.

The variables are set globally:

NxRevoluteJoint* playerLeftJoint= 0;
NxRevoluteJoint* playerRightJoint= 0;

This is the code in the seperate function where the player is being built as a compound object:

NxVec3 globalAnchor(0,1,0);     
NxVec3 globalAxis(0,0,1);       

playerLeftJoint= CreateRevoluteJoint(0,actor2,globalAnchor,globalAxis);
playerRightJoint= CreateRevoluteJoint(0,actor2,globalAnchor,globalAxis);


//set joint limits
NxJointLimitPairDesc limit1;
limit1.low.value = -0.3f;
limit1.high.value = 0.0f;
playerLeftJoint->setLimits(limit1);


NxJointLimitPairDesc limit2;
limit2.low.value = 0.0f;
limit2.high.value = 0.3f;
playerRightJoint->setLimits(limit2);    

NxMotorDesc motorDesc1;
motorDesc1.velTarget = 0.15;
motorDesc1.maxForce = 1000;
motorDesc1.freeSpin = true;
playerLeftJoint->setMotor(motorDesc1);

NxMotorDesc motorDesc2;
motorDesc2.velTarget = -0.15;
motorDesc2.maxForce = 1000;
motorDesc2.freeSpin = true;
playerRightJoint->setMotor(motorDesc2);

The line where I am getting the error is at the playerLeftJoint->setLimits(limit1);

4

1 回答 1

1

CreateRevoluteJoint返回一个空指针,就这么简单。错误消息清楚地表明指针的值为0。当然,你没有发布那个功能,所以这是我能给你的最好的信息。因此,这条线;

playerLeftJoint->setLimits(limit1);

取消引用指针playerLeftJoint,这是一个无效的指针。您需要初始化您的指针。我看不到你的整个程序结构,所以在这种情况下,最简单的修复是这样的;

if(!playerLeftJoint)
    playerLeftJoint = new NxRevoluteJoint();

// same for the other pointer, now they are valid

此外,由于这是 C++ 而不是 C,请使用智能指针为您处理内存,即

#include <memory>

std::unique_ptr<NxRevoluteJoint> playerLeftJoint;

// or, if you have a custom deallocater...
std::unique_ptr<NxRevoluteJoint, RevoluteJointDeleter> playerLeftJoint;

// ...

playerLeftJoint.reset(new NxRevoluteJoint(...));
于 2013-01-01T18:52:22.080 回答