Box2d物理引擎实战开发(共6部分)
来源:互联网 发布:icmp.dll被java嗲用 编辑:程序博客网 时间:2024/05/01 01:48
第四阶段:功能扩展1
1.Box2d物理引擎:Box2d介绍
官网http://box2d.org/
定义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:
virtualvoidupdate(floatdt);
HelloWorldScene.cpp中
HelloWorld::init()中:
world = new b2World(b2Vec2(0,-10));
// float32 timeStep模拟物理世界一步一步之间的时间差 这里用每帧之间的时间差dt
// int32 velocityIterations数据迭代(重新计算的次数来避免误差) 次数越多越精确但也耗费资源 官方定为8
// int32 positionIterations 位置迭代官方建议为3
voidHelloWorld::update(floatdt){
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 RATIO80.0f
classHelloWorld : public cocos2d::Layer,publicb2ContactListener
{
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(floatdt);
virtual void BeginContact(b2Contact* contact);
void addRect(floatx,floaty,b2BodyType type);
void addGround();
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
#include <Box2D/Box2D.h>
#define RATIO80.0f
classHelloWorld : public cocos2d::Layer,publicb2ContactListener
{
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(floatdt);
virtual void BeginContact(b2Contact* contact);
void addRect(floatx,floaty,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
boolHelloWorld::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;
}
voidHelloWorld::update(floatdt){
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);
}
}
}
voidHelloWorld::BeginContact(b2Contact *contact){
if (contact->GetFixtureA()->GetBody()==groundBody||contact->GetFixtureB()->GetBody()==groundBody) {
log("有物体落在了地板上");
}
}
voidHelloWorld::addRect(floatpositionX,floatpositionY,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);
}
voidHelloWorld::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
boolHelloWorld::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;
}
voidHelloWorld::update(floatdt){
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);
}
}
}
voidHelloWorld::BeginContact(b2Contact *contact){
if (contact->GetFixtureA()->GetBody()==groundBody||contact->GetFixtureB()->GetBody()==groundBody) {
log("有物体落在了地板上");
}
}
voidHelloWorld::addRect(floatpositionX,floatpositionY,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);
}
voidHelloWorld::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);
}
0 0
- Box2d物理引擎实战开发(共6部分)
- Box2D物理引擎(1)
- Cocos2D 开发之 学习Box2d物理引擎(1)
- Box2D引擎实现割绳子物理部分的方法
- cocos2dx读书笔记(Box2D 物理引擎)
- cocos2dx读书笔记(Box2D 物理引擎)
- box2d物理引擎学习
- Box2D物理引擎介绍
- Box2d,物理引擎
- box2D 物理引擎例子
- Box2d 物理引擎
- 物理引擎Box2D 入门
- Box2d,物理引擎
- Box2D物理引擎
- cocos Box2D物理引擎
- Box2D物理引擎入门
- box2d 在lua中的运用(Box2D物理引擎)
- Cocos2d-X游戏开发之物理引擎Box2D基础篇(十五)
- 汇编求和实验
- iphone 自学开发(二)图片轮播器的实现
- 盒子与球
- C++如何调用DLL呢,有两种,一种是静态,另外一种是动态,即通过调用windowsAPI 来加载和卸载DLL,具体思路:
- Linux删除文件后空间没有释放
- Box2d物理引擎实战开发(共6部分)
- 【学习笔记】Shell基础学习
- 外网不能访问svn服务器问题解决
- cocoStudio: 层容器锚点设置问题
- 关于网络安全与实名上网的通知
- PHP输出XML
- 解决Android启动显示空白界面的问题,自定义进入软件前的背景图片。
- PHP在页面输出XML的方法.避免乱码.
- 取数字问题