1
votes

I am creating a game where a ball is suppose to bounce off from platforms. I have set up physics properties for the ball and the platform(platform only attains physics property when it's below the ball). My problem is: the ball is not bouncing (I have applied impulse in didbegincontact method) when the ball makes contact with the platform, it however detects contact.

Here is my didBeginContact Code:

- (void) didBeginContact:(SKPhysicsContact *)contact {

    SKSpriteNode *firstNode, *secondNode;

    firstNode = (SKSpriteNode*) contact.bodyA.node;
    secondNode = (SKSpriteNode*) contact.bodyB.node;

    if ((contact.bodyA.categoryBitMask == ballCategory) && (contact.bodyB.categoryBitMask == solidPlatformCategory)) {

        NSLog(@"Platform Hit");

        CGPoint contactPoint = contact.contactPoint;

        [_ball.physicsBody applyImpulse:CGVectorMake(0, 4) atPoint:contactPoint];

    }

}



///// Here is the code for SKSpriteNode Ball

- (void) addBall {

    _myBall = [SKSpriteNode spriteNodeWithImageNamed:@"ball.png"];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        _myBall.scale = 0.4;
    } else {
        _myBall.scale = 0.3;
    }
    _ball.position = CGPointMake(self.frame.size.width/2, _solidPlatform.position.y + 2.5*_ball.size.height);
    _ball.zPosition = 2;
    _ball.name = @"doodle";
    _ball.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_myDoodle.frame.size];
    _ball.physicsBody.mass = 1.0;
    _ball.physicsBody.restitution = 0.8;
    _ball.physicsBody.dynamic = YES;
    _ball.physicsBody.allowsRotation = NO;
    _ball.physicsBody.usesPreciseCollisionDetection = YES;
    _ball.physicsBody.categoryBitMask = ballCategory;
    _ball.physicsBody.collisionBitMask = solidPlatformCategory;
    _ball.physicsBody.contactTestBitMask = solidPlatformCategory;

    //SKAction *moveUpAction = [SKAction moveByX:0.0 y:8*numberOfPlatforms duration:0.5];


    [self addChild:_ball];
}

////Platform has been defined as (not a complete code):

_solidPlatform7.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_solidPlatform7.frame.size];
        _solidPlatform7.physicsBody.dynamic = NO;
        _solidPlatform7.physicsBody.affectedByGravity = NO;
        _solidPlatform7.physicsBody.usesPreciseCollisionDetection = YES;
        _solidPlatform7.physicsBody.categoryBitMask = solidPlatformCategory;

PS: I am not getting any collusion detection if I define platform as bodyWithEdgeFromRect

1
your code, por favor ... - LearnCocos2D
Can you please help by looking at the code above. - Ashwin

1 Answers

0
votes

difficult to answer without seeing some code, but I'll try:

If the ball does not bounce at all, check the restitution property. Higher values provide a higher "bounciness":

ball.physicsBody.restitution=0.8;

If you want the ball to bounce endless between bottom and ceiling you can invert the gravity after each collision:

self.physicsWorld.gravity = CGVectorMake(0, self.physicsWorld.gravity.dy * (-1));

Hope that helps. If not, please share some code.

I've tried your code. With some smaller changes it works:

- (void) addBall {

    // Bottom platforms
    for (int i=0; i<10; i++) {
        SKSpriteNode *mySprite = [SKSpriteNode  spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(40, 20)];
        CGPoint location = CGPointMake(i*40+60, 10);
        //mySprite.size =CGSizeMake(20, 40);
        mySprite.position=location;
        mySprite.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:mySprite.size];
        mySprite.physicsBody.dynamic=false;
        mySprite.physicsBody.categoryBitMask=solidPlatformCategory;
        [self addChild:mySprite];
    }


    _ball = [SKSpriteNode spriteNodeWithImageNamed:@"ball.png"];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        _ball.scale = 0.4;
    } else {
        _ball.scale = 0.3;
    }

    _ball.position = CGPointMake(self.frame.size.width/2, self.frame.size.width/2);
    _ball.zPosition = 2;
    _ball.name = @"doodle";
    //_ball.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_myDoodle.frame.size];
    _ball.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:_ball.size.width/2];
    _ball.physicsBody.mass = 1.0;
    _ball.physicsBody.restitution = 1;
    _ball.physicsBody.dynamic = YES;
    _ball.physicsBody.allowsRotation = NO;
    _ball.physicsBody.usesPreciseCollisionDetection = YES;
    _ball.physicsBody.categoryBitMask = ballCategory;
    _ball.physicsBody.collisionBitMask = solidPlatformCategory;
    _ball.physicsBody.contactTestBitMask = solidPlatformCategory;




    //SKAction *moveUpAction = [SKAction moveByX:0.0 y:8*numberOfPlatforms duration:0.5];


    [self addChild:_ball];
}