标签:ios esb 分析 mit 最大 get table eid sbo
方法 | 功能 |
---|---|
- (void)applyForce:(CGVector)force; |
施加一个均匀作用于物理体的拉力 |
- (void)applyTorque:(CGFloat)torque; |
施加一个作用于物理体的侧向推力 |
- (void)applyImpulse:(CGVector)impulse; |
施加一个均匀作用于物理体的推力 |
- (void)applyAngularImpulse:(CGFloat)impulse; |
向物理体施加一个角推力 |
首先我让整个场景的物理世界的重力加速为0,也就是说没有重力加速度.
-(instancetype)initWithSize:(CGSize)size{
if (self = [super initWithSize:size]) {
//设置场景的物理世界
self.physicsWorld.gravity = CGVectorMake(0.0, 0.0);
}
return self;
}
接下来就是设置背景和实验体精灵以及场景的边界.代码如下所示.
-(void)didMoveToView:(SKView *)view{
[self backgroundNode];
[self planeNode];
//设置边界
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.size.width, self.size.height)];
}
#pragma mark ----创建背景----
-(void)backgroundNode{
SKSpriteNode *backgroundNode = [SKSpriteNode spriteNodeWithImageNamed:@"bg_02.jpg"];
backgroundNode.position = CGPointZero;
backgroundNode.zPosition = 0;
backgroundNode.anchorPoint = CGPointZero;
backgroundNode.size = self.size;
[self addChild:backgroundNode];
}
#pragma mark ---- 创建飞船 ----
-(void)planeNode{
SKSpriteNode *planeNode = [SKSpriteNode spriteNodeWithImageNamed:@"飞机.png"];
planeNode.position = CGPointMake(self.size.width/2, self.size.height/2);
planeNode.anchorPoint = CGPointMake(0.5, 0.5);
planeNode.zPosition = 1;
planeNode.name = @"plane";
[self addChild:planeNode];
}
下面,我们在touchBegan里面创建物理体,并且给它一个水平向右的1000N拉力.代码如下
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
SKSpriteNode *planeNode = (SKSpriteNode *)[self childNodeWithName:@"plane"];
planeNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:planeNode.size];
planeNode.physicsBody.restitution = 0;
[planeNode.physicsBody applyForce:CGVectorMake(1000, 0)];
}
接下来,小葵花牌妈妈开课了!!!我们就对上面的物理体进行受力分析,我们知道物体受到一个水平向右的1000N拉力,由于线性阻尼没有进行设置,默认为0.2.所以物体同时受到一个水平向左的摩擦力,这样物体就做一个减速运动.
我们看一下直接的效果图.
类 | 功能 |
---|---|
SKPhysicsJointFixed | 固定联合在某个参考点混合两个主体在一起.固定联合用于创建以后可以打散的复杂形状 |
SKPhysicsJointSliding | 滑动联合允许两个主体的锚点沿选定的轴滑动 |
SKPhysicsJointSpring | 弹簧联合就想弹簧那样,它的长度是两个主体之间的初始距离 |
SKPhysicsJointLimit | 界限联合限定了两个主体之间的最大距离,就想它们使用绳子连接那样 |
SKPhysicsJointPin | 针联合与必须两个主体独立地绕锚点旋转,就像钉在一起那样 |
下面我们就以弹簧联合来做一个示例.首先,我们先把准备工作做好.创建场景,在场景中创建两个正方体,代码如下所示.
#import "GameScene.h"
@implementation GameScene
-(instancetype)initWithSize:(CGSize)size{
if (self = [super initWithSize:size]) {
self.backgroundColor = [SKColor whiteColor];
// self.physicsWorld.gravity = CGVectorMake(0, 0);
}
return self;
}
-(void)didMoveToView:(SKView *)view{
[self cubeNode];
//设置边界
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.size.width, self.size.height)];
}
#pragma mark ----创建两个正方体 ---
-(void)cubeNode{
SKSpriteNode *cubeNode = [SKSpriteNode spriteNodeWithImageNamed:@"正方体.png"];
cubeNode.position = CGPointMake(self.size.width/2, self.size.height/2+200);
cubeNode.anchorPoint = CGPointMake(0.5, 0.5);
cubeNode.zPosition = 1;
cubeNode.name = @"cubeNode";
[self addChild:cubeNode];
SKSpriteNode *cubeNode1 = [SKSpriteNode spriteNodeWithImageNamed:@"正方体.png"];
cubeNode1.position = CGPointMake(self.size.width/2, self.size.height/2);
cubeNode1.anchorPoint = CGPointMake(0.5, 0.5);
cubeNode1.zPosition = 1;
cubeNode1.name = @"cubeNode1";
[self addChild:cubeNode1];
}
我们依然是在touchBegan方法中进行操作,在方法中,我们个两个正方体节点创建对应的物理体,然后创建两个物理体的弹簧联合方式以及设置联合方式的属性,并且添加到场景的物理世界中.代码如下所示.
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
SKSpriteNode *cubeNode = (SKSpriteNode *)[self childNodeWithName:@"cubeNode"];
cubeNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:cubeNode.size];
SKSpriteNode *cubeNode1 = (SKSpriteNode *)[self childNodeWithName:@"cubeNode1"];
cubeNode1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:cubeNode1.size];
SKPhysicsJointSpring *springPhysicsJoint = [SKPhysicsJointSpring jointWithBodyA:cubeNode.physicsBody bodyB:cubeNode1.physicsBody anchorA:cubeNode.position anchorB:cubeNode1.position];
//弹簧的阻尼
springPhysicsJoint.damping = 0.2;
//弹簧的振动频率
springPhysicsJoint.frequency = 2.0;
[self.physicsWorld addJoint:springPhysicsJoint];
}
那么,小葵花牌妈妈又开课了,我们看一下下图,一旦touchBegan方法被触发,那么两个物体就会做自由落体运动.掉落到场景的边界之后,反弹,同时弹簧发生振动.由于摩擦力的存在,最后,全部静止.不懂的童鞋可以去看下高中的物理必修一,力那一章??.
整体的效果图如下.
好了,SKPhysicsBody的移动和连接就说到这了,希望这篇SKPhysicsBody的移动和连接能对您有所帮助.最后双手奉上SKPhysicsBody的移动和连接的Demo.这里是整合版吆~
iOS -- SpriteKit框架之SKPhysicsBody的移动和连接
标签:ios esb 分析 mit 最大 get table eid sbo
原文地址:http://www.cnblogs.com/mafeng/p/7567437.html