1
添加Box2D相关的库

步骤1:右击项目所在的解决方案à添加—>现有项目àE:\Installed\cocos2d-x-3.2\cocos2d-x-3.2\external\Box2D\proj.win32\Box2D.vcxproj

步骤2:右击项目à生成依赖项à项目依赖项à将关于libBox2D的复选框选中

步骤3:为项目添加libBox2D的库

方法:右击项目à属性à链接器à输入—>附加依赖项à编辑,添加上libBox2d.lib,à确定

案例说明:

1.编写T32.h

#ifndef
__T32_H__

#define
__T32_H__

#include
"cocos2d.h"

USING_NS_CC;

#define
winSize
Director::getInstance()->getWinSize()

#define
CCLog
cocos2d::log

#endif

2.编写TBack.h

#ifndef
__TBack_H__

#define
__TBack_H__

#include
"T32.h"

class
TBack :
public
Layer

{

public:

CREATE_FUNC(TBack);

bool
init();

};

#endif

3编写TBack.cpp

#include
"TBack.h"

bool
TBack::init()

{

Layer::init();

setLocalZOrder(100);

Menu*
menu =
Menu::create();

MenuItemImage*
item =
MenuItemImage::create("CloseNormal.png",
"CloseSelected.png",

[](Ref*){

Director::getInstance()->popScene();

});

menu->addChild(item);

item->setPosition(winSize.width
/ 2 - item->getBoundingBox().size.width
/ 2,

item->getBoundingBox().size.height
/ 2 - winSize.height
/ 2);

addChild(menu);

return
true;

}

4.编写T06Box2D.h

#ifndef
__T06Box2D_H__

#define
__T06Box2D_H__

#include
"T32.h"

#include
"Box2D/Box2D.h"

class
T06Box2D :
public
Layer

{

public:

CREATE_FUNC(T06Box2D);

bool
init();

b2World*
_world;

b2Body*
_bat;

void
update(float);

};

#endif

5编写:T06Box2D.cpp

#include
"T06Box2D.h"

#define
PTM_RATIO 32.0f

bool
T06Box2D::init()

{

Layer::init();

//创建世界,后面的-9.8表示向下的重力加速度为9.8

//b2Vec2 gravity(0,-9.8f);

//这个表示没有重力加速度

b2Vec2
gravity(0,0.0f);

_world =
new
b2World(gravity);

{

b2BodyDef
def;

//这里是一个动态的body,默认是静态的body

def.type
= b2_dynamicBody;

//设置位置,要转换成重力场中的位置要除以PTM_RATIO

def.position.Set(winSize.width
/ 2 / PTM_RATIO,
winSize.height
/ 2 / PTM_RATIO);

b2Body*
body =
_world->CreateBody(&def);

//让body受力

body->SetLinearVelocity(b2Vec2(10,20));

//显示body的精灵

Sprite*
sprite =
Sprite::create("CloseNormal.png");

addChild(sprite);

sprite->setPosition(body->GetPosition().x*PTM_RATIO,
body->GetPosition().y*PTM_RATIO);

//设置body的形状,让它和sprite相一致,是圆形的

b2CircleShape
shape;

//设置半径

shape.m_radius
= sprite->getContentSize().width
/ 2 / PTM_RATIO;

//后面的一个参数表示的是密度系数

b2Fixture*
fixture =
body->CreateFixture(&shape,
1.0f);

//设置摩擦系统

fixture->SetFriction(0.0f);

//弹性系数

fixture->SetRestitution(1.0f);

//关联body和精灵

body->SetUserData(sprite);

}

//加个地板

{

b2BodyDef
def;

// def.position.Set(0, 0);

b2Body*
body =
_world->CreateBody(&def);

//设置边界类型的形状

b2EdgeShape
shape;

//设置地板的开始点和结束点

shape.Set(b2Vec2(0,
0), b2Vec2(winSize.width
/ PTM_RATIO, 0));

b2Fixture*
fixture =
body->CreateFixture(&shape,
1.0f);

//设置摩擦系数

fixture->SetFriction(0.0f);

//设置弹性系数

fixture->SetRestitution(1.0f);

}

//加个天花板

{

b2BodyDef
def;

def.position.Set(0,
winSize.height
/ PTM_RATIO);

b2Body*
body =
_world->CreateBody(&def);

b2EdgeShape
shape;

shape.Set(b2Vec2(0,
0), b2Vec2(winSize.width
/ PTM_RATIO, 0));

b2Fixture*
fixture =
body->CreateFixture(&shape,
1.0f);

//摩擦系统

fixture->SetFriction(0.0f);

//弹性系数

fixture->SetRestitution(1.0f);

}

//左挡板

{

b2BodyDef
def;

//def.position.Set(0, winSize.height / PTM_RATIO);

b2Body*
body =
_world->CreateBody(&def);

b2EdgeShape
shape;

shape.Set(b2Vec2(0,
0), b2Vec2(0,
winSize.height
/ PTM_RATIO));

b2Fixture*
fixture =
body->CreateFixture(&shape,
1.0f);

fixture->SetFriction(0.0f);
//摩擦系统

fixture->SetRestitution(1.0f);
//弹性系数

}

//右挡板

{

b2BodyDef
def;

def.position.Set(winSize.width
/ PTM_RATIO, 0);

b2Body*
body =
_world->CreateBody(&def);

b2EdgeShape
shape;

shape.Set(b2Vec2(0,
0), b2Vec2(0,
winSize.height
/ PTM_RATIO));

b2Fixture*
fixture =
body->CreateFixture(&shape,
1.0f);

//摩擦系数

fixture->SetFriction(0.0f);

//弹性系数

fixture->SetRestitution(1.0f);

}

//球拍

{

b2BodyDef
def;

def.position.Set(winSize.width
/ 2 / PTM_RATIO,
winSize.height
/ 4 / PTM_RATIO);

b2Body*
body =
_world->CreateBody(&def);

_bat =
body;

Sprite*
sprite =
Sprite::create("bat.png");

body->SetUserData(sprite);

addChild(sprite);

sprite->setPosition(body->GetPosition().x*PTM_RATIO,
body->GetPosition().y*PTM_RATIO);

Size
batSize =
Size(100,30);

Size
content =
sprite->getContentSize();

sprite->setScale(batSize.width
/ content.width,
batSize.height
/ content.height);

b2PolygonShape
shape;

shape.SetAsBox(batSize.width
/ 2 / PTM_RATIO,
batSize.height
/ 2 / PTM_RATIO);

b2Fixture*
fixture =
body->CreateFixture(&shape,
1.0f);

//摩擦系统

fixture->SetFriction(0.0f);

//弹性系统

fixture->SetRestitution(1.0f);

//touch

EventListenerTouchOneByOne*
ev =
EventListenerTouchOneByOne::create();

ev->onTouchBegan
= [](Touch*,
Event*){return
true; };

ev->onTouchMoved
= [&](Touch*
touch,
Event*){

float
dx =
touch->getDelta().x
/ PTM_RATIO;

b2Vec2
pos =
_bat->GetPosition();

pos.x
+= dx;

//下面的函数等价于setPosition()

_bat->SetTransform(pos,
0);

};

_eventDispatcher->addEventListenerWithSceneGraphPriority(ev,
this);

}

scheduleUpdate();

return
true;

}

void
T06Box2D::update(float
dt)

{

//时间在流逝

_world->Step(dt,
8, 1);

//遍历这个世界的body

b2Body*
body =
_world->GetBodyList();

while (body)

{

//设置body相关的精灵的位置

Sprite*
sprite = (Sprite*)body->GetUserData();

if (sprite)

{

sprite->setPosition(body->GetPosition().x*PTM_RATIO,
body->GetPosition().y*PTM_RATIO);

sprite->setRotation(body->GetAngle()*180.0
/ M_PI);

}

body =
body->GetNext();

}

}

6.编写TMenu.h

#ifndef
__TMenu_H__

#define
__TMenu_H__

#include
"T32.h"

class
TMenu :
public
Layer

{

public:

CREATE_FUNC(TMenu);

bool
init();

bool
TouchBegan(Touch*,
Event*);

};

#endif

7.
编写:TMenu.cpp

#include
"TMenu.h"

#include
"TBack.h"

#include
"T01CPP11.h"

#include
"T02Vector.h"

#include
"T03Map.h"

#include
"T04Label.h"

#include
"T06Box2D.h"

static
const
char*
title[] = {

"T01CPP11",

"T02Vector",

"T03Map",

"T04Label",

"T06Box2D"

};

bool
TMenu::init()

{

Layer::init();

Menu*
menu =
Menu::create();

addChild(menu);

for (int
i = 0;
i <
sizeof(title)
/ sizeof(*title);
++i)

{

MenuItemFont*
item =
MenuItemFont::create(title[i],
[](Ref*
sender){

MenuItem*
item = (MenuItem*)sender;

int
i =
item->getTag()-1000;

Layer*
l =
NULL;

if (title[i]
== "T01CPP11") 
l =
T01CPP11::create();

if (title[i]
== "T02Vector")
l =
T02Vector::create();

if (title[i]
== "T03Map")
l =
T03Map::create();

if (title[i]
== "T04Label")
l =
T04Label::create();

if (title[i]
== "T06Box2D")
l =
T06Box2D::create();

if (l)

{

TBack*
b =
TBack::create();

Scene*
s =
Scene::create();

s->addChild(b);

s->addChild(l);

Director::getInstance()->pushScene(s);

}

});

menu->addChild(item);

item->setTag(1000
+ i);

}

menu->alignItemsVertically();

//
触摸

auto
ev =
EventListenerTouchOneByOne::create();

#if 0

ev->onTouchBegan
= [](Touch*,
Event*){

return
true;

};

#endif

//ev->onTouchBegan = std::bind(&TMenu::TouchBegan, this, std::placeholders::_1, std::placeholders::_2);

ev->onTouchBegan
= CC_CALLBACK_2(TMenu::TouchBegan,
this);

ev->onTouchMoved
= [&](Touch*
touch,
Event*){

setPositionY(getPositionY()
+ touch->getDelta().y);

};

_eventDispatcher->addEventListenerWithSceneGraphPriority(ev,
this);

return
true;

}

bool
TMenu::TouchBegan(/*TMEnu*
this, */Touch*,
Event*)

{

return
true;

}

8.编写AppDelegate.cpp

#include
"AppDelegate.h"

#include
"TMenu.h"

#include
"TBack.h"

USING_NS_CC;

AppDelegate::AppDelegate()
{

}

AppDelegate::~AppDelegate()

{

}

bool
AppDelegate::applicationDidFinishLaunching()
{

// initialize director

auto
director =
Director::getInstance();

auto
glview =
director->getOpenGLView();

if(!glview)
{

glview =
GLView::create("My
Game");

glview->setFrameSize(480,
320);

director->setOpenGLView(glview);

}

glview->setDesignResolutionSize(480,
320, ResolutionPolicy::EXACT_FIT);

// turn on display FPS

director->setDisplayStats(true);

// set FPS. the default value is 1.0/60 if you don't call this

director->setAnimationInterval(1.0
/ 60);

// create a scene. it's an autorelease object

auto
scene =
Scene::create();

scene->addChild(TMenu::create());

scene->addChild(TBack::create());

// run

director->runWithScene(scene);

return
true;

}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too

void
AppDelegate::applicationDidEnterBackground()
{

Director::getInstance()->stopAnimation();

// if you use SimpleAudioEngine, it must be pause

// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();

}

// this function will be called when the app is active again

void
AppDelegate::applicationWillEnterForeground()
{

Director::getInstance()->startAnimation();

// if you use SimpleAudioEngine, it must resume here

// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();

}

运行效果:

2.Cocos2dx 3.2中的重力系统Box2D的更多相关文章

  1. 2.Cocos2dx 3.2重力系统Box2D

     1 加入Box2D相关的库 步骤1:右击项目所在的解决方式à加入->现有项目àE:\Installed\cocos2d-x-3.2\cocos2d-x-3.2\external\Box2D ...

  2. JAVA中获取当前系统时间及格式转换

    JAVA中获取当前系统时间   一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; publi ...

  3. java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET

    java中的io系统详解 - ilibaba的专栏 - 博客频道 - CSDN.NET 亲,“社区之星”已经一周岁了!      社区福利快来领取免费参加MDCC大会机会哦    Tag功能介绍—我们 ...

  4. 【腾讯GAD暑期训练营游戏程序开发】游戏中的动画系统作业

    游戏中的动画系统作业说明文档   一.实现一个动画状态机:至少包含3组大的状态节点

  5. cocos2dx 3.7中 AppDelegate.h的class TestController;这种写法的具体意思不太明白,只能猜是类似于外部定义的东西。

    cocos2dx 3.7中 AppDelegate.h的class TestController;这种写法的具体意思不太明白,只能猜是类似于外部定义的东西.

  6. 解决cocos2dx在Xcode中运行时报:convert: iCCP: known incorrect sRGB profile 的问题

    解决cocos2dx在Xcode中运行时报:convert: iCCP: known incorrect sRGB profile 的问题 本文的实践来源是参照了两个帖子完成的: http://dis ...

  7. cocos2d-x 3.0rc2中读取sqlite文件

    cocos2d-x 3.0rc2中读取sqlite文件的方式,在Android中直接读取软件内的会失败.须要复制到可写的路径下 sqlite3* dbFile = NULL; std::string ...

  8. 配置SecureCRT连接本地虚拟机中的Linux系统

    转自:http://www.pythoner.com/196.html 由于平时公司开发时都是使用SecureCRT连接的Linux服务器,所以也想使用SecureCRT在自己电脑上连接本地虚拟机中的 ...

  9. Linux内核中的GPIO系统之(3):pin controller driver代码分析

    一.前言 对于一个嵌入式软件工程师,我们的软件模块经常和硬件打交道,pin control subsystem也不例外,被它驱动的硬件叫做pin controller(一般ARM soc的datash ...

随机推荐

  1. 【python进阶】详解元类及其应用2

    前言 在上一篇文章[python进阶]详解元类及其应用1中,我们提到了关于元类的一些前置知识,介绍了类对象,动态创建类,使用type创建类,这一节我们将继续接着上文来讲~~~ 5.使⽤type创建带有 ...

  2. [自用]多项式类数学相关(定理&证明&板子)

    写在前面 由于上一篇总结的版面限制,特开此文来记录 \(OI\) 中多项式类数学相关的问题. 该文启发于Miskcoo的博客,甚至一些地方直接引用,在此特别说明:若文章中出现错误,烦请告知. 感谢你的 ...

  3. [HNOI 2003]消防局的设立

    Description 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来 连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成 ...

  4. ●POJ 3237 Tree

    题链: http://poj.org/problem?id=3237 题解: LCT 说一说如何完成询问操作就好了(把一条链的边权变成相反数的操作可以类比着来): 首先明确一下,我们把边权下放到点上. ...

  5. APIO 2015

    老师让我们打这套题练练手.感觉这套题还是挺有意思的,比国内某些比赛不知道高到哪里去.最后我拿了284/300,貌似比赛是IOI赛制啊,强行被当成OI赛制做了,不然我T3可能还能多骗点. T1.scul ...

  6. 【UVA–11997 K Smallest Sums 】

    ·哦,这题要用优先队列?那大米饼就扔一个手写堆上去吧! ·英文题,述大意:       输入n个长度为n的序列(题中是k,2<=k<=750).一种结果定义为:从每个序列中都要挑选一个数加 ...

  7. String 类

    一.String类String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.java把String类声明的final类,不能有类.String类对象创建 ...

  8. Lintcode394 Coins in a Line solution 题解

    [题目描述] There are n coins in a line. Two players take turns to take one or two coins from right side ...

  9. discuz全新安装升级,导入旧数据过程,顺便gbk转utf8

    由于discuz官方已经不更新了,现在又只有现成的utf8版本,没有gbk版本.我们原来使用的是gbk编码的,最近想改版,顺便升级一下,就索性把gbk也换成utf8吧,这样以后也方便,国际化嘛! 第一 ...

  10. jenkins部署.net平台自动化构建

    在引入自动化部署工具的时候,我们对比了jenkins和gitlab CI,jenkins有非常丰富的插件,配置起来方便.gitlab CI更倾向于脚本配置,当然jenkins也可以使用pipeline ...