设为首页 加入收藏

TOP

物理模拟(基于定时器的动画 11.2)(二)
2017-10-11 13:57:33 】 浏览:2585
Tags:物理 模拟 基于 定时器 动画 11.2
ITY 1000 65 66 - (void)viewDidLoad 67 { 68 //invert view coordinate system to match physics 69 self.containerView.layer.geometryFlipped = YES; 70 //set up physics space 71 self.space = cpSpaceNew(); 72 cpSpaceSetGravity(self.space, cpv(0, -GRAVITY)); 73 //add a crate 74 Crate *crate = [[Crate alloc] initWithFrame:CGRectMake(100, 0, 100, 100)]; 75 [self.containerView addSubview:crate]; 76 cpSpaceAddBody(self.space, crate.body); 77 cpSpaceAddShape(self.space, crate.shape); 78 //start the timer 79 self.lastStep = CACurrentMediaTime(); 80 self.timer = [CADisplayLink displayLinkWithTarget:self 81 selector:@selector(step:)]; 82 [self.timer addToRunLoop:[NSRunLoop mainRunLoop] 83 forMode:NSDefaultRunLoopMode]; 84 } 85 86 void updateShape(cpShape *shape, void *unused) 87 { 88 //get the crate object associated with the shape 89 Crate *crate = (__bridge Crate *)shape->data; 90 //update crate view position and angle to match physics shape 91 cpBody *body = shape->body; 92 crate.center = cpBodyGetPos(body); 93 crate.transform = CGAffineTransformMakeRotation(cpBodyGetAngle(body)); 94 } 95 96 - (void)step:(CADisplayLink *)timer 97 { 98 //calculate step duration 99 CFTimeInterval thisStep = CACurrentMediaTime(); 100 CFTimeInterval stepDuration = thisStep - self.lastStep; 101 self.lastStep = thisStep; 102 //update physics 103 cpSpaceStep(self.space, stepDuration); 104 //update all the shapes 105 cpSpaceEachShape(self.space, &updateShape, NULL); 106 } 107 108 @end View Code

图11.1 一个木箱图片,根据模拟的重力掉落

添加用户交互

下一步就是在视图周围添加一道不可见的墙,这样木箱就不会掉落出屏幕之外。或许你会用另一个矩形的cpPolyShape来实现,就和之前创建木箱那样,但是我们需要检测的是木箱何时离开视图,而不是何时碰撞,所以我们需要一个空心而不是固体矩形。

我们可以通过给cpSpace添加四个cpSegmentShape对象(cpSegmentShape代表一条直线,所以四个拼起来就是一个矩形)。然后赋给空间的staticBody属性(一个不被重力影响的结构体)而不是像木箱那样一个新的cpBody实例,因为我们不想让这个边框矩形滑出屏幕或者被一个下落的木箱击中而消失。

同样可以再添加一些木箱来做一些交互。最后再添加一个加速器,这样可以通过倾斜手机来调整重力矢量(为了测试需要在一台真实的设备上运行程序,因为模拟器不支持加速器事件,即使旋转屏幕)。清单11.4展示了更新后的代码,运行结果见图11.2。

由于示例只支持横屏模式,所以交换加速计矢量的x和y值。如果在竖屏下运行程序,请把他们换回来,不然重力方向就错乱了。试一下就知道了,木箱会沿着横向移动。

清单11.4 使用围墙和多个木箱的更新后的代码

 1 - (void)addCrateWithFrame:(CGRect)frame
 2 {
 3     Crate *crate = [[Crate alloc] initWithFrame:frame];
 4     [self.containerView addSubview:crate];
 5     cpSpaceAddBody(self.space, crate.body);
 6     cpSpaceAddShape(self.space, crate.shape);
 7 }
 8 
 9 - (void)addWallShapeWithStart:(cpVect)start end:(cpVect)end
10 {
11     cpShape *wall = cpSegmentShapeNew(self.space->staticBody, start, end, 1);
12     cpShapeSetCollisionType(wall, 2);
13     cpShapeSetFriction(wall, 0.5);
14     cpShapeSetElasticity(wall, 0.8);
15     cpSpaceAddStaticShape(self.space, wall);
16 }
17 
18 - (void)viewDidLoad
19 {
20     //invert view coordinate system to match physics
21     self.containerView.layer.geometryFlipped = YES;
22     //set up physics space
23     self.space = cpSpaceNew();
24     cpSpaceSetGravity(self.space, cpv(0, -GRAVITY));
25     //add wall around edge of view
26     [self addWallShapeWithStart:cpv(0, 0) end:cpv(300, 0)];
27     [self addWallShapeWithStart:cpv(300, 0) end:cpv(300, 300)];
28     [self addWallShapeWithStart:cpv(300, 300) end:cpv(0, 300)];
29     [self addWallShapeWithStart:cpv(0, 300) end:cpv(0, 0)];
30     //add a crates
31     [self addCrateWithFrame:CGRectMake(0, 0, 32, 32)];
32     [self addCrateWithFrame:CGRectMake(32, 0, 32, 32)];
33     [self addCrateWithFrame:CG
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇动画速度(缓冲 10.1) 下一篇安装及使用cocos2d-x

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目