亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

PhysicsEditor with cocos2d and box2d usecase

系統 1685 0

轉載自: http://www.physicseditor.de/cocos2d-box2d/

Tutorial: cocos2d + box2d

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:

// Define the gravity vector.
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 -& gt;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.

[ [ GB2ShapeCache sharedShapeCache ]
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:

CCSprite * sprite = [ CCSprite spriteWithFile : @ "object.png" ] ;
[ self addChild : sprite ] ;

(I did not use CCBatchNode in this example because I wanted to keep the demo simple and independent from<a ="" 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:

b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;

bodyDef.position.Set ( p.x / PTM_RATIO, p.y / PTM_RATIO ) ;
bodyDef.userData = sprite;
b2Body * body = world -& gt;CreateBody ( & amp;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 the object.png onto PhysicsEditor it’s name in the plist file will be object.

Create the body, then add the shape to the body:

[ [ GB2ShapeCache sharedShapeCache ]
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.

[ sprite setAnchorPoint : [
[ 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)

[ self schedule : @selector ( tick : ) ] ;

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:

- ( void ) tick : ( ccTime ) dt
{
int32 velocityIterations = 8 ;
int32 positionIterations = 1 ;
world -& gt;Step ( dt, velocityIterations, positionIterations ) ;

for ( b2Body * b = world -& gt;GetBodyList ( ) ; b; b = b -& gt;GetNext ( ) )
{
if ( b -& gt;GetUserData ( ) != NULL )
{
CCSprite * myActor = ( CCSprite * ) b -& gt;GetUserData ( ) ;
myActor.position = CGPointMake (
b -& gt;GetPosition ( ) .x * PTM_RATIO,
b -& gt;GetPosition ( ) .y * PTM_RATIO ) ;
myActor.rotation = - 1 * CC_RADIANS_TO_DEGREES ( b -& gt;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

PhysicsEditor with cocos2d and box2d usecase


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久精品国产99久久99久久久 | 亚洲高清资源在线观看 | 一区二区亚洲精品 | 99er这里只有精品 | 日日夜夜操操 | 奇米影视555 | 国产精品一级 | 欧美一欧美一区二三区性 | 狠狠狠狠狠狠狠狠狠狠 | 人人射人人干 | 精品亚洲性xxx久久久 | 二级毛片全部 | 黑人边吃奶边扎下面激情视频 | 国产日韩欧美精品一区 | 午夜特级毛片 | 四虎www成人影院观看 | 特黄日韩免费一区二区三区 | 亚洲欧美中文字幕高清在线一 | 日本免费中文字幕在线看 | 亚洲福利视频精选在线视频 | 天天爽夜夜爽夜夜爽精品视频 | 欧洲一级 | 四虎成人精品国产一区a | 99热在线免费 | 国产精品爱啪在线线免费观看 | www四虎在线高清 | 色国产在线视频一区 | 国产在线小视频 | 亚欧在线精品免费观看一区 | 国产区精品一区二区不卡中文 | 亚洲艹逼 | 在线播放日本爽快片 | 日本一级大黄毛片免费基地 | 成人香蕉网 | 九九99香蕉在线视频网站 | 韩国日本美国免费毛片 | 亚洲精品国产福利片 | 国产91在线|亚洲 | 日韩在线观看网站 | 美国毛片一级视频在线aa | 午夜色大片在线观看 |