iPhoneスリングショット

引っ張ってボールを飛ばすiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController ()

@property (nonatomic, weak) SCNView *sceneView;

@property (nonatomic, weak) SCNScene *scene;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithHue:0.3 saturation:0.3 brightness:0.7 alpha:1];

    [self setupScene];

    [self createGround];

    [self createBall];

    [self createSlingShot];

    [self createCamera];

    [self createLight];

}

– (void)setupScene

{

    float w = CGRectGetMaxX(self.view.bounds);

    SCNView *sceneView = [[SCNView alloc] initWithFrame:CGRectMake(0, 0, w, w)];

    sceneView.backgroundColor = [UIColor colorWithHue:0.7 saturation:0.1 brightness:1 alpha:1];

    sceneView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));

    sceneView.scene = [SCNScene scene];

    [self.view addSubview:sceneView];

    

    self.scene = sceneView.scene;

    self.sceneView = sceneView;

}

– (void)createSlingShot

{

    UIColor *color = [UIColor colorWithHue:0.4 saturation:0.6 brightness:1 alpha:1];

    SCNCylinder *handle = [SCNCylinder cylinderWithRadius:0.4 height:4];

    handle.firstMaterial.diffuse.contents = color;

    SCNNode *handleNode = [SCNNode nodeWithGeometry:handle];

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

    [self.scene.rootNode addChildNode:handleNode];

    

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

        SCNCylinder *bar = [SCNCylinder cylinderWithRadius:0.4 height:3];

        bar.firstMaterial.diffuse.contents = color;

        SCNNode *barNode = [SCNNode nodeWithGeometry:bar];

        if (i == 0) {

            barNode.transform = SCNMatrix4Rotate(barNode.transform, M_PI/4.0, 0, 0, 1);

            barNode.transform = SCNMatrix4Translate(barNode.transform, –1, 2, 0);

        } else {

            barNode.transform = SCNMatrix4Rotate(barNode.transform, –M_PI/4.0, 0, 0, 1);

            barNode.transform = SCNMatrix4Translate(barNode.transform, 1, 2, 0);

        }

        [self.scene.rootNode addChildNode:barNode];

    }

}

– (void)createGround

{

    SCNTorus *torus = [SCNTorus torusWithRingRadius:18 pipeRadius:8];

    torus.firstMaterial.diffuse.contents = [UIColor colorWithHue:0.5 saturation:0.4 brightness:1 alpha:1];

    SCNNode *ground = [SCNNode nodeWithGeometry:torus];

    ground.name = @”ground”;

    ground.position = SCNVector3Make(0, –10, –60);

    ground.physicsBody = [SCNPhysicsBody staticBody];

    ground.physicsBody.physicsShape = [SCNPhysicsShape shapeWithGeometry:ground.geometry options:nil];

    [self.scene.rootNode addChildNode:ground];

}

– (void)createBall

{

    SCNSphere *ball = [SCNSphere sphereWithRadius:1];

    ball.firstMaterial.diffuse.contents = [UIColor colorWithHue:0 saturation:0.7 brightness:1 alpha:1];

    SCNNode *ballNode = [SCNNode nodeWithGeometry:ball];

    ballNode.name = @”ball”;

    ballNode.position = SCNVector3Make(0, 4, 0);

    [self.scene.rootNode addChildNode:ballNode];

}

– (void)createCamera

{

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

    camera.position = SCNVector3Make(0, 20, 20);

    camera.constraints = @[[SCNLookAtConstraint lookAtConstraintWithTarget:[self.scene.rootNode childNodeWithName:@”ground” recursively:NO]]];

    [self.scene.rootNode addChildNode:camera];

}

– (void)createLight

{

    SCNLight *light = [SCNLight light];

    light.type = SCNLightTypeOmni;

    SCNNode *lightNode = [SCNNode node];

    lightNode.light = light;

    lightNode.position = SCNVector3Make(0, 10, 50);

    [self.scene.rootNode addChildNode:lightNode];

}

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

{

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

    ball.physicsBody = nil;

}

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

{

    CGPoint p = [[touches anyObject] locationInView:self.sceneView];

    

    SCNVector3 o = [self.sceneView projectPoint:SCNVector3Zero];

    SCNVector3 vector = [self.sceneView unprojectPoint:SCNVector3Make(p.x, p.y, o.z)];

    SCNVector3 newVector = SCNVector3Make(vector.x, vector.y, o.z – vector.y + 1);

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

    ball.position = newVector;

}

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

{

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

    ball.physicsBody = [SCNPhysicsBody dynamicBody];

    ball.physicsBody.physicsShape = [SCNPhysicsShape shapeWithGeometry:ball.geometry options:nil];

    

    [ball.physicsBody applyForce:SCNVector3Make(-5.0 * ball.position.x, –15.0 * ball.position.y, –10.0 * ball.position.z) impulse:YES];

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end