Box2d , Cocos2D - 相同的身体,相同的冲动,但不同的效果

发布于 2024-12-23 07:05:10 字数 1294 浏览 0 评论 0原文

在我的游戏中,我每 2 秒创建一个新物体,然后对这个 Box2d 物体施加一个脉冲。

我的问题是冲动仍然是一样的,但身体以两种方式表现:

它慢慢向上,或者另一次它快速下降

我希望身体在冲动后以相同的方式表现,可能吗?

代码:

- (void)newBullet
{
CGPoint touchedAt;
touchedAt.x = 184;
touchedAt.y = 1200;
bullet = [CCSprite spriteWithSpriteSheet:spriteSheet  rect:CGRectMake(586, 719, 32, 32)];
[spriteSheet addChild: bullet z: 10 tag: 8];

bullet.position = ccp( touchedAt.x , touchedAt.y);
bullet.rotation = 90;
bulletBodyDef.type = b2_dynamicBody;
bulletBodyDef.position.Set(touchedAt.x / PTM_RATIO, touchedAt.y / PTM_RATIO);
bulletBodyDef.userData = bullet;
bulletBodyDef.angle = CC_DEGREES_TO_RADIANS( 90 );
bulletBody = _world->CreateBody(&bulletBodyDef);

b2CircleShape bulletShape;
bulletShape.m_radius = bullet.contentSize.width/PTM_RATIO/2;

b2FixtureDef bulletShapeDef;
bulletShapeDef.shape = &bulletShape;
bulletShapeDef.density = 0.0f;
bulletShapeDef.friction = 0.9f;
bulletShapeDef.restitution = 0.0f;
bulletShapeDef.isSensor = false;
bulletFixture = bulletBody->CreateFixture(&bulletShapeDef);

b2Vec2 force = b2Vec2(6.0f, 4.0f );
bulletBody->ApplyImpulse(force, bulletBody->GetPosition());
}

视频(抱歉质量较差,但你可以明白我的意思) http://vimeo.com/34215327

In my game every 2 seconds I create a new body and then I apply an impulse to this Box2d body.

My problem is that impulse is still the same, but the body behaves in two ways:

It slowly goes upwards , or another time it quickly falls down

I want the body to behave in the same way after impulse, is it possible?

Code:

- (void)newBullet
{
CGPoint touchedAt;
touchedAt.x = 184;
touchedAt.y = 1200;
bullet = [CCSprite spriteWithSpriteSheet:spriteSheet  rect:CGRectMake(586, 719, 32, 32)];
[spriteSheet addChild: bullet z: 10 tag: 8];

bullet.position = ccp( touchedAt.x , touchedAt.y);
bullet.rotation = 90;
bulletBodyDef.type = b2_dynamicBody;
bulletBodyDef.position.Set(touchedAt.x / PTM_RATIO, touchedAt.y / PTM_RATIO);
bulletBodyDef.userData = bullet;
bulletBodyDef.angle = CC_DEGREES_TO_RADIANS( 90 );
bulletBody = _world->CreateBody(&bulletBodyDef);

b2CircleShape bulletShape;
bulletShape.m_radius = bullet.contentSize.width/PTM_RATIO/2;

b2FixtureDef bulletShapeDef;
bulletShapeDef.shape = &bulletShape;
bulletShapeDef.density = 0.0f;
bulletShapeDef.friction = 0.9f;
bulletShapeDef.restitution = 0.0f;
bulletShapeDef.isSensor = false;
bulletFixture = bulletBody->CreateFixture(&bulletShapeDef);

b2Vec2 force = b2Vec2(6.0f, 4.0f );
bulletBody->ApplyImpulse(force, bulletBody->GetPosition());
}

Video(Sorry for poor quality, but you can see what I mean)
http://vimeo.com/34215327

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看春风乍起 2024-12-30 07:05:10

我不知道你如何调用这个方法,但我建议你更好地控制调用该方法的位置以及执行 box2d 时间步长的位置,它可能与此无关,但值得检查。可能发生的情况是,脉冲没有被应用,因为直到您重置力(我假设您正在这样做)之前,不会调用决定身体速度的解算器,因此效果消失了。

I don't know how you are calling this method, but I suggest that you have more control over where you call the method and where you do the box2d time step, it might not be related to that, but its worth checking out. What might be happening is that the impulse is not being applied because the solver which determines the speed of the body isn't called until you reset forces (which I assume you are doing), and so the effect is lost.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文