(No. 00004) Implementing brick-hitting games for iOS (8): collision between small balls and bricks in the game

Source: Internet
Author: User

(No. 00004) Implementing brick-hitting games for iOS (8): collision between small balls and bricks in the game

 

Now return to GameScene. m, all our collision processing is placed in this class. first, we need to figure out what will conflict with what. for the moment, we will first achieve the collision between the ball and the brick, as well as the collision between the ball and the rebound rod.

Collision between ball and Brick: collision begins

We know that the physical engine does not deal with collision overnight. It is divided into multiple stages. in Chipmunk, the collision is divided into the start part and the subsequent two parts by time. Not all collision processing requires attention to these two methods. Generally, you only need to pay attention to the start collision part. however, in the process of ball and brick collision, we need to pay attention to the subsequent parts, because some Code cannot be placed at the beginning of the collision. next, let's take a look at how the collision callback code is written:

-(BOOL) ccPhysicsCollisionBegin :( algorithm *) pair ball :( CCNode *) ball brick :( CCNode *) brick {[_ userInterface updateHitsLabel: [NSString stringWithFormat: @ Hits: % d, ++ _ hits]; _ score + = (Brick *) brick ). value * _ scoreRatio; if (_ scoreRatio> 1) {[self updateMsgLabel: [NSString stringWithFormat: @ Score X % d, _ scoreRatio];} // if the ball does not collide with the bricks once in a row, the score ratio will be doubled _ scoreRatio * = 2; [_ userInterface updateScoreLabel: [NSString stringWithFormat: @ score: % d, _ score]; [brick removeFromParent]; @ synchronized (self) {[_ level removeFromBricks: brick];} return YES ;}

The code is not long. First, the impact count in the game scenario is updated, and then the score of players is updated based on the score of different bricks. Then, the bricks are deleted from the scenario. note that the removeFromBricks method we previously implemented in the Level class is called, because we must update the array of the Level.

Collision between ball and Brick: collision ends

We hope to increase the reverse elasticity of the ball when it Colliders with bricks, so we need to adjust the torque of the ball, and the torque of the physical object cannot be adjusted in CollisionBegin, otherwise the physical engine will complain; [, we naturally put it in the collision end part:

-(BOOL) ccPhysicsCollisionPreSolve :( CCPhysicsCollisionPair *) pair ball :( CCNode *) ball brick :( CCNode *) brick {CGPoint velocity = ball. physicsBody. velocity; // slightly increase the ball torque. physicsBody applyImpulse: ccpMult (velocity, 1.00005)]; return YES ;}

Note that I have added a very small torque value, because the torque will quickly accumulate after multiple collisions and will soon become very large, after all, you also don't want to see the ball bounce several times before the speed of light ;)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.