2.Cocos2dx 3.2中的重力系统Box2D



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

}


运行效果:

时间: 2024-10-05 04:45:37

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

2.Cocos2dx 3.2重力系统Box2D

 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 #if

Android游戏开发:物理游戏之重力系统开发--圆形自由落体Demo

本节为大家提供有关物理游戏的知识,讲解了一个简单的圆形自由落体Demo的编写.. Java代码 package com.himi; import java.util.Random; import java.util.Vector; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import a

cocos2dx 3.2中的物理引擎初探(一)

cocos2dx在设计之初就集成了两套物理引擎,它们是box2d和chipmunk.我目前使用的是最新版的cocos2dx 3.2.引擎中默认使用的是chipmunk,如果想要改使用box2d的话,需要修改对应的android工程或者是ios工程的配置文件. 在2.x版本的cocos中,使用物理引擎的步骤十分繁琐.但在3.x版本中变得非常方便了.我这次的学习目标是制作一个打砖块的小游戏. 首先,现在的Scene类提供了一个静态工厂方法,用以创造一个集成物理引擎的场景. Scene::initWi

Cocos2d-x 3.0 中使用单点触摸

Cocos2d-x 3.0 中使用单点触摸 尊重原创:http://cn.cocos2d-x.org/tutorial/show?id=2712 在游戏中,经常会用到触摸,大部分游戏也是通过触摸控制游戏角色运动的,在Cocos2d-x 3.0中使用了新的触摸机制,Cocos2d-x 3.0中摒弃了Cocos2d-x 2.0中将要触发的事件交给代理(delegate)处理,再通过实现代理里面的onTouchBegan等方法接收事件,最后完成事件的响应,在Cocos2d-x 3.0中只需通过创建一个

c#语言-多线程中的锁系统

介绍 平常在多线程开发中,总避免不了线程同步.这次就对net多线程中的锁系统做个简单描述. 目录 一:lock.Monitor 1:基础. 2: 作用域. 3:字符串锁. 二: mutex 三:Semaphore 四:总结 一:lock.Monitor 1:基础 Lock是Monitor语法糖简化写法.Lock在IL会生成Monitor. //======Example 1===== string obj = "helloworld"; lock (obj) { Console.Wri

Eclipse中添加Android系统jar包

这样做的好处是,可以使用Eclipse开发系统应用了,这样可以调用系统中才使用的API. 1.首先在项目中右击->属性.如图所示依次操作 2.添加User Library 3.第一次要新建User Library名字 注:一定要勾选上System library(addedto the boot class path)否则会出现错误1. 4.添加jar包 2.${ANDROID_SOURCE}/out/target/common/obj/JAVA_LIBRARIES/framework_inte

Cocos2d-x 3.0中实现多点触摸

Cocos2d-x 3.0中实现多点触摸 尊重原创:http://cn.cocos2d-x.org/tutorial/show?id=2713 在上一篇<Cocos2d-x 3.0 中使用单点触摸>中介绍了在Cocos2d-x 3.0中实现单点触摸,但是有些游戏还会用到多点触摸,其中最典型的游戏是节奏大师,在节奏大师中会不断产生运动的音符,玩家需要不停地点击音符以获得高分,而且玩家可以多个手指头一起点,多个手指头一起点就是使用多点触摸实现的. 下面通过一个小的例子介绍如何在Cocos2d-x

java中的io系统详解

java中的io系统详解 分类: JAVA开发应用 笔记(读书.心得)2009-03-04 11:26 46118人阅读 评论(37) 收藏 举报 javaiostreamconstructorstringbyte 相关读书笔记.心得文章列表 Java 流在处理上分为字符流和字节流.字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符.字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组. Java 内用 Unicode 编码存储字符,字符流处理类负责将外部的其他

Android中StatFs获取系统/sdcard存储(剩余空间)大小

package com.orgcent.util; import java.io.File; import android.os.Environment; import android.os.StatFs; public class MemoryStatus { static final int ERROR = -1; /** * 外部存储是否可用 * @return */ static public boolean externalMemoryAvailable() { return andr