iPhoneヘディング

ヘディングシュートするiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController ()

@property (nonatomic, weak) SCNView *sceneView;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    [self setupScene];

    [self createGoal];

    [self createPlayer];

    [self createBall];

    [self createCamera];

}

– (void)setupScene {

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

    sv.scene = [SCNScene scene];

    sv.allowsCameraControl = YES;

    sv.backgroundColor = [UIColor blackColor];

    [self.view addSubview:sv];

    

    self.sceneView = sv;

}

– (void)createGoal {

    

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

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

    SCNNode *groundNode = [SCNNode nodeWithGeometry:ground];

    groundNode.physicsBody = [SCNPhysicsBody staticBody];

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

    

    

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

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

    SCNNode *boxNode = [SCNNode nodeWithGeometry:box];

    boxNode.position = SCNVector3Make(0, 5, –15);

    boxNode.physicsBody = [SCNPhysicsBody staticBody];

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

    

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

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

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

        SCNNode *boxNode = [SCNNode nodeWithGeometry:box];

        boxNode.position = SCNVector3Make(i ? –6 : 6, 2.5, –15);

        boxNode.physicsBody = [SCNPhysicsBody staticBody];

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

    }

}

– (void)createBall {

    SCNSphere *ball = [SCNSphere sphereWithRadius:1];

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

    SCNNode *ballNode = [SCNNode nodeWithGeometry:ball];

    ballNode.position = SCNVector3Make(12, 1, –12);

    ballNode.physicsBody = [SCNPhysicsBody dynamicBody];

    ballNode.physicsBody.mass = 0.1;

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

    

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

        [ballNode.physicsBody applyForce:SCNVector3Make(-1.1, 0.9, 1) impulse:YES];

    });

    

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

        [ballNode removeFromParentNode];

        [self createBall];

    });

}

– (void)createPlayer {

    SCNNode *player = [SCNNode node];

    player.name = @”player”;

    player.position = SCNVector3Make(-2, 2, 5);

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

    

    SCNSphere *head = [SCNSphere sphereWithRadius:1];

    head.firstMaterial.diffuse.contents = [UIColor purpleColor];

    SCNNode *headNode = [SCNNode nodeWithGeometry:head];

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

    [player addChildNode:headNode];

    

    SCNCone *body = [SCNCone coneWithTopRadius:1.2 bottomRadius:0.2 height:1];

    body.firstMaterial.diffuse.contents = [UIColor purpleColor];

    SCNNode *bodyNode = [SCNNode nodeWithGeometry:body];

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

    [player addChildNode:bodyNode];

}

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

    

    SCNNode *player = [self.sceneView.scene.rootNode childNodeWithName:@”player” recursively:NO];

    player.physicsBody = [SCNPhysicsBody dynamicBody];

    player.physicsBody.angularDamping = 0.9;

    player.physicsBody.damping = 0.8;

    [player.physicsBody applyForce:SCNVector3Make(1, 8, –15) atPosition:SCNVector3Make(0, 1, –1) impulse:YES];

    

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

        [player removeFromParentNode];

        [self createPlayer];

    });

}

– (void)createCamera {

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

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

    camera.rotation = SCNVector4Make(1, 0, 0, –0.2);

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

}

@end