开发者

applying gravity of zero to only one sprite in the world

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 s开发者_开发知识库tatic,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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜