仅对世界上的一个精灵施加零重力

发布于 2024-11-17 23:27:06 字数 992 浏览 1 评论 0原文

我在 cocos2d 和 box2d 中有一个世界,它有重力。 现在,为了为每个精灵添加主体,我调用一个函数并向她发送精灵。

sprite1,必须根据重力移动,但 sprite2 必须是静态的,没有重力,直到 sprite1 击中它,然后世界力量才会影响他。

我如何仅将精灵1/身体重力设置为零,直到另一个精灵击中他?

我的问题是所有精灵对身体使用相同的功能:

- (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);

}

我想在开始时仅在精灵1上应用重力,但我确实想为精灵2创建一个身体,因为稍后它将受到世界的影响。

那么,在开始时创建 2 个物体后,如何仅阻止 sprite2 掉落?
多谢。

i have a world in cocos2d with box2d, that has a gravity.
now in order to add a body for each sprite, i am calling a function and send her the sprite.

sprite1, has to move according to gravity, but sprite2 has to be static,without gravity, just until sprite1 hit it,then the world forces should affect him.

how do i set only sprite1/body gravity to be zero 'til the other sprite hit him ?

my problem is that all sprites using the same function for the body:

- (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);

}

i want to apply gravity at the start only on sprite1, BUT i do want to create a body for sprite2 also, because later it will be affected by the world.

so, after create 2 bodies at start, how do i stop only sprite2 from falling ?
thanks a lot.

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

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

发布评论

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

评论(2

み零 2024-11-24 23:27:06

我通常最常使用 SetActive(),但根据您的需求,我认为 SetAwake() 就是您想要的。

b2Body.h

/// You can disable sleeping on this body. If you disable sleeping, the
/// body will be woken.
void SetSleepingAllowed(bool flag);

/// Is this body allowed to sleep
bool IsSleepingAllowed() const;

/// Set the sleep state of the body. A sleeping body has very
/// low CPU cost.
/// @param flag set to true to put body to sleep, false to wake it.
void SetAwake(bool flag);

/// Get the sleeping state of this body.
/// @return true if the body is sleeping.
bool IsAwake() const;

/// Set the active state of the body. An inactive body is not
/// simulated and cannot be collided with or woken up.
/// If you pass a flag of true, all fixtures will be added to the
/// broad-phase.
/// If you pass a flag of false, all fixtures will be removed from
/// the broad-phase and all contacts will be destroyed.
/// Fixtures and joints are otherwise unaffected. You may continue
/// to create/destroy fixtures and joints on inactive bodies.
/// Fixtures on an inactive body are implicitly inactive and will
/// not participate in collisions, ray-casts, or queries.
/// Joints connected to an inactive body are implicitly inactive.
/// An inactive body is still owned by a b2World object and remains
/// in the body list.
void SetActive(bool flag);

/// Get the active state of the body.
bool IsActive() const;

这应该就是您需要的一切。

I generally use SetActive() the most, but for your needs I think SetAwake() is what you want.

b2Body.h

/// You can disable sleeping on this body. If you disable sleeping, the
/// body will be woken.
void SetSleepingAllowed(bool flag);

/// Is this body allowed to sleep
bool IsSleepingAllowed() const;

/// Set the sleep state of the body. A sleeping body has very
/// low CPU cost.
/// @param flag set to true to put body to sleep, false to wake it.
void SetAwake(bool flag);

/// Get the sleeping state of this body.
/// @return true if the body is sleeping.
bool IsAwake() const;

/// Set the active state of the body. An inactive body is not
/// simulated and cannot be collided with or woken up.
/// If you pass a flag of true, all fixtures will be added to the
/// broad-phase.
/// If you pass a flag of false, all fixtures will be removed from
/// the broad-phase and all contacts will be destroyed.
/// Fixtures and joints are otherwise unaffected. You may continue
/// to create/destroy fixtures and joints on inactive bodies.
/// Fixtures on an inactive body are implicitly inactive and will
/// not participate in collisions, ray-casts, or queries.
/// Joints connected to an inactive body are implicitly inactive.
/// An inactive body is still owned by a b2World object and remains
/// in the body list.
void SetActive(bool flag);

/// Get the active state of the body.
bool IsActive() const;

That should be everything you need.

绅刃 2024-11-24 23:27:06

您可以尝试仅将您想要受世界影响的精灵添加到空间中,然后再添加您想要受世界影响的精灵。

因此,在这种情况下,将 Sprite1 添加到您的空间,但不添加 Sprite2,稍后您可以添加 Sprite2,这样它就会受到空间的影响。

You can try adding only the sprite you want to be affected by the world to the space, and later on add the one you want to be affected by the world.

So in this case add Sprite1 to your space but not Sprite2, later on you can add Sprite2 so it is affected by the space.

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