第四阶段:功能扩展1
1.Box2d物理引擎:Box2d介绍
定义NDK_ROOT 终端打开proj.android 然后 sudo nano /etc/profile回车输入密码 export NDK_ROOT=
eclipse-sdks- android -android-ndk-r9c- ndk-build右键的路径 之后 退出-保存 重启终端
2.Box2d物理引擎:创建世界
HelloWorldScene.h
中
#include
<Box2D/Box2D.h>
private
:
b2World *world;
public
:
virtual
void
update(
float
dt);
HelloWorldScene.cpp
中
HelloWorld::init()中:
world =
new
b2World(b2Vec2(
0
,-
10
));
// float32 timeStep模拟物理世界一步一步之间的时间差 这里用每帧之间的时间差dt
// int32 velocityIterations数据迭代(重新计算的次数来避免误差) 次数越多越精确但也耗费资源 官方定为8
// int32 positionIterations 位置迭代官方建议为3
void
HelloWorld::update(
float
dt){
world->Step(dt, 8, 3);
}
3.Box2d物理引擎:创建运动的物体
glview->setDesignResolutionSize(
800
,
600
, ResolutionPolicy::SHOW_ALL);
b2_staticBody = 0静态的物体
b2_kinematicBody漂浮的物体
b2_dynamicBody 动态的物体
4.Box2d物理引擎:创建静止的物体
5.Box2d物理引擎:创建漂浮的物体
6.Box2d物理引擎:物体碰撞检测
#include
"cocos2d.h"
#include <Box2D/Box2D.h>
#define RATIO 80.0f
class HelloWorld : public cocos2d::Layer, public b2ContactListener
{
private :
b2World *world;
b2Body *groundBody;
public :
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
virtual void update( float dt);
virtual void BeginContact(b2Contact* contact);
void addRect( float x, float y,b2BodyType type);
void addGround();
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
#include <Box2D/Box2D.h>
#define RATIO 80.0f
class HelloWorld : public cocos2d::Layer, public b2ContactListener
{
private :
b2World *world;
b2Body *groundBody;
public :
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
virtual void update( float dt);
virtual void BeginContact(b2Contact* contact);
void addRect( float x, float y,b2BodyType type);
void addGround();
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
};
#include
"HelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1. super init first
if ( !Layer::init() )
{
return false ;
}
world = new b2World(b2Vec2( 0 ,- 10 ));
world->SetContactListener( this );
addGround();
addRect( 5 , 3 , b2_dynamicBody);
// addRect(1,5,b2_kinematicBody);
scheduleUpdate();
return true ;
}
void HelloWorld::update( float dt){
world->Step(dt, 8 , 3 );
Sprite *s;
for (b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData()) {
s = (Sprite*)b->GetUserData();
s->setPosition(b->GetPosition().x*RATIO, b->GetPosition().y*RATIO);
}
}
}
void HelloWorld::BeginContact(b2Contact *contact){
if (contact->GetFixtureA()->GetBody()==groundBody||contact->GetFixtureB()->GetBody()==groundBody) {
log( " 有物体落在了地板上 " );
}
}
void HelloWorld::addRect( float positionX, float positionY,b2BodyType type){
//config box2d
b2BodyDef def;
def.position = b2Vec2(positionX, positionY);
def.linearVelocity = b2Vec2( 0 , 10 );
def.type = type;
b2Body* body = world->CreateBody(&def);
b2PolygonShape shape;
shape.SetAsBox( 0.5 , 0.5 );
b2FixtureDef fixtureDef;
fixtureDef.density = 1 ;
fixtureDef.friction = 0.3 ;
fixtureDef.shape = &shape;
body->CreateFixture(&fixtureDef);
//config cocos shape
auto s = Sprite::create();
s->setTextureRect(Rect( 0 , 0 , 0.5 * 2 *RATIO, 0.5 * 2 *RATIO));
addChild(s);
body->SetUserData(s);
}
void HelloWorld::addGround(){
b2BodyDef def;
def.position = b2Vec2( 400 /RATIO, 0 );
def.type = b2_staticBody;
groundBody = world->CreateBody(&def);
b2PolygonShape groundShape;
groundShape.SetAsBox( 400 /RATIO, 0.5 );
b2FixtureDef fixureDef;
fixureDef.density = 1 ;
fixureDef.friction = 0.3 ;
fixureDef.shape = &groundShape;
groundBody->CreateFixture(&fixureDef);
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//
// 1. super init first
if ( !Layer::init() )
{
return false ;
}
world = new b2World(b2Vec2( 0 ,- 10 ));
world->SetContactListener( this );
addGround();
addRect( 5 , 3 , b2_dynamicBody);
// addRect(1,5,b2_kinematicBody);
scheduleUpdate();
return true ;
}
void HelloWorld::update( float dt){
world->Step(dt, 8 , 3 );
Sprite *s;
for (b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData()) {
s = (Sprite*)b->GetUserData();
s->setPosition(b->GetPosition().x*RATIO, b->GetPosition().y*RATIO);
}
}
}
void HelloWorld::BeginContact(b2Contact *contact){
if (contact->GetFixtureA()->GetBody()==groundBody||contact->GetFixtureB()->GetBody()==groundBody) {
log( " 有物体落在了地板上 " );
}
}
void HelloWorld::addRect( float positionX, float positionY,b2BodyType type){
//config box2d
b2BodyDef def;
def.position = b2Vec2(positionX, positionY);
def.linearVelocity = b2Vec2( 0 , 10 );
def.type = type;
b2Body* body = world->CreateBody(&def);
b2PolygonShape shape;
shape.SetAsBox( 0.5 , 0.5 );
b2FixtureDef fixtureDef;
fixtureDef.density = 1 ;
fixtureDef.friction = 0.3 ;
fixtureDef.shape = &shape;
body->CreateFixture(&fixtureDef);
//config cocos shape
auto s = Sprite::create();
s->setTextureRect(Rect( 0 , 0 , 0.5 * 2 *RATIO, 0.5 * 2 *RATIO));
addChild(s);
body->SetUserData(s);
}
void HelloWorld::addGround(){
b2BodyDef def;
def.position = b2Vec2( 400 /RATIO, 0 );
def.type = b2_staticBody;
groundBody = world->CreateBody(&def);
b2PolygonShape groundShape;
groundShape.SetAsBox( 400 /RATIO, 0.5 );
b2FixtureDef fixureDef;
fixureDef.density = 1 ;
fixureDef.friction = 0.3 ;
fixureDef.shape = &groundShape;
groundBody->CreateFixture(&fixureDef);
}