您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x Box2D 简单例子

2013-08-04 18:32 302 查看
原文:http://www.raywenderlich.com/28602/intro-to-box2d-with-cocos2d-2-x-tutorial-bouncing-balls

原文是用Objective-C,我转成C++然后再稍微改一下功能。实现了一个有弹力的物体,随着触摸点的不同而向不同的方向加动势。具体点说,你点它的左下方,就会向右上方加一个动势,导致向右上方运动。

想体验的请点这里下载已打包的APK文件,在手机或者模拟器上运行:http://download.csdn.net/detail/u010639508/5868545

代码和resources依旧全放上了GitHub: https://github.com/serika00/android/tree/master/cocos2d-x/Box2DExample

环境: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 32

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android cocos2d-x Box2D