身体不碰撞
我设置了2个物体,一个有重力的世界,一个带有步骤的刻度方法,所有的物体都移动得很好,我可以对它们施加力,重力也在起作用。
但没有碰撞。 当 body1 击中地面时,他只是走出屏幕,而不是击中它并像真正的球一样跳跃。 当 body1 击中 body2 时,就继续运动,因为什么也没有发生。
身体有形状,世界有边缘,但没有碰撞。 我在这里缺少什么?
以下是我从 init 调用的一些函数:
- (void)addBoxBodyForSprite:(CCSprite *)sprite {
b2BodyDef spriteBodyDef;
spriteBodyDef.type = b2_dynamicBody;
spriteBodyDef.position.Set(sprite.position.x/PTM_RATIO,sprite.position.y/PTM_RATIO);
spriteBodyDef.userData = sprite;
spriteBody = world->CreateBody(&spriteBodyDef);
b2PolygonShape spriteShape;
spriteShape.SetAsBox(sprite.contentSize.width/PTM_RATIO/2,sprite.contentSize.height/PTM_RATIO/2);
b2FixtureDef spriteShapeDef;
spriteShapeDef.shape = &spriteShape;
spriteShapeDef.density = 10.0;
spriteShapeDef.isSensor = true;
spriteBody->CreateFixture(&spriteShapeDef);
}
-(void)worldEdge
{
CGSize winSize = [CCDirector sharedDirector].winSize;
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
b2Body *groundBody = world->CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
b2FixtureDef boxShapeDef;
boxShapeDef.shape = &groundBox;
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
}
-(void)tick:(ccTime) dt
{
world->Step(dt,10,10);
for(b2Body *b=world->GetBodyList(); b; b=b->GetNext())
{
if(b->GetUserData() !=NULL )
{
CCSprite *sprite=(CCSprite *) b->GetUserData();//every b of the world will be update his position
sprite.position=ccp( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO ) ;
sprite.rotation=-1*CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
一切都进展顺利,但从未发生过碰撞。
i have set 2 bodies, a world with gravity,a tick method with step,and all the bodies are moving just fine,and i can apply forces on them, gravity is also working.
but no collision.
when body1 hit the ground he just go out of screen and not hit it and jump like a real ball.
when body1 hit body2 the just continue movement as nothing was happen.
the bodies have shape ,the world have edges, but no collision.
what am i missing here ?
here are some functions that i call from the init :
- (void)addBoxBodyForSprite:(CCSprite *)sprite {
b2BodyDef spriteBodyDef;
spriteBodyDef.type = b2_dynamicBody;
spriteBodyDef.position.Set(sprite.position.x/PTM_RATIO,sprite.position.y/PTM_RATIO);
spriteBodyDef.userData = sprite;
spriteBody = world->CreateBody(&spriteBodyDef);
b2PolygonShape spriteShape;
spriteShape.SetAsBox(sprite.contentSize.width/PTM_RATIO/2,sprite.contentSize.height/PTM_RATIO/2);
b2FixtureDef spriteShapeDef;
spriteShapeDef.shape = &spriteShape;
spriteShapeDef.density = 10.0;
spriteShapeDef.isSensor = true;
spriteBody->CreateFixture(&spriteShapeDef);
}
-(void)worldEdge
{
CGSize winSize = [CCDirector sharedDirector].winSize;
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
b2Body *groundBody = world->CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
b2FixtureDef boxShapeDef;
boxShapeDef.shape = &groundBox;
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
}
-(void)tick:(ccTime) dt
{
world->Step(dt,10,10);
for(b2Body *b=world->GetBodyList(); b; b=b->GetNext())
{
if(b->GetUserData() !=NULL )
{
CCSprite *sprite=(CCSprite *) b->GetUserData();//every b of the world will be update his position
sprite.position=ccp( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO ) ;
sprite.rotation=-1*CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
everything is moving allright but no collision ever.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
具体问题是这一行:
在box2d中,当你将fixture设置为传感器时,它不会与其他fixture发生碰撞。
The specific problem is this line:
In box2d, when you set an fixture as a sensor, it will not collide with other fixtures.