SpriteKit - creating a gravity-resistant sprite (like a balloon)

Does anyone know how I can make SKSpriteNodeignore gravity? I thought about gravity inversion by default, but I understand that I also need things to fall!

It seems like it should be easy, but after reading the documentation, I can't see how I will do it!

thank

+4
source share
4 answers

Update: In iOS 8 / OS X Yosemite (10.10), physical fields provide an elegant solution to these problems. Here you can quickly use them to add buoyancy (for a specific set of nodes) to your scene.

, . strength , ( , ) , ( ).

, , region . ( , - , .)

, @TheisEgeberg answer, falloff.

iOS 7/OS X Mavericks (10.9), , , , .


, , , , , , ( update: ).

: - , , . , applyForce:, :

  • {-1,-1,-1},
  • mass , , (F = ma, a).
  • 150 - , SKPhysicsWorld.gravity , applyForce:. ( SKPhysicsWorld SKFieldNode , .)

affectedByGravity , . , , - . , , .

+9

-, SKSpriteNode . SKPhysicsBody, node, .

-...

myNode.physicsBody.afectedByGravity = NO;

: D

, , ...

SKAction *moveAction = [SKAction moveByX:0 y:-10 duration:1];

SKAction *repeatingAction = [SKAction repeatActionForever:moveAction];

[myNode runAction:repeatingAction];
+2

.

, , , .

, , : . - , , , , , , . , , .

So what you need to do to get the buttress to adapt to the height of the ball, the higher it gets the less pressure that you apply up. This is a way to simulate buoyancy.

+1
source

Here is my answer:

- (MyClass *)newRisingObject
{
    MyClass *risingObject = [MyClass spriteNodeWithImageNamed:@"image"];

    [risingObject setPosition:CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame))];
    [risingObject setName:@"name"];
    [risingObject setUserInteractionEnabled:YES];

    //  Physics
    //
    [risingObject setPhysicsBody:[SKPhysicsBody bodyWithCircleOfRadius:risingObject.size.width / 2.0f]];
    [risingObject.physicsBody setRestitution:1.0f];
    [risingObject.physicsBody setFriction:0.0f];
    [risingObject.physicsBody setLinearDamping:0.0f];
    [risingObject.physicsBody setAngularDamping:0.0f];
    [risingObject.physicsBody setMass:1.3e-6f];

    return risingObject;
}

.

- (void)didMoveToView:(SKView *)view
{
    /* Setup your scene here */
    MyClass *risingObject = [self newRisingObject];

    [self addChild:risingObject];
}

.

- (void)update:(CFTimeInterval)currentTime
{
    /* Called before each frame is rendered */
    [self enumerateChildNodesWithName:@"name"
                           usingBlock:^(SKNode *node, BOOL *stop)
     {
         //  Push object up
         //
         [node.physicsBody applyImpulse:CGVectorMake(0.0f, node.physicsBody.mass * OBJECT_ACCELERATION)];
    }];
}
+1
source

Source: https://habr.com/ru/post/1543429/


All Articles