iPhoneくるくる板

繋げた板をくるくるまわすiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController ()

@property (nonatomic, weak) SCNView *sceneView;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    [self setupScene];

    SCNNode *main = [self createWheel:40 x:0];

    SCNPhysicsHingeJoint *joint = [SCNPhysicsHingeJoint jointWithBody:main.physicsBody axis:SCNVector3Make(0, 0, 1) anchor:SCNVector3Zero];

    [self.sceneView.scene.physicsWorld addBehavior:joint];

    [self createCamera];

}

– (void)setupScene {

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

    sv.backgroundColor = [self color:0];

    sv.scene = [SCNScene scene];

    sv.autoenablesDefaultLighting = YES;

    sv.allowsCameraControl = YES;

    [self.view addSubview:sv];

    

    self.sceneView = sv;

}

– (SCNNode *)createWheel:(float)l x:(float)x{

    

    SCNBox *bar = [SCNBox boxWithWidth:l*0.95 height:0.2 length:2 chamferRadius:0];

    bar.firstMaterial.diffuse.contents = [UIColor colorWithHue:l*0.02 saturation:1 brightness:1 alpha:1];//[self color:1];

    SCNNode *barNode = [SCNNode nodeWithGeometry:bar];

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

    barNode.physicsBody = [SCNPhysicsBody dynamicBody];

    barNode.physicsBody.collisionBitMask = 0x0;

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

    

    if (l > 4) {

        SCNNode *right = [self createWheel:l * 0.5 x:x + l*0.25];

        SCNNode *left = [self createWheel:l * 0.5 x:x -l*0.25];

        for (SCNNode *child in @[right, left]) {

            SCNPhysicsHingeJoint *joint = [SCNPhysicsHingeJoint jointWithBodyA:child.physicsBody axisA:SCNVector3Make(0, 0, 1) anchorA:SCNVector3Zero bodyB:barNode.physicsBody axisB:SCNVector3Make(0, 0, 1) anchorB:[self.sceneView.scene.rootNode convertPosition:child.position toNode:barNode]];

            [self.sceneView.scene.physicsWorld addBehavior:joint];

        }

    } else {

        barNode.physicsBody.collisionBitMask = 0xF;

    }

    

    return barNode;

}

– (void)createCamera {

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

    camera.position = SCNVector3Make(35, 0, 60);

    camera.rotation = SCNVector4Make(0, 1, 0.1, 0.5);

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

}

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

{

    SCNBox *box = [SCNBox boxWithWidth:1 height:1 length:1 chamferRadius:0];

    box.firstMaterial.diffuse.contents = [self color:3];

    SCNNode *boxNode = [SCNNode nodeWithGeometry:box];

    int rand = arc4random() % 40;

    boxNode.position = SCNVector3Make(rand – 20,10,0);

    boxNode.physicsBody = [SCNPhysicsBody dynamicBody];

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

    

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

        [boxNode removeFromParentNode];

    });

}

#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 colorCode[] = {0x006D8D, 0x008A6E, 0x549E39, 0x8AB833, 0xC0CF3A};

    return ColorHex(colorCode[i]);

}

@end