在传递给方法后,Struct具有未定义的值

时间:2014-08-25 19:32:53

标签: objective-c c

我对一对GLKVector3结构有一个奇怪的问题。我通过两次调用objc方法axisForIndex:得到它们,然后将它们传递给C函数getContactPoint来执行计算。但是,当两个调用axisForIndex:的参数都为零时,它们的值&#34;消失了#34;在我输入方法之后 - 也就是说,first.x的值可能是方法之外的.55,但它在内部是0。当我在调试器中查看它们时,它们的所有字段都没有被填充,当我打印描述时,我得到类似(GLKVector3) varName = <variable not available>的内容。但是,在调用它之前和之后检查调用getContactPoint的方法中的值表明它们应该是这样的。我使用Xcode6-Beta6编译并运行代码。

以下是代码:

    GLKVector3 first = [self axisForIndex: oneAxisIndex]; // the structs I want to use
    GLKVector3 second = [other axisForIndex: twoAxisIndex];

    //in this method they have garbage values
    result.contactLocation = getContactPoint(first, 
                                             second,
                                             ptOnEdgeOne,
                                             ptOnEdgeTwo);

    //test to see if the result was computed correctly
    if (result.contactLocation.x == 0 && result.contactLocation.y == 0 && result.contactLocation.z == 0) {

        NSLog(@"%ld,%ld", (unsigned long)oneAxisIndex, (unsigned long)twoAxisIndex);
    }

    result.interpenetration = bestOverlap;

    return result;

这是getContactPoint的实现,getContactPoint是未定义值的方法。

GLKVector3 getContactPoint(GLKVector3 axisOne,
                       GLKVector3 axisTwo,
                       GLKVector3 ptOnEdgeOne,
                       GLKVector3 ptOnEdgeTwo) {

GLKVector3 toSt = GLKVector3Subtract(ptOnEdgeOne, ptOnEdgeTwo);

GLfloat dpStaOne = GLKVector3DotProduct(axisOne, toSt);
GLfloat dpStaTwo = GLKVector3DotProduct(axisTwo, toSt);

GLfloat smOne = GLKVector3Length(axisOne);
GLfloat smTwo = GLKVector3Length(axisTwo);

GLfloat dotProductEdges = GLKVector3DotProduct(axisTwo, axisOne);

GLfloat denom = smOne * smTwo - dotProductEdges * dotProductEdges;

GLfloat a = (dotProductEdges * dpStaOne - smTwo * dpStaOne)/denom;
GLfloat b = (smOne * dpStaTwo - dotProductEdges * dpStaOne)/denom;

GLKVector3 nearestPtOnOne = GLKVector3MultiplyScalar(GLKVector3Add(ptOnEdgeOne, axisOne), a);
GLKVector3 nearestPtOnTwo = GLKVector3MultiplyScalar(GLKVector3Add(ptOnEdgeTwo, axisTwo), b);

return GLKVector3Add(GLKVector3MultiplyScalar(nearestPtOnOne, 0.5), GLKVector3MultiplyScalar(nearestPtOnTwo, 0.5));
}

实现axisForIndex。

    -(GLKVector3) axisForIndex: (NSUInteger) index
   {

        GLKVector4 vec = GLKMatrix4GetColumn(self.transformationMatrix, index);

        return GLKVector3Make(vec.x, vec.y, vec.z);
   }

2 个答案:

答案 0 :(得分:0)

您在调试器中收到<variable not available>的原因可能是编译器优化。

您是否会尝试为构建关闭编译器优化(Build Setting&gt; Optimization Level)并再次运行调试器?

请参阅:https://stackoverflow.com/a/13041747/3367343

答案 1 :(得分:0)

将getContactPoint的参数更改为指针,然后在方法内取消引用它们,并将指针传递给我想要的结构解决问题。

GLKVector3 getContactPoint(GLKVector3* axisOnePointer,
                           GLKVector3* axisTwoPointer,
                           GLKVector3* ptOnEdgeOnePointer,
                           GLKVector3* ptOnEdgeTwoPointer) {

GLKVector3 axisOne = *axisOnePointer;
GLKVector3 axisTwo = *axisTwoPointer;

GLKVector3 ptOnEdgeOne = *ptOnEdgeOnePointer;
GLKVector3 ptOnEdgeTwo = *ptOnEdgeTwoPointer;

我很想知道为什么会这样。