iPhoneカードタワー

カードでタワーを表示して、パラパラ崩すiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController ()

@property (nonatomic, weak) SCNView *sceneView;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    [self setupScene];

    [self createDesk];

    [self createCards];

    [self createCamera];

}

– (void)setupScene {

    SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];

    sv.scene = [SCNScene scene];

    sv.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];

    sv.autoenablesDefaultLighting = true;

    sv.allowsCameraControl = true;

    [self.view addSubview:sv];

    

    self.sceneView = sv;

}

– (void)createDesk {

    SCNCylinder *desk = [SCNCylinder cylinderWithRadius:3 height:0.2];

    desk.firstMaterial.diffuse.contents = [UIColor brownColor];

    SCNNode *deskNode = [SCNNode nodeWithGeometry:desk];

    [self.sceneView.scene.rootNode addChildNode:deskNode];

    

    deskNode.physicsBody = [SCNPhysicsBody staticBody];

}

– (void)createCards {

    float delay = 0;

    float l = 0.6, h = l * 1.4;

    int count = 10, n = 10;

    

    float angle = M_PI * 0.115;

    float h0 = h * cos(angle);

    float l0 = h * sin(angle) * 0.5;

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

        for (int j=0; j<count; j++) {

            float x = j * l – l * 4.5 + (n – count) * l * 0.5;

            float y = i * h0 + h0 * 0.5;

            SCNBox *card = [SCNBox boxWithWidth:0.02 height:h length:l chamferRadius:0.01];

            float hue = (arc4random() % 100) * 0.01;

            card.firstMaterial.diffuse.contents = [UIColor colorWithHue:hue saturation:0.1 brightness:1 alpha:1];

            card.firstMaterial.specular.contents = [UIColor redColor];

            SCNNode *cardNode = [SCNNode nodeWithGeometry:card];

            cardNode.name = @”card”;

            cardNode.position = SCNVector3Make(x + l0, y, 0);

            cardNode.rotation = SCNVector4Make(0, 0, 1, angle);

            

            SCNNode *cardNode2 = [SCNNode nodeWithGeometry:card];

            cardNode2.name = @”card”;

            cardNode2.position = SCNVector3Make(x – l0, y, 0);

            cardNode2.rotation = SCNVector4Make(0, 0, 1, -angle);

            

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                [self.sceneView.scene.rootNode addChildNode:cardNode];

                [self.sceneView.scene.rootNode addChildNode:cardNode2];

            });

            delay += 0.04;

        }

        count–;

    }

}

– (void)createCamera {

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

    camera.position = SCNVector3Make(0, 4, 10);

    [self.sceneView.scene.rootNode addChildNode:camera];

}

– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    for (SCNNode *n in self.sceneView.scene.rootNode.childNodes) {

        if ([n.name isEqual:@”card”]) {

            n.physicsBody = [SCNPhysicsBody dynamicBody];

            n.physicsBody.damping = 0.97;

        }

    }

}

@end