タップで壁を出して、ボールをはねかえすiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SceneKit;
@interface ViewController ()
@property (nonatomic, weak) SCNView *sceneView;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createPanel];
[self createDisk];
[self createCamera];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.scene = [SCNScene scene];
sv.backgroundColor = [UIColor orangeColor];
sv.autoenablesDefaultLighting = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createPanel {
float dw = M_PI / 2.0;
for (int i=0; i<4; i++) {
SCNBox *wall = [SCNBox boxWithWidth:5 height:1 length:0.1 chamferRadius:0];
wall.firstMaterial.diffuse.contents = [UIColor darkGrayColor];
SCNNode *wallNode =[SCNNode nodeWithGeometry:wall];
wallNode.position = SCNVector3Make(2.5 * cos(i * dw) , 0.125, 2.5 * sin(i * dw));
if (i % 2 == 0) {
wallNode.rotation = SCNVector4Make(0, 1, 0, dw);
}
wallNode.physicsBody = [SCNPhysicsBody staticBody];
[self.sceneView.scene.rootNode addChildNode:wallNode];
}
for (int i=0; i<25; i++) {
float x = (i % 5) – 2;
float z = (i / 5) – 2;
SCNBox *box = [SCNBox boxWithWidth:1 height:0.4 length:1 chamferRadius:0];
NSMutableArray *arr = [NSMutableArray array];
SCNMaterial *m0 = [SCNMaterial material];
m0.diffuse.contents = [UIColor lightGrayColor];
SCNMaterial *m1 = [SCNMaterial material];
m1.diffuse.contents = [UIColor yellowColor];
[arr addObject:m0];
[arr addObject:m1];
box.materials = arr;
SCNNode *boxNode = [SCNNode nodeWithGeometry:box];
boxNode.name = @”panel”;
boxNode.position = SCNVector3Make(x, 0, z);
boxNode.physicsBody = [SCNPhysicsBody staticBody];
boxNode.physicsBody.friction = 0;
[self.sceneView.scene.rootNode addChildNode:boxNode];
}
}
– (void)createDisk {
SCNSphere *c = [SCNSphere sphereWithRadius:0.3];
c.firstMaterial.diffuse.contents = [UIColor whiteColor];
SCNNode *cNode = [SCNNode nodeWithGeometry:c];
cNode.name = @”disk”;
cNode.position = SCNVector3Make(0, 0.2, 0);
cNode.physicsBody = [SCNPhysicsBody dynamicBody];
cNode.physicsBody.damping = 0;
cNode.physicsBody.friction = 0;
cNode.physicsBody.angularVelocityFactor = SCNVector3Make(0, 0, 0);
cNode.physicsBody.velocityFactor = SCNVector3Make(1, 0.1, 1);
[self.sceneView.scene.rootNode addChildNode:cNode];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint p = [[touches anyObject] locationInView:self.view];
SCNHitTestResult *n = [self.sceneView hitTest:p options:@{SCNHitTestSortResultsKey : @YES}].firstObject;
if ([n.node.name isEqual:@”panel”]) {
[n.node runAction:[SCNAction rotateByAngle:M_PI aroundAxis:SCNVector3Make(1, 0, 0) duration:0.5]];
if (n.node.position.y == 0) {
n.node.position = SCNVector3Make(n.node.position.x, 0.4, n.node.position.z);
} else {
n.node.position = SCNVector3Make(n.node.position.x, 0, n.node.position.z);
}
} else if ([n.node.name isEqual:@”disk”]) {
float x = arc4random() % 3;
float z = arc4random() % 3;
[n.node.physicsBody applyForce:SCNVector3Make((x – 1) * 5, 0, (z – 1) * 5) impulse:YES];
}
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(0, 6, 8);
camera.rotation = SCNVector4Make(1, 0, 0, –0.5);
[self.sceneView.scene.rootNode addChildNode:camera];
}
@end