سؤال

I am creating a game, where there is a ball, and there are some rectangles, as they were tables.

Well, it's working with Box2d, so I can move the ball freely with my SneakyInput joystick, and here is my problem:

I have a "table" and my ball, made with LevelHelper and SpriteHelper. Everything seems to work properly here. My ball is dynamic and my table is kinematic (tried with static too). There is no gravity in my world.

Well, when I'm moving the ball, it collides just for a second with the table, and it can't go through it for that second. After that, it does.

I tried with the boxes that come with the Cocos2D HelloWorld file, and my ball does the same, collide with them for a second or so, and then I can go through them, and it won't collide again.

Do you have any ideas?

Here you have some code:

{
-(id) init
{
    if( (self=[super init])) {


        self.isTouchEnabled = YES;



        // init physics

       b2Vec2 gravity = b2Vec2(0,0);
        world = new b2World(gravity);

        //load from level

       loader = [[LevelHelperLoader alloc] initWithContentOfFile:@"Level1"];

       [loader addObjectsToWorld:world cocos2dLayer:self];

        [self schedule: @selector(tick:) interval:1.0f/60.0f];



        [self draw];


        [self initJoystick];



     //This load some example sprites for collisions

        // Use batch node. Faster
        CCSpriteBatchNode *parent = [CCSpriteBatchNode batchNodeWithFile:@"blocks.png" capacity:100];
        spriteTexture_ = [parent texture];

        // doesn't use batch node. Slower
        spriteTexture_ = [[CCTextureCache sharedTextureCache] addImage:@"blocks.png"];
        CCNode *parent = [CCNode node];

        [self addChild:parent z:0 tag:kTagParentNode];



        [self scheduleUpdate];







    }
    return self;
}


 -(void) tick: (ccTime) dt{
//scaled velocity for joystick
    CGPoint scaledVelocity = ccpMult(joystick.velocity, 120);

    world->Step(dt, 10, 10);

    for (b2Body* b = world->GetBodyList();b; b=b->GetNext()){
        CCSprite *myActor = (CCSprite*)b->GetUserData();

        if (b->GetUserData() != NULL){


            if (b->GetType() == b2_dynamicBody && myActor == [loader spriteWithUniqueName:@"prota"]){

               b->SetTransform(b2Vec2(b->GetPosition().x+scaledVelocity.x*dt/PTM_RATIO,b->GetPosition().y+scaledVelocity.y*dt/PTM_RATIO), 0);

                //move sprite to body position
                myActor.position = ccp(b->GetPosition().x * PTM_RATIO,
                                    b->GetPosition().y * PTM_RATIO);



            }







        }
    }

}

Thanks in advance!

هل كانت مفيدة؟

المحلول

The problem is with setting the tranformation of your body manually as you do. Set only the velocity with :

b->SetVelocity(b2Vec2(scaledVelocity.x*dt/PTM_RATIO, scaledVelocity.y*dt/PTM_RATIO));

The world step method calculates the destination of each body by using physical properties of each body (velocity amongst them) and I believe that setting the position manually interferes with these calculations

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top