IOS--Skphysicsbody of Spritekit frames--Move and connect

Source: Internet
Author: User


Force movement of the Skphysicsbody The Skphysicsbody object differs from the Skspritenode object. The Skspritenode object can be moved by adding an action, but the Skphysicsbody object does not move. So how do we move the Skphysicsbody object? This requires that we use force. Force the physical body to produce motion. Let's take a look at the method of adding forces to a physical body.

Method function
- (void)applyForce:(CGVector)force; Exert a force that uniformly acts on the physical body.
- (void)applyTorque:(CGFloat)torque; Exert a lateral thrust acting on the physical body
- (void)applyImpulse:(CGVector)impulse; Exert a force that uniformly acts on the physical body
- (void)applyAngularImpulse:(CGFloat)impulse; Applying a angular thrust to a physical body

Let's look at a concrete example, as shown below.

First I let the physical world of the whole scene accelerate to 0, which means there is no acceleration of gravity.

-(instancetype)initWithSize:(CGSize)size{    if (self = [super initWithSize:size]) {        //设置场景的物理世界        self.physicsWorld.gravity = CGVectorMake(0.0, 0.0);    }    return self;}

The next step is to set the background and the body of the experiment and the boundaries of the scene. The code looks like this.

 -(void) Didmovetoview: (Skview *) view{[self backgroundnode];    [Self planenode]; Set Boundary self.physicsbody = [Skphysicsbody bodywithedgeloopfromrect:cgrectmake (Self.frame.origin.x, SELF.FRAME.ORIGIN.Y, Self.size.width, Self.size.height)];} #pragma mark----Create background-----(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----Create a spaceship-----(void) planenode{skspritenode *planenode = [Skspritenode spritenodewithimagenamed:@ "aircraft. 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];}  

Below, we create the physical body inside the Touchbegan, and give it a horizontal 1000N pull to the right. The code is as follows

-(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)];}

Next, the young sunflower brand mother started!!! We have a force analysis of the physical body above, and we know that the object is subjected to a horizontal right 1000N pull, since the linear damping is not set, the default is 0.2. So the object is simultaneously subjected to a horizontal left friction, so that the object does a deceleration motion.


Let's take a look at the direct.


A few other stress situations are similar, so there is no explanation here.

Connection to the Skphysicsbody The physical connection, in fact, is to connect two physical bodies in a certain way. There are five types of connections as shown.
Physical body Connection mode
class function
Skphysicsjointfixed A fixed union mixes two bodies together at a reference point. Fixed Union for creating complex shapes that can be beaten later
Skphysicsjointsliding Slide Union allows two-body anchor points to slide along the selected axis
Skphysicsjointspring The spring joint is like a spring, and its length is the initial distance between the two bodies.
Skphysicsjointlimit The boundary union limits the maximum distance between the two bodies, just as if they were connected using a rope.
Skphysicsjointpin Needle union and must be two bodies independently of the anchor point rotation, just like nailed together

Let's do an example with a spring join. First, let's get the work done. Create a scene and create two cubes in the scene, as shown in the code below.

#import "GameScene.h" @implementation gamescene-(Instancetype) Initwithsize: (cgsize) size{if (self = [super Initwithsize:size]) {self.backgroundcolor = [skcolor whitecolor];//self.physicsWorld.gravity = Cgvectormak    E (0, 0); } return self;}    -(void) Didmovetoview: (Skview *) view{[self cubenode]; Set Boundary self.physicsbody = [Skphysicsbody bodywithedgeloopfromrect:cgrectmake (Self.frame.origin.x, SELF.FRAME.ORIGIN.Y, Self.size.width, Self.size.height)];} #pragma mark----Create two cube----(void) cubenode{skspritenode *cubenode = [Skspritenode spritenodewithimagenamed:@ "cube. 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:@ "cube. 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];}

We still operate in the Touchbegan method, in which we create the corresponding physical body with two cube nodes, then create a spring union of two physical bodies and set the properties of the Union mode, and add them to the physical world of the scene. The code is shown below.

-(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];}

Then, the young Sunflower brand mother again, we see, once the Touchbegan method is triggered, then two objects will do free fall. After falling to the boundary of the scene, bounce, and the spring vibrates. Due to the existence of friction, finally, All still. Do not know the children's shoes can go to the high School of physics compulsory one, the chapter of the Force??.


The whole is as follows.


Well, Skphysicsbody's move and connection are here, and hopefully this skphysicsbody's move and connection will help you. Finally the hands are on the Skphysicsbody's mobile and connected demo. Here is the integrated version of Yo ~
-->skphysicsbody's mobile and connected demo??

IOS--Skphysicsbody of Spritekit frames--Move and connect

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.