Building a simple element for your game with SKPhysics and SKPhysicsJointPin. Here is the class code for a simple physical seesaw like in this video
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
// // WWSeesaw.m // PoBo // // Created by zinne on 18.07.14. // Copyright (c) 2014 waveworks. All rights reserved. // #import "WWSeesaw.h" @implementation WWSeesaw +(void)addSeesaw: (CGPoint)pos game:(SKScene*)game { SKSpriteNode * anchor = [SKSpriteNode spriteNodeWithColor:[UIColor clearColor] size:CGSizeMake(1, 1)]; anchor.position = pos; anchor.name = @"whip_anchor"; anchor.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:1]; anchor.physicsBody.affectedByGravity = false; anchor.physicsBody.dynamic = false; [game addChild:anchor]; SKSpriteNode * whip = [SKSpriteNode spriteNodeWithImageNamed:@"seesaw.png"]; whip.name = @"whip"; whip.size = CGSizeMake(200, 15); whip.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(200, 15)]; [anchor addChild:whip]; SKSpriteNode * gear = [SKSpriteNode spriteNodeWithImageNamed:@"gear_1.png"]; gear.size = CGSizeMake(20, 20); gear.name = @"gear"; [anchor addChild:gear]; SKPhysicsJointPin* jointPin = [SKPhysicsJointPin jointWithBodyA:anchor.physicsBody bodyB:whip.physicsBody anchor:anchor.position]; [game.physicsWorld addJoint:jointPin]; } @end |
Thanks for the very helpful post. Can you please also show example of making a „cross“ – with 2 whips connected to the same anchor point in the middle rather than just a seesaw? I tried to add a second whip to the same anchor point but it doesn’t work…
Thanks
Got the „cross“ working by joining 2nd rectangle to the first one using fixed joint (SKPhysicsJointFixed). Code available on my blog as below. Thanks.
http://new2objectivec.blogspot.com.au/2014/08/how-to-create-cross-or-plus-shape.html
Hi,
I added a new tutorial for this crosses you asked:
http://www.free-pobo.com/build-a-rotating-mill-with-sprite-kit-physics/
Thanks you!