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) {
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];
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];
self.head = [SKSpriteNode spriteNodeWithImageNamed:@"Bobble-Head"];
self.head.position = CGPointMake(self.center.x, self.body.frame.size.height);
self.head.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.head.size center:self.head.position];
[bobbleheadScene addChild:self.head];
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];
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;
spring1.damping = 5.0;
[bobbleheadScene.physicsWorld addJoint:spring1];
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;
spring.damping = 1.0;
[bobbleheadScene.physicsWorld addJoint:spring];
}
return self;
}
-(void)animateBobble{
NSLog(@"Did Tap Bobblehead!");
[self.head.physicsBody applyImpulse:CGVectorMake(100, -200)];
}
source
share