PhysicsEditor with cocos2d and box2d
This is a short tutorial how to use PhysicsEditor together with cocos2d and box2d as physics engine. The project is bases on the box2d template project which comes with cocos2d. The project is complete – it contains cocos2d and everything you need for a quick start.
Structure:
- Classes – example classes to for the demo
- libs – cocos2d, box2d and other sources
- PhysicsEditorSources – the plist loader for box2d
- Resources – the sprites
The main folder contains the shapedefs.pes file which is the file for PhysicsEditor. You can open this to experiment with parameters and add new sprites to the project.
Setting up box2d
First you need to create the b2World object which runs the complete simulation:
b2Vec2 gravity;
gravity.Set(0.0f,-10.0f);
// Do we want to let bodies sleep?
// This will speed up the physics simulation
bool doSleep=true;
// Construct a world object, which will hold
// and simulate the rigid bodies.
world=new b2World(gravity, doSleep);
world->SetContinuousPhysics(true);
In the demo I also add some floor shapes which are created programatically (not shown here).
Preparing the GB2ShapeCache
Next we need to set up the GB2ShapeCache and load the plist file with the shapes created with PhysicsEditor.
addShapesWithFile:@"shapedefs.plist"];
Make sure the plist file is in your resources.
Create a CCSprite and attach it to a b2Body
With that done we can create a CCSprite and add it to the current scene:
[self addChild:sprite];
(I did not use CCBatchNode in this example because I wanted to keep the demo simple and independent from<a href="http://www.texturepacker.com/" "="" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; outline-width: 0px; outline-style: initial; outline-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: rgb(131, 180, 65); text-decoration: none; background-position: initial initial; background-repeat: initial initial; ">TexturePacker.)
Next is to create a b2Body object. We create a b2_dynamicBody which means that the object will be controlled by box2d. We also set the position and which is very important we also set the userData field to the CCSprite we created in the step before:
bodyDef.type=b2_dynamicBody;
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
bodyDef.userData=sprite;
b2Body*body=world->CreateBody(&bodyDef);
With that done we own a b2Body which has no fixtures or shape yet. We now use the GB2ShapeCache to attach the fixtures created with PhysicsEditor. If you simply dragged theobject.pngonto PhysicsEditor it’s name in the plist file will beobject.
Create the body, then add the shape to the body:
addFixturesToBody:body forShapeName:"object"];
The last step is to set the anchor point for the CCSprite. PhysicsEditor allows you to set and edit anchor points. If you don’t do this the b2Body and the sprite displayed might have some offset which results in a strange behavior.
[GB2ShapeCache sharedShapeCache]
anchorPointForShape:"object"]];
Simulating the b2World
Now we have the physics in place but also need to update sprites by setting them to the position of the b2Body object and adjust rotation. We do this in the tick routine which we set up to be called from cocos2d’s scheduler for each frame (during init)
In the tick we need to simulate the world. box2d does several iteration simulating small time steps and moving the bodies for each of them. Adjusting the values might give you better collision detection and physics behavior but might also result in longer calculation times.
After stepping the world we need to adjust each CCSprite according to the b2Body. We do this by iterating over all bodies in the world. Remember that we stored the CCSprite’s pointer in the userdata field of the b2Body. This is how we can adjust them:
{
int32 velocityIterations=8;
int32 positionIterations=1;
world->Step(dt, velocityIterations, positionIterations);
for(b2Body*b=world->GetBodyList(); b; b=b->GetNext())
{
if(b->GetUserData()!=NULL)
{
CCSprite*myActor=(CCSprite*)b->GetUserData();
myActor.position=CGPointMake(
b->GetPosition().x*PTM_RATIO,
b->GetPosition().y*PTM_RATIO);
myActor.rotation=-1*CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}
What next
Start playing with PhysicsEditor. E.g. adjust the parameters like bounce, friction to see the sprites bounce and jump.
The complete demo project including resouces and all you need to get started can be found in the Examples folder in the dmg file.
Download
相关推荐
物理编辑器 cocos2d box2d.很好用哦
This book shows you how to use the powerful new cocos2d, version 2 game engine to develop games for iPhone and iPad with tilemaps, virtual joypads, Game Center, and more. It teaches you: The ...
This book shows you how to use the powerful new cocos2d, version 2 game engine to develop games for iPhone and iPad with tilemaps, virtual joypads, Game Center, and more. It teaches you: The process...
PhysicsEditor 软件附带的 demo 是 xcode 的 project, 移到 vs2010 + cocos2d-x 1.X 的示例在这里: http://download.csdn.net/download/zhangxaochen/4646477 因为 cocos2d-x 2.x 删掉了 CCMutableArray 一些类,...
physicsEditor 编辑 coco2d-x解析 box2d debugdraw 实例工程 zip for mac
Cocos2d物理演示这是CodeAndWeb编写的 Physical 的源代码。... 克隆项目时,使用以下命令行-这将自动加载所有依赖关系: git clone --recursive https://github.com/CodeAndWeb/PhysicsEditor-Cocos2d-ObjC
之前上传失败了么。。不大清楚。再传一遍吧。 win32 下visual studio 2010 的工程。解压到 cocos2d-x 根目录下,应该可以运行。运行效果截图可以看: http://blog.csdn.net/zhangxaochen/article/details/8018489
cocos2d 物理编辑器 Win32版 PhysicsEditor-1.0.9
cocos2d 物理编辑器 Mac版 PhysicsEditor-v1-0-10.dmg
GB2ShapeCache-x PhysicsEditor 加快 cocos2dx-Box2D 开发 cocos2d-x 2.0.4
PhysicsEditor-Loader-for-cocos2d-x-3.0 适用于 cocos2d-x 3.0 的 PhysicsEditor 加载器
支持主流2d刚体编辑器,像box2d,cocos2d,
Cocos2d-x v3.0物理系统 利用PhysicsEditor创建多边形 免费版 Cocos2d-x v3.0物理系统 利用PhysicsEditor创建多边形 免费版
box2dweb-exporter-physicseditor Box2DWeb的自定义导出器,可与CodeAndWeb的PhysicsEditor一起使用导出的脚本是ES6 / 2015模块 默认情况下,该脚本设置为导入ES2015模块box2dweb-haircut: Box2DWeb的CommonJS...
PhysicsEditor是一款可编辑Box2D和Chipmunk collision形状的物理编辑器。您可以使用它来添加图像,跟踪他们的轮廓,以物理参数导出编辑内容,分享之与大家共同体进步。
physicsEditor的接口文件,因cocos2d-x版本变更无法适配,故修改之
Cocos JS使用chipmunk PhysicsEditor 工具类
PhysicsEditor是一款可编辑Box2D和Chipmunk collision形状的物理编辑器。您可以使用它来添加图像,跟踪他们的轮廓,以物理参数导出编辑内容。 PhysicsEditor附带的Helper文件,可以只管理shape和物理属性,不负责...