IOS SpriteKit - Creating physics for bobblehead?

I am trying to create a realistic bobblehead application and want to use physics to animate bobblehead. I saw other applications out there that do this, and from what I'm going to, I need to use SpriteKit.

I created SKScene and used SKPhysicsJointPin to attach the head to the body, but it does not move at all. Any ideas or suggestions?

UPDATED CODE (5/28):

Now using 2 spring joints on the head, and it moves left and right, but not up and down. In addition, tapping quickly leads to the fact that the head eventually goes far enough to "fall" and out of sight. Weird

Do you have ideas on what the correct setting should be to allow it to swing up, down, left and right, while remaining remotely centered on this starting position and stay within a certain region so that it does not come off the body and everything looks ridiculous?

BobbleheadView is a subclass of SKView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor =[UIColor clearColor];
        self.showsFPS = YES;
        self.showsNodeCount = YES;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                       initWithTarget:self
                                       action:@selector(animateBobble)];

        [self addGestureRecognizer:tap];

        SKScene *bobbleheadScene = [SKScene sceneWithSize:self.bounds.size];
        [self presentScene:bobbleheadScene];

        // 1. Body
        self.body = [SKSpriteNode spriteNodeWithImageNamed:@"Bobble-Body"];
        self.body.position = CGPointMake(self.frame.size.width/2, self.body.frame.size.height/2);
        self.body.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
        [bobbleheadScene addChild:self.body];

        // 2. Head
        self.head = [SKSpriteNode spriteNodeWithImageNamed:@"Bobble-Head"];
        self.head.position = CGPointMake(self.center.x, self.body.frame.size.height);
        //self.head.physicsBody.affectedByGravity = YES;
//        self.head.physicsBody.dynamic = NO;
        //This bobbles head great, but head falls off body and out of view
        self.head.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.head.size center:self.head.position];
        //End
        //self.head.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.head.frame];
        //self.head.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.head.size.height/2];
        [bobbleheadScene addChild:self.head];

        // 3. Ceiling
        self.ceiling = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(32, 32)];
        self.ceiling.position = CGPointMake(self.frame.origin.x+self.frame.size.width/2, self.frame.size.height);
        self.ceiling.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
        [bobbleheadScene addChild:self.ceiling];



        //Spring Joint for Ceiling to Head
        SKPhysicsJointSpring *spring1 = [SKPhysicsJointSpring jointWithBodyA:self.ceiling.physicsBody bodyB:self.head.physicsBody anchorA:self.ceiling.position anchorB:CGPointMake(self.head.frame.origin.x+self.head.frame.size.width/2, 0)];

        spring1.frequency = 20.0; //gives the spring some elasticity.
        spring1.damping = 5.0; //Will remove damping to create the 'pendulum'
        [bobbleheadScene.physicsWorld addJoint:spring1];


        //Spring Joint for Head to Body
        SKPhysicsJointSpring *spring = [SKPhysicsJointSpring jointWithBodyA:self.body.physicsBody bodyB:self.head.physicsBody anchorA:CGPointMake(self.body.position.x+self.body.size.width/2, self.body.position.y) anchorB:CGPointMake(self.head.position.x+self.head.size.width/2, self.body.position.y-self.body.size.height/2)];

        spring.frequency = 10.0; //gives the spring some elasticity.
        spring.damping = 1.0;
        [bobbleheadScene.physicsWorld addJoint:spring];

    }
    return self;
}

-(void)animateBobble{
    NSLog(@"Did Tap Bobblehead!");
    [self.head.physicsBody applyImpulse:CGVectorMake(100, -200)];
    //[self.body.physicsBody applyImpulse:CGVectorMake(20, 10)];
}
+4
source share
1 answer

The only way to make it work is to make a sled.

So: create an application where there are (say) six sliders on the screen.

TBC I mean, when the application really works, you will see six sliders.

TBC, , , .

( , "" , /, .)

, spring / ( , ), /spring .

! , , .

. , - , . "". , !

+5
source

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


All Articles