如何在 Objective-C 中访问 Box2d 的 b2CollideCircles?
我正在尝试使用 Box2d 测试一些碰撞检测(使用 此方法) 。我已将源代码和标头添加到我的 XCode 项目中,并向我的标头搜索路径添加了递归 ./**
。看起来构建得很好。
不幸的是,由于我不明白的原因,我无法直接使用碰撞检测方法。
CollisionTest.h:
#import <Foundation/Foundation.h>
@interface CollisionTest : NSObject
-(void)test;
@end
CollisionTest.mm:
(正确重命名为 mm 以访问 C++ 代码)
#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
圆设置代码运行良好,但编译器在 b2CollideCircles
行上出现“没有调用‘b2CollideCircles’的匹配函数”的错误。
默认情况下,Box2d.h
似乎不包含 b2Collision.h
,因此我尝试将其添加到我的实现文件中,作为 #import "b2Collision.h "
以及#import
,但错误仍然相同。
如何直接访问碰撞方法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误消息是编译器找不到适合我正在使用的签名的方法 - 我传递的变量不适合声明的方法参数的类型,并且由于 C++ 允许方法重载,因此它不会注册为相同的方法方法。
正确定义变量(C++ 不是 Objective-C)并仔细阅读方法签名会生成运行良好的代码:
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: