Cocos2d-x box2d simple example (for beginners)

Source: Internet
Author: User

Original article:

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: on your phone or Simulator

Code and resources are still fully put on GitHub:

Environment: cocs2d-x-2.14, vs2010


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());}}}

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: 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.