Box2d 与 cocos2d 的联合
我是 box2d 的新手,我尝试在两个身体之间创建关节。 我写了一个像这样的关节
b2RevoluteJointDef jointDef;
jointDef.bodyA=worm_head;
jointDef.bodyB=worm_body;
jointDef.lowerAngle = -0.25f * b2_pi; // -45 degrees
jointDef.upperAngle = 0.25f * b2_pi; // 45 degrees
jointDef.enableLimit=true;
jointDef.maxMotorTorque = 10.0f;
jointDef.motorSpeed = 10.0f;
jointDef.enableMotor = true;
joint=(b2DistanceJoint*)_world->CreateJoint(&jointDef);
,但是当头部移动时身体不移动。
我的勾选方法是
- (void)tick:(ccTime) dt {
//we update the position of the b2body based on the sprite position
for (b2Body* body = _world->GetBodyList(); body != nil; body = body->GetNext())
{
if (body->GetUserData() != NULL) {
CCSprite *spritedata = (CCSprite *)body->GetUserData();
if(spritedata.tag==1)
{
b2Vec2 b2Position = b2Vec2(SCREEN_TO_WORLD(spritedata.position.x),
SCREEN_TO_WORLD(spritedata.position.y));
float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(spritedata.rotation);
body->SetTransform(b2Position,b2Angle);
}
else {
spritedata.position = ccp(body->GetPosition().x * PTM_RATIO,
body->GetPosition().y * PTM_RATIO);
spritedata.rotation = -1 * CC_RADIANS_TO_DEGREES(body->GetAngle());
}
}
}
}
为什么不动?我应该如何更改我的代码?
I'm new in box2d and I tried to create joint between two body.
I wrote a joint like
b2RevoluteJointDef jointDef;
jointDef.bodyA=worm_head;
jointDef.bodyB=worm_body;
jointDef.lowerAngle = -0.25f * b2_pi; // -45 degrees
jointDef.upperAngle = 0.25f * b2_pi; // 45 degrees
jointDef.enableLimit=true;
jointDef.maxMotorTorque = 10.0f;
jointDef.motorSpeed = 10.0f;
jointDef.enableMotor = true;
joint=(b2DistanceJoint*)_world->CreateJoint(&jointDef);
but body is not moving when head is moving.
my tick method is
- (void)tick:(ccTime) dt {
//we update the position of the b2body based on the sprite position
for (b2Body* body = _world->GetBodyList(); body != nil; body = body->GetNext())
{
if (body->GetUserData() != NULL) {
CCSprite *spritedata = (CCSprite *)body->GetUserData();
if(spritedata.tag==1)
{
b2Vec2 b2Position = b2Vec2(SCREEN_TO_WORLD(spritedata.position.x),
SCREEN_TO_WORLD(spritedata.position.y));
float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(spritedata.rotation);
body->SetTransform(b2Position,b2Angle);
}
else {
spritedata.position = ccp(body->GetPosition().x * PTM_RATIO,
body->GetPosition().y * PTM_RATIO);
spritedata.rotation = -1 * CC_RADIANS_TO_DEGREES(body->GetAngle());
}
}
}
}
Why is not moving ? How should I change my code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 b2RevoluteJointDef 中,一个主体是静态主体,另一个主体是动态主体。我的问题是使用两个动态问题。现在,它解决了。
In b2RevoluteJointDef , one body is static body and another is dynamic body. My problem is using two dynamic problem. Now, it solved.