iPhoneドラム三角

ドラム缶に書いた線を回して三角にするiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController () <SCNSceneRendererDelegate>

@property (nonatomic, weak) SCNView *sceneView;

@property (nonatomic) NSTimeInterval last;

@property (nonatomic) int status;

@property (nonatomic) int drumCount;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    [self setupScene];

    [self createGround];

    [self createCamera];

}

– (void)setupScene {

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

    sv.backgroundColor = [UIColor darkGrayColor];

    sv.scene = [SCNScene scene];

    sv.delegate = self;

    sv.autoenablesDefaultLighting = YES;

    [self.view addSubview:sv];

    

    self.sceneView = sv;

}

– (void)createGround {

    SCNBox *g = [SCNBox boxWithWidth:12 height:1 length:6 chamferRadius:0];

    g.firstMaterial.diffuse.contents = [UIColor blackColor];

    SCNNode *ground = [SCNNode nodeWithGeometry:g];

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

    ground.physicsBody = [SCNPhysicsBody staticBody];

}

– (void)falldownDrum:(int)drumNo {

    float r = 1;

    

    float x = 2.0 * (drumNo % 6) * r – 5.0;

    SCNCylinder *c = [SCNCylinder cylinderWithRadius:1 height:3];

    c.firstMaterial.diffuse.contents = [UIColor whiteColor];

    SCNNode *drum = [SCNNode nodeWithGeometry:c];

    drum.name = [NSString stringWithFormat:@”drum%d”, drumNo];

    drum.position = SCNVector3Make(x, 20, 0);

    drum.rotation = SCNVector4Make(1, 0, 0, M_PI * 0.5);

    drum.physicsBody = [SCNPhysicsBody dynamicBody];

    drum.physicsBody.velocityFactor = SCNVector3Make(0, 1, 0);

    drum.physicsBody.angularVelocityFactor = SCNVector3Make(0, 0, 0);

    

    SCNBox *bar = [SCNBox boxWithWidth:0.25 height:0.05 length:1.8 chamferRadius:0.05];

    bar.firstMaterial.diffuse.contents = [UIColor greenColor];

    SCNNode *barNode = [SCNNode nodeWithGeometry:bar];

    barNode.position = SCNVector3Make(0, 1.5, 0);

    barNode.opacity = 0.2;

    [drum addChildNode:barNode];

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

}

– (void)createCamera {

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

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

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

}

– (void)renderer:(id<SCNSceneRenderer>)aRenderer updateAtTime:(NSTimeInterval)time {

    if (self.last == 0) {

        self.last = time;

    }

    

    if (self.drumCount > 0 && self.drumCount < 36 && time – self.last > 0.2) {

        [self falldownDrum:self.drumCount];

        self.last = time;

        ++self.drumCount;

    }

}

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

    if (self.status == 0) {

        [self falldownDrum:self.drumCount];

        ++self.drumCount;

        ++self.status;

        self.sceneView.playing = YES;

        return;

    } else if (self.status == 1) {

        for (int i=0; i<36; i+=7) {

            SCNNode *n = [self.sceneView.scene.rootNode childNodeWithName:[NSString stringWithFormat:@”drum%d”, i] recursively:NO];

            [n.childNodes[0] runAction:[SCNAction rotateToX:0 y:-M_PI/4.0 z:0 duration:1.0]];

            [n.childNodes[0] runAction:[SCNAction fadeInWithDuration:0.4]];

        }

        for (int i=1; i<6; i++) {

            SCNNode *n = [self.sceneView.scene.rootNode childNodeWithName:[NSString stringWithFormat:@”drum%d”, i] recursively:NO];

            [n.childNodes[0] runAction:[SCNAction rotateToX:0 y:M_PI/2.0 z:0 duration:1.0]];

            [n.childNodes[0] runAction:[SCNAction fadeInWithDuration:0.4]];

        }

        ++self.status;

    } else if (self.status == 2) {

        for (int i=5; i<36; i+=5) {

            SCNNode *n = [self.sceneView.scene.rootNode childNodeWithName:[NSString stringWithFormat:@”drum%d”, i] recursively:NO];

            [n.childNodes[0] runAction:[SCNAction rotateToX:0 y:M_PI/4.0 z:0 duration:1.0]];

            [n.childNodes[0] runAction:[SCNAction fadeInWithDuration:0.4]];

        }

        for (int i=30; i<36; i++) {

            SCNNode *n = [self.sceneView.scene.rootNode childNodeWithName:[NSString stringWithFormat:@”drum%d”, i] recursively:NO];

            [n.childNodes[0] runAction:[SCNAction rotateToX:0 y:M_PI/2.0 z:0 duration:1.0]];

            [n.childNodes[0] runAction:[SCNAction fadeInWithDuration:0.4]];

        }

        ++self.status;

    }

}

@end