[英]Struct has undefined value after being passed into a method
我对一对GLKVector3结构有一个奇怪的问题。 我通过两次调用objc方法axisForIndex:
来获取它们,然后将它们传递给C函数getContactPoint
以执行计算。 但是,当两个对axisForIndex:
调用的参数均为零时,进入方法后它们的值“消失”-也就是说,方法外部的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的实现,该方法的值未定义。
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);
}
在调试器中收到<variable not available>
的原因可能是由于编译器优化所致。
您会尝试为您的构建关闭编译器优化(构建设置>优化级别),然后再次运行调试器吗?
将getContactPoint的参数更改为指针,然后在方法内部对其取消引用,然后将指针传递给我想要的结构即可解决此问题。
GLKVector3 getContactPoint(GLKVector3* axisOnePointer,
GLKVector3* axisTwoPointer,
GLKVector3* ptOnEdgeOnePointer,
GLKVector3* ptOnEdgeTwoPointer) {
GLKVector3 axisOne = *axisOnePointer;
GLKVector3 axisTwo = *axisTwoPointer;
GLKVector3 ptOnEdgeOne = *ptOnEdgeOnePointer;
GLKVector3 ptOnEdgeTwo = *ptOnEdgeTwoPointer;
我想知道为什么会这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.