iPhoneシーソー

シーソーなiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController ()

@property (nonatomic, weak) SCNView *sceneView;

@property (nonatomic) BOOL jumpBoyOrGirl;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    [self setupScene];

    [self createPark];

    [self createChildren];

}

– (void)setupScene {

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

    sv.scene = [SCNScene scene];

    sv.backgroundColor = [self color:0];

    sv.autoenablesDefaultLighting = YES;

    sv.allowsCameraControl = YES;

    [self.view addSubview:sv];

    

    self.sceneView = sv;

}

– (void)createPark {

    SCNCylinder *ground = [SCNCylinder cylinderWithRadius:20 height:1];

    ground.firstMaterial.diffuse.contents = [self color:1];

    SCNNode *groundNode = [SCNNode nodeWithGeometry:ground];

    groundNode.physicsBody = [SCNPhysicsBody staticBody];

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

    

    SCNSphere *sphere = [SCNSphere sphereWithRadius:4];

    sphere.firstMaterial.diffuse.contents = [self color:2];

    SCNNode *sphereNode = [SCNNode nodeWithGeometry:sphere];

    sphereNode.position = SCNVector3Make(0, 0, 0);

    sphereNode.physicsBody = [SCNPhysicsBody staticBody];

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

    

    SCNBox *board = [SCNBox boxWithWidth:30 height:1 length:4 chamferRadius:0];

    board.firstMaterial.diffuse.contents = [self color:2];

    SCNNode *boardNode = [SCNNode nodeWithGeometry:board];

    boardNode.position = SCNVector3Make(0, 2.5, 0);

    boardNode.physicsBody = [SCNPhysicsBody dynamicBody];

    boardNode.physicsBody.angularVelocityFactor = SCNVector3Make(0, 0, 1);

    boardNode.physicsBody.friction = 1.0;

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

}

– (void)createChildren {

    

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

        SCNNode *child = [SCNNode node];

        child.name = i ? @”boy” : @”girl”;

        child.position = SCNVector3Make(-12 + i * 24, 8, 0);

        

        SCNSphere *head = [SCNSphere sphereWithRadius:2];

        head.firstMaterial.diffuse.contents = [self color:3 + i];

        SCNNode *headNode = [SCNNode nodeWithGeometry:head];

        headNode.position = SCNVector3Make(0, 1, 0);

        [child addChildNode:headNode];

        

        SCNCone *body = [SCNCone coneWithTopRadius:0.5 bottomRadius:2 height:2];

        body.firstMaterial.diffuse.contents = [self color:3 + i];

        SCNNode *bodyNode = [SCNNode nodeWithGeometry:body];

        bodyNode.position = SCNVector3Make(0, –1, 0);

        [child addChildNode:bodyNode];

        

        child.physicsBody = [SCNPhysicsBody dynamicBody];

        child.physicsBody.physicsShape = [SCNPhysicsShape shapeWithNode:child options:@{SCNPhysicsShapeTypeKey : SCNPhysicsShapeTypeConvexHull}];

        child.physicsBody.friction = 1.0;

        child.physicsBody.damping = 0.3;

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

    }

}

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

{

    self.jumpBoyOrGirl = !self.jumpBoyOrGirl;

    

    NSString *name = self.jumpBoyOrGirl ? @”boy” : @”girl”;

    SCNNode *child = [self.sceneView.scene.rootNode childNodeWithName:name recursively:NO];

    [child.physicsBody applyForce:SCNVector3Make(0, 9, 0) impulse:YES];

}

#define ColorHex(rgb)[UIColor colorWithRed:((rgb & 0xFF0000)>>16)/255.0 green:((rgb & 0xFF00) >> 8)/255.0 blue:(rgb & 0xFF)/255.0 alpha:1.0]

– (UIColor *)color:(int)i {

    if (i>4) return nil;

    int colorCodes[] = {0xF2EADF, 0xDEC175, 0xF2C230, 0x7FBF3F, 0xF288A4};

    return ColorHex(colorCodes[i]);

}

@end