我的游戏中有很多精灵,所有精灵都在b2world中。为了检测触摸我做下一个:
currentPosition = [[CCDirector sharedDirector] convertToGL: currentPosition];
b2Vec2 locationWorld = b2Vec2(currentPosition.x/PTM_RATIO, currentPosition.y/PTM_RATIO);
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
b2Fixture *bf1 = b->GetFixtureList();
if (bf1->TestPoint(locationWorld) )
{
CCSprite *tempSprite = (CCSprite *) b->GetUserData();
if (tempSprite.tag==2 )
{
现在,因为我的精灵的身体太小了,而且他正在移动,所以当它移动时很难触摸它,所以我需要更改这段代码,以便检测广域也围绕这个精灵。 我如何将测试点扩展为+ - 更多50像素?
非常感谢。
答案 0 :(得分:0)
您可以将更大的夹具连接到主体,并将夹具的传感器标志设置为true。传感器夹具不会改变任何物理,但你可以检查一个点是否落在其边界内。
您可以像这样创建一个传感器夹具(来自SensorTest.h):
b2CircleShape shape;
shape.m_radius = 5.0f;
shape.m_p.Set(0.0f, 10.0f);
b2FixtureDef fd;
fd.shape = &shape;
fd.isSensor = true;
body->CreateFixture(&fd);
参见Box2D手册,第6.3节(PDF)和Testbed中包含的SensorTest.h。