Original article: http://www.raywenderlich.com/28602/intro-to-box2d-with-cocos2d-2-x-tutorial-bouncing-balls
The original article uses objective-C. I converted it to C ++ and then changed the function slightly. An elastic object is implemented, and the moving trend is added to different directions with different touch points. Specifically, when you click it in the lower left corner, a momentum is added to the upper right corner, causing motion to the upper right corner.
Click here to download the packaged APK file and run: http://download.csdn.net/detail/u010639508/5868545 on your phone or Simulator
Code and resources are still fully put on GitHub: https://github.com/serika00/android/tree/master/cocos2d-x/Box2DExample
Environment: cocs2d-x-2.14, vs2010
Run
Helloworldscene. h
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include "Box2D/Box2D.h"USING_NS_CC;class HelloWorld : public cocos2d::CCLayer{public: virtual bool init(); static cocos2d::CCScene* scene(); CREATE_FUNC(HelloWorld);void tick(float dt);void kick(float dt);virtual void ccTouchesBegan(CCSet* pTouches, CCEvent* pEvent);private:b2World* _world;b2Body* _body;CCSprite* _ball;};#endif
Helloworldscene. cpp
#include "HelloWorldScene.h"USING_NS_CC;#define PTM_RATIO 32CCScene* HelloWorld::scene(){ CCScene *scene = CCScene::create(); HelloWorld *layer = HelloWorld::create(); scene->addChild(layer); return scene;}bool HelloWorld::init(){setTouchEnabled(true);CCSize winSize = CCDirector::sharedDirector()->getWinSize(); _ball = CCSprite::create("ball2.png");this->addChild(_ball);b2Vec2 gravity = b2Vec2(0.0f, -8.0f);_world = new b2World(gravity);b2BodyDef ballBodyDef;ballBodyDef.type = b2_dynamicBody;ballBodyDef.position.Set(winSize.width/2/PTM_RATIO, winSize.height/2/PTM_RATIO);ballBodyDef.userData = _ball;_body = _world->CreateBody(&ballBodyDef);b2CircleShape circle;circle.m_radius - 26.0/PTM_RATIO;b2FixtureDef ballShapeDef;ballShapeDef.shape = &circle;ballShapeDef.density = 1.0f;ballShapeDef.friction = 0.2f;ballShapeDef.restitution = 0.8f;_body->CreateFixture(&ballShapeDef);b2BodyDef groundBodyDef;groundBodyDef.position.Set(0, 0);b2Body* groundBody = _world->CreateBody(&groundBodyDef);b2EdgeShape groundEdge;b2FixtureDef boxShapeDef;boxShapeDef.shape = &groundEdge;groundEdge.Set(b2Vec2(0,0), b2Vec2(0,winSize.height/PTM_RATIO)); groundBody->CreateFixture(&boxShapeDef); groundEdge.Set(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO)); groundBody->CreateFixture(&boxShapeDef);groundEdge.Set(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0)); groundBody->CreateFixture(&boxShapeDef);groundEdge.Set(b2Vec2(winSize.width/PTM_RATIO, 0), b2Vec2(0,0));groundBody->CreateFixture(&boxShapeDef);schedule(schedule_selector(HelloWorld::tick));//schedule(schedule_selector(HelloWorld::kick), 5.0f);return true;}void HelloWorld::tick(float dt) {_world->Step(dt, 10, 10);for (b2Body* b = _world->GetBodyList(); b; b = b->GetNext()) {if (b->GetUserData() != NULL) {CCSprite* ballData = (CCSprite*)b->GetUserData();ballData->setPosition(ccp(b->GetPosition().x*PTM_RATIO, b->GetPosition().y*PTM_RATIO));ballData->setRotation(-1*CC_RADIANS_TO_DEGREES(b->GetAngle()));}}}void HelloWorld::kick(float dt) {b2Vec2 force = b2Vec2(3, 3);_body->ApplyLinearImpulse(force, _body->GetPosition());}void HelloWorld::ccTouchesBegan(CCSet* pTouches, CCEvent* pEvent) {CCTouch* touch = (CCTouch*)pTouches->anyObject();CCPoint tp = touch->getLocation();CCPoint _ballPoint = _ball->getPosition();if (_ballPoint.x > tp.x) {if (_ballPoint.y > tp.y) {b2Vec2 force = b2Vec2(3, 3);_body->ApplyLinearImpulse(force, _body->GetPosition());} else {b2Vec2 force = b2Vec2(3, -3);_body->ApplyLinearImpulse(force, _body->GetPosition());}} else {if (_ballPoint.y > tp.y) {b2Vec2 force = b2Vec2(-3, 3);_body->ApplyLinearImpulse(force, _body->GetPosition());} else {b2Vec2 force = b2Vec2(-3, -3);_body->ApplyLinearImpulse(force, _body->GetPosition());}}}