1

I'm trying to test out some collision detection with Box2d (using this method). I've added the source and headers to my XCode project, and have added a recursive ./** to my header search paths. It appears to build fine.

Unfortunately, I can't use the collision detection methods directly, for reasons I don't understand.

CollisionTest.h:

#import <Foundation/Foundation.h>

@interface CollisionTest : NSObject
-(void)test;
@end

CollisionTest.mm: (renamed to mm properly for access to C++ code)

#import "CollisionTest.h"
#import "Box2D.h"

@implementation CollisionTest

-(void)test {
  b2CircleShape *circle1 = new b2CircleShape();
  circle1->m_radius = 5.0;
  b2Vec2 circle1Pos(0.0, 0.0);
  b2Transform *transform1 = new b2Transform();
  transform1->Set(circle1Pos, 0.0);
  ... ditto for circle2 ...

  b2Manifold *manifold = new b2Manifold();
  b2CollideCircles(&manifold, circle1, transform1, circle2, transform2);
  if (manifold->pointCount > 0){
    NSLog(@"collided");
  } else {
    NSLog(@"no collision");
  }
}

@end

The circle setup code runs fine, but the compiler craps out on the b2CollideCircles line with "No matching function for call to 'b2CollideCircles'".

Box2d.h does not seem to include b2Collision.h by default, so I've tried adding it to my implementation file both as #import "b2Collision.h" as well as #import <Box2d/Collision/b2Collision.h>, but the error remains the same.

How do I access the collision methods directly?

Ian Terrell
  • 10,667
  • 11
  • 45
  • 66

1 Answers1

1

The error message was that the compiler couldn't find a method that fit the signature I was using—the variables I was passing did not fit the types of the declared method parameters, and since C++ allows method overloading it did not register as the same method.

Properly defining the variables (C++ is not Objective-C) and carefully reading the method signature produces code that works fine:

b2CircleShape circle1;
circle1.m_radius = 5.0;
b2Vec2 circle1Pos(10.01, 0.0);
b2Transform transform1;
transform1.Set(circle1Pos, 0.0);
...
b2Manifold manifold;
b2CollideCircles(&manifold, &circle1, transform1, &circle2, transform2);
Ian Terrell
  • 10,667
  • 11
  • 45
  • 66