- 浏览: 414859 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
IThead:
纠结了几天,用了你的方法,现在解决了,谢谢!
Eclipse 写Javascript卡死问题 -
Rubicon__:
你好,我在运用PageWidget这个类时,出现第一页翻到第二 ...
android翻书效果实现原理( 贝塞尔曲线绘制原理/点坐标计算) -
lionios:
如果不显示printDialog,则打印出来的是空白页,请问你 ...
Print打印机例子 -
rayln:
weiqiulai 写道哥们儿,我怎么没有看到监控队列的配置和 ...
JMS监听MQ实例 -
weiqiulai:
哥们儿,我怎么没有看到监控队列的配置和代码?
JMS监听MQ实例
新建一个cocos2d-chipmunk项目
首先AppDelegate.m中加入
初始化!!
然后添加类
TestWorld.h
首先AppDelegate.m中加入
- (void)applicationDidFinishLaunching:(UIApplication *)application{ cpInitChipmunk(); }
初始化!!
然后添加类
TestWorld.h
// // TestWorld.h // TestPiking // // Created by Rayln Guan on 10/3/13. // Copyright 2013 __MyCompanyName__. All rights reserved. // #import <Foundation/Foundation.h> #import "cocos2d.h" #import "chipmunk.h" #import "TestSprite.h" #import "TestSprite2.h" @interface TestWorld : CCLayerColor { cpSpace *space; cpShape *wall[4]; } + (CCScene *)scene; @end
// // TestWorld.m // TestPiking // // Created by Rayln Guan on 10/3/13. // Copyright 2013 __MyCompanyName__. All rights reserved. // #import "TestWorld.h" @implementation TestWorld + (CCScene *)scene{ TestWorld *t = [TestWorld node]; CCScene *s = [CCScene node]; [s addChild: t]; return s; } - (id)init{ if(self = [self initWithColor:ccc4(166, 166, 166, 255)]){ self.isAccelerometerEnabled = YES; self.isTouchEnabled = YES; //init physics [self initPhysics]; [self initSprite]; [self scheduleUpdate]; } return self; } -(void) initSprite{ TestSprite *t1 = [[TestSprite alloc] initWithSpace:space]; [self addChild:t1 z:0 tag:1]; TestSprite2 *t2 = [[TestSprite2 alloc] initWithSpace:space]; [self addChild:t2 z:0 tag:1]; } //该方法是触发chipmunk的重新渲染 -(void) update:(ccTime)time{ // Should use a fixed size step based on the animation interval. int steps = 2; CGFloat dt = [[CCDirector sharedDirector] animationInterval]/(CGFloat)steps; for(int i=0; i<steps; i++){ cpSpaceStep(space, dt); } } -(void) initPhysics{ CGSize s = [[CCDirector sharedDirector] winSize]; //创建一个space space = cpSpaceNew(); //设置一个重力的x,y,即是垂直和横向的重力 space->gravity = ccp(10, 10); //物理空间设定默认的碰撞回调函数,它的设置是针对全局的,也就是物理空间里任何对象的碰撞都会执行设定的回调函数 //cpSpaceSetDefaultCollisionHandler //设定自定义的碰撞回调规则 cpSpaceAddCollisionHandler(space, 1, 2, begin, NULL, NULL, NULL, NULL); //设置四面墙壁 wall[0] = cpSegmentShapeNew( space->staticBody, cpv(0,0), cpv(s.width,0), 0.0f); // top wall[1] = cpSegmentShapeNew( space->staticBody, cpv(0,s.height), cpv(s.width,s.height), 0.0f); // left wall[2] = cpSegmentShapeNew( space->staticBody, cpv(0,0), cpv(0,s.height), 0.0f); // right wall[3] = cpSegmentShapeNew( space->staticBody, cpv(s.width,0), cpv(s.width,s.height), 0.0f); for( int i=0;i<4;i++) { //摩擦力 cpShapeSetElasticity( wall[i], 1.0f ); //反作用力 cpShapeSetFriction( wall[i], 1.0f ); cpSpaceAddStaticShape(space, wall[i] ); } } static int begin(cpArbiter *arb, cpSpace *space, void *unused){ // Get pointers to the two bodies in the collision pair and define local variables for them. // Their order matches the order of the collision types passed // to the collision handler this function was defined for //CP_ARBITER_GET_SHAPES取出是哪两个shape发生了碰撞,a,b是emery还是bullet,与之前设置回调函数时的第2,3参数的顺序有关 CP_ARBITER_GET_SHAPES(arb, a, b); NSLog(@"begin collision p_x:%1.1f p_y:%1.1f",a->body->p.x,a->body->p.y); NSLog(@"%i, %i", a->collision_type, b->collision_type); // additions and removals can't be done in a normal callback. // Schedule a post step callback to do it. // Use the hook as t he key and pass along the arbiter. //我们不能直接在begin函数里面释放刚体或者shape,要等chipmunk做完必要的计算后才能释放。cpSpaceAddPostStepCallback用来安全的完成这个步骤 cpSpaceAddPostStepCallback(space, (cpPostStepFunc)postStepRemove, a, NULL); // The object is dead, don't process the collision further //返回1是物体撞击事件有效 //返回0是物体撞击事件无效 return 1; } //碰撞的处理函数 static void postStepRemove(cpSpace *space, cpShape *shape, void *unused) { TestSprite *sprite = shape->data; assert(sprite.tag == 1); if( sprite ) { //[sprite removeFromParentAndCleanup:YES]; } } @end
// // TestSprite.h // TestPiking // // Created by Rayln Guan on 10/3/13. // Copyright 2013 __MyCompanyName__. All rights reserved. // #import <Foundation/Foundation.h> #import "cocos2d.h" #import "chipmunk.h" @interface TestSprite : CCSprite { cpSpace *space; cpShape *shape; cpBody *body; } - (id)initWithSpace:(cpSpace *)space; - (void)setShape:(cpSpace *)space; @end
// // TestSprite.m // TestPiking // // Created by Rayln Guan on 10/3/13. // Copyright 2013 __MyCompanyName__. All rights reserved. // #import "TestSprite.h" @implementation TestSprite - (id)initWithSpace:(cpSpace *)space_{ if (self = [self initWithFile:@"Icon-72.png"]) { self.position = ccp(100, 100); self.tag = 1; int num = 4; CGPoint vers[4] = { ccp(-36, -36), ccp(-36, 36), ccp(36, 36), ccp(36, -36) }; space = space_; //cpBodyNew创建一个刚体,参数如下:刚性质量,刚性惯性值。 //惯性值的计算使用chipmunk提供的cpMomentForPoly助手函数,参数如下:刚体质量。多边形点数量。多边形点的坐标集。偏移量,会作用到每个点,设为CGPointZero会让事情简单很多。 body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, vers, CGPointZero)); //设置body的位置 body->p = ccp(100, 100); cpSpaceAddBody(space, body); shape = cpPolyShapeNew(body, num, vers, CGPointZero); //自定义一个类型,用于回调函数用,否则不会回调 shape->collision_type = 1; //shape->e = .5f;shape->u = .5f; shape->data = self; cpSpaceAddShape(space, shape); //cpSpaceAddStaticShape(space, shape); //cpSpaceSetGravity( space, cpv(0, -100) ); } return self; } - (void)setShape:(cpSpace *)space_{ //shape = shape_; space = space_; } //重写该方法,返回YES,这样可以触发nodeToParentTransform方法 - (BOOL)dirty{ return YES; } //重烩Sprite,如果不懂什么意思,可以直接用该方法 -(CGAffineTransform) nodeToParentTransform { CGFloat x = body->p.x; CGFloat y = body->p.y; if ( ignoreAnchorPointForPosition_ ) { x += anchorPointInPoints_.x; y += anchorPointInPoints_.y; } // Make matrix CGFloat c = body->rot.x; CGFloat s = body->rot.y; if( ! CGPointEqualToPoint(anchorPointInPoints_, CGPointZero) ){ x += c*-anchorPointInPoints_.x + -s*-anchorPointInPoints_.y; y += s*-anchorPointInPoints_.x + c*-anchorPointInPoints_.y; } // Translate, Rot, anchor Matrix transform_ = CGAffineTransformMake( c, s, -s, c, x, y ); return transform_; } - (void)removeFromParentAndCleanup:(BOOL)cleanup{ // cpSpaceRemoveBody(space, shape->body); // cpBodyFree(shape->body); // // cpSpaceRemoveShape(space, shape); // cpShapeFree(shape); [super removeFromParentAndCleanup:cleanup]; } @end
发表评论
-
【iOS-Cocos2d游戏开发之十四】音频/音效/视频播放(利用Cocos2D-iPhone-Extensions嵌入Cocos2d进行视频播放!)
2013-11-18 11:04 652李华明Himi 原创,转载务必在明显处注明: 转载自【黑米Ga ... -
MAC系统下COCOS2D-X(IPHONE-ANDROID)及IDE的开发环境搭建
2013-11-11 16:30 1204最近正在做移动多平台对应游戏框架的调查,因为之前业余时间一直使 ... -
CCCallFunc
2013-11-08 14:08 655猴子原创,欢迎转载。转载请注明: 转载自Cocos2D开发网– ... -
Sprite Kit学习笔记(一)初探
2013-11-06 21:23 1080转载:http://blog.csdn.net/k ... -
绘制图形
2013-10-15 13:40 706转载:http://book.2cto.com/201304/ ... -
CocosBuilder各元素渲染完成后的回调函数
2013-10-13 18:00 1085在调用ccbi文件后很多时候需要做一次元素渲染完成后的回调函数 ... -
Retina屏幕的支持
2013-10-12 15:54 4431。 由于retina和普通屏像素相差2倍,所以需要一套高清图 ... -
ccTouchesBegan 中如何让获取的坐标是相对整个屏幕的坐标,而不是在当前view的坐标
2013-10-11 13:36 735ccTouchesBegan 中如何让获取的坐标是相对整个屏幕 ... -
关于内存管理的几个基本要点!!(例子)
2013-10-08 15:25 622首先:如果alloc一个对象就必须要释放代码 比如: Te ... -
定位程序出错位置
2013-10-03 15:03 659在.m文件中加入 #ifdef _FOR_DEBUG_ -( ... -
多线程之NSThread、NSOperation、NSInvocationOperation
2013-10-01 21:46 724IOS中支持多线程操作,使用NSThread和NSInvoca ... -
简单的数据存储
2013-09-26 10:09 720// // ViewController.h // d ... -
TabView在底部的使用
2013-09-24 14:41 782通过UITabBarController去实现多页面切换效果 ... -
TabView的使用
2013-09-24 13:57 956多页面的tab使用,就是ActionBar。直接看例子 其实就 ... -
TableView分组Group写法
2013-09-22 17:05 800需要多实现方法 首先需要在xib的视图文件中修改TableVi ... -
TableView的使用
2013-09-22 16:19 673在界面上拖一个TableView控件在可视区域。定义一个Pro ... -
实现alert弹出框
2013-09-22 10:34 727首先需要先实现UIAlertViewDelegate协议,这样 ... -
Copy方法
2013-09-13 22:42 754NSArray NSMutableArray copy NSA ... -
NSObject 和 反射的使用
2013-09-13 15:50 944// // main.m // Refect // ... -
NSNull
2013-09-09 16:30 668NSNull是为了解决字典等各种类不能放null而用的,这个对 ...
相关推荐
通常这种事情都是交给物理引擎来做 但它们使用的过程中有个令人讨厌的地方 参数太多了 为了初始化一个简单的场景要写很多代码 在cocos2d x 3 0版本中 出现了一个新类族 physicals 它将Box2D或者chipmunk做了一层封装...
IOS应用源码之基于cocos2d和chipmunk的DEMOCrayonBallDemo.rar
IOS应用源码——采用了cocos2d和chipmunk的DEMOCrayonBallDemo.zip
ios应用源码之共享一个自己做的,采用了cocos2d和chipmunk的democrayonballdemo
Chipmunk物理引擎,触摸点击屏幕,每次触摸时候,就会在触摸点生成一个新的精灵,精灵的运行自由落体运动。Webstorm+cocos2d-js
Cocos2d-x 3.0 开发(九)使用Physicals代替Box2D和chipmunk的3.0 alpha1新版本 对应于Coco2d-x 3.0 alpha1 使用,更新之前3.0 alpha0 的 文件。相关博客地址:...
How to enhance your games using the Box2D and Chipmunk physics engines and other cocos2d-related tools and libraries. How to add UIKit views to cocos2d and how to add cocos2d to UIKit apps. The ins ...
cocos2d-x默认项目注释chipmunk模板注释 博客主页 http://blog.csdn.net/runaying
想学 cocos2d + chipmunk 的人找到这个资源有福了, 这个是本人在学习中找到的, 非常好用!
使用cocos code ide,基于cocos2d-js中的物理引擎chipmunk制作的一个demo,简单的陈述了从引力场创建,到刚体创建,刚体下落和碰撞的过程。Demo简单,容易上手。
How to enhance your games using the Box2D and Chipmunk physics engines and other cocos2d-related tools and libraries. How to add UIKit views to cocos2d and how to add cocos2d to UIKit apps. The ins ...
Get a gentle introduction to the Cocos2d-JS framework to begin working with sprite manipulations, animations, and other 2d game development topics. This book covers environment setup and getting ...
Learning Cocos2D_A Hands-On Guide to Building iOS Games with Cocos2D, Box2D, and Chipmunk 。。。 iOS开发大神Ray写的书。非常的不错,值得cocos2d新手学习。
Get a gentle introduction to the Cocos2d-JS framework to begin working with sprite manipulations, animations, and other 2d game development topics. This book covers environment setup and getting ...
Title: Learning Cocos2d-JS Game Development Author: Emanuele Feronato Length: 188 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2015-01-12 ISBN-10: 1784390070 ISBN-...
Cocos2d-x2.1版本ChipmunkTest修改 详情请看我的博客http://blog.csdn.net/bill_man,为了配合文章上传
6.4.4 在cocos2d中使用纹理图册 6.4.5 改进ccanimation辅助类别 6.4.6 将所有图像都放入一个纹理图册中 6.5 自己动手 6.6 本章小结 第7章 滚屏射击游戏(上) 7.1 高级视差滚屏 7.1.1 将背景创建为底纹 ...
本资源是英文完整版,请看资源大小14MB,介绍了cocos2d, box2d and chipmunk。对于想要学习ios游戏开发的朋友来说非常有帮助。
A hands-on guide on building ios games with cocos2d box2d and chipmunk.
A Hands-On Guide to Building iOS Games with Cocos2D, Box2D, and Chipmunk. This book is for anyone with basic programming knowledge who wants to create a game for the iPhone or iPad. In this book you ...