iPhoneマジックハンド

棒をつなげ合わせてマジックハンド。という感じでiPhoneアプリのサンプルコードを描いてみます。

今回使った画像


動かすとこんな感じです

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface ManipulatorScene : SKScene

@end

@implementation ManipulatorScene

– (void)didMoveToView:(SKView *)view

{

    self.backgroundColor = [SKColor lightGrayColor];

    self.physicsWorld.gravity = CGVectorMake(0, –0.3);

    [self createManipulator];

    

    SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(320, 120)];

    ground.position = CGPointMake(160, 60);

    [self addChild:ground];

}

– (void)createManipulator

{

    int max = 16;

    for (int i=0; i<max; i++) {

        

        float x = 160;

        float y = (i / 2) * 30 + 120;

        SKSpriteNode *bar = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(50, 10)];

        bar.name = [NSString stringWithFormat:@”BAR%d”, i];

        bar.position = CGPointMake(x, y);

        bar.zRotation = (i%2) ? (M_PI/4.0) : – (M_PI/4.0);

        [self addChild:bar];

        bar.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bar.size];

        bar.physicsBody.angularDamping = 1.0;

        bar.physicsBody.restitution = 0;

        bar.physicsBody.friction = 1.0;

        bar.physicsBody.categoryBitMask = 0x1 << 1;

        bar.physicsBody.collisionBitMask = 0x1 << 2;

        

        if (i == 0) {

            bar.physicsBody.dynamic = NO;

        }

        if (i > 0) {

            if (i % 2) {

                // odd

                SKNode *previous = [self childNodeWithName:[NSString stringWithFormat:@”BAR%d”, i-1]];

                SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:previous.physicsBody bodyB:bar.physicsBody anchor:bar.position];

                pin.shouldEnableLimits = YES;

                pin.lowerAngleLimit = –M_PI * 0.45;

                pin.upperAngleLimit = M_PI * 0.2;

                pin.frictionTorque = 0.001;

                [self.physicsWorld addJoint:pin];

            }

            

            if (i>1) {

                SKSpriteNode *second = (SKSpriteNode*)[self childNodeWithName:[NSString stringWithFormat:@”BAR%d”, (i%2) ? i-3 : i-1]];

                CGPoint anchor = CGPointMake(20 * cos(bar.zRotation + M_PI/2.0) + bar.position.x, –20 * sin(bar.zRotation + M_PI/2.0) + bar.position.y);

                

                SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:second.physicsBody bodyB:bar.physicsBody anchor:anchor];

                [self.physicsWorld addJoint:pin];

                

                if (i % 2) {

                    bar.color = [SKColor blueColor];

                    second.color = [SKColor blueColor];

                } else {

                    bar.color = [SKColor greenColor];

                    second.color = [SKColor greenColor];

                }

            }

        }

        

        if (i == max – 1) {

            SKSpriteNode *glove = [SKSpriteNode spriteNodeWithImageNamed:@”glove”];

            glove.position = bar.position;

            [self addChild:glove];

            glove.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:glove.size];

            glove.physicsBody.density = 0.1;

            glove.physicsBody.categoryBitMask = 0x1 << 1;

            glove.physicsBody.collisionBitMask = 0x1 << 2;

            glove.physicsBody.friction = 1.0;

            glove.physicsBody.restitution = 0;

            

            SKPhysicsJointFixed *fixed = [SKPhysicsJointFixed jointWithBodyA:bar.physicsBody bodyB:glove.physicsBody anchor:bar.position];

            [self.physicsWorld addJoint:fixed];

        }

    }

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    SKNode *bar1 = [self childNodeWithName:@”BAR1″];

    [bar1.physicsBody applyAngularImpulse:0.2];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];

    [self.view addSubview:spriteView];

    

    SKScene *scene = [[ManipulatorScene alloc] initWithSize:spriteView.frame.size];

    [spriteView presentScene:scene];

}

@end