0

I'm have a weird issue with a pair of GLKVector3 structs. I get them from two calls to an objc method axisForIndex:, then pass them to a C function getContactPoint to perform a computation. However, when the argument of both calls to axisForIndex: are zero, their values "vanish" after I enter the method -- that is, the value of first.x might be .55 outside the method, but it is 0 inside. When I look at them in the debugger, none of their fields are filled in, and when I print description I get something like (GLKVector3) varName = <variable not available>. However, checking the values in the method calling getContactPoint before and after it is called shows that they are as they should be. I'm using Xcode6-Beta6 to compile and run the code.

Here's the code:

    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;

Here is the implementation of getContactPoint, the method where the values are undefined.

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));
}

Implementation of axisForIndex.

    -(GLKVector3) axisForIndex: (NSUInteger) index
   {

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

        return GLKVector3Make(vec.x, vec.y, vec.z);
   }
Ben Pious
  • 4,765
  • 2
  • 22
  • 34

2 Answers2

0

The reason you're receiving <variable not available> in the debugger may be due to compiler optimization.

Will you try turning off compiler optimization (Build Setting > Optimization Level) for your build and running the debugger again?

See: https://stackoverflow.com/a/13041747/3367343

Community
  • 1
  • 1
Travis Hohl
  • 2,176
  • 2
  • 14
  • 15
  • Optimization is not on. Also, my question isn't that I can't see the values of the structs in lldb, rather that they are *wrong* -- the structs value has "vanished" and isn't what it was when I entered the method. Them not being available is just an interesting symptom of the underlying problem. – Ben Pious Aug 25 '14 at 21:12
  • I understand your question -- I would have preferred to make this a comment, but Stack Overflow won't allow me to comment until I have a 50 reputation. It feels like there's a function returning `nil` where it shouldn't -- I will run some tests using your code this evening. – Travis Hohl Aug 25 '14 at 21:45
  • I haven't been able to reproduce the problem... I added `NSLog(@"%f", axisOne.x);` to the first line of the function definition of `getContactPoint` and I hardcoded the value of the argument of both calls to `axisForIndex:` to zero, but NSLog prints the expected value (in my case "0.5"). Setting a breakpoint before `getContactPoint` and stepping into the function with the debugger, I found the same result. What am I missing? – Travis Hohl Aug 27 '14 at 01:00
0

Changing the arguments of getContactPoint to pointers and then dereferencing them inside the method, and passing in pointers to the structs I want solves the issue.

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

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

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

I would be interested in knowing why this is the case.

Ben Pious
  • 4,765
  • 2
  • 22
  • 34