iPhoneビークル

ビークルって言うのがあったので、ちょっとつかってみたiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SceneKit;

@interface ViewController ()

@property (nonatomic, weak) SCNScene *scene;

@property (nonatomic, weak) SCNPhysicsVehicle *myCar;

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    

    [self setupScene];

    [self createGround];

    [self createCar];

    [self createCamera];

}

– (void)setupScene {

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

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

    sceneView.backgroundColor = [UIColor orangeColor];

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

    sceneView.scene = [SCNScene scene];

    [self.view addSubview:sceneView];

    

    self.scene = sceneView.scene;

}

– (void)createGround {

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

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

    SCNNode *boxNode = [SCNNode nodeWithGeometry:box];

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

    boxNode.name = @”ground”;

    [self.scene.rootNode addChildNode:boxNode];

    boxNode.physicsBody = [SCNPhysicsBody staticBody];

    

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

        SCNBox *bar = [SCNBox boxWithWidth:30 height:4 length:0.4 chamferRadius:0];

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

        SCNNode *barNode = [SCNNode nodeWithGeometry:bar];

        

        float x = 15* cos(i * M_PI * 0.5);

        float z = 15 * sin(i * M_PI * 0.5);

        barNode.transform = SCNMatrix4Rotate(barNode.transform, (i+1) * M_PI*0.5, 0, 1, 0);

        barNode.transform = SCNMatrix4Translate(barNode.transform, x, 0, z);

        [self.scene.rootNode addChildNode:barNode];

        

        barNode.physicsBody = [SCNPhysicsBody staticBody];

    }

}

– (void)createCar {

    SCNBox *body = [SCNBox boxWithWidth:1.0 height:0.5 length:4 chamferRadius:0];

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

    SCNNode *bodyNode = [SCNNode nodeWithGeometry:body];

    [self.scene.rootNode addChildNode:bodyNode];

    bodyNode.physicsBody = [SCNPhysicsBody dynamicBody];

    

    NSMutableArray *tires = [NSMutableArray array];

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

        

        float x = (i % 2) ? 1 : –1;

        float z = (i / 2) > 0 ? 1.4 : –1.4;

        

        SCNBox *tire = [SCNBox boxWithWidth:0.5 height:1 length:1 chamferRadius:1];

        tire.firstMaterial.diffuse.contents = [UIColor blackColor];

        SCNNode *tireNode = [SCNNode nodeWithGeometry:tire];

        tireNode.position = SCNVector3Make(x, 0, z);

        [self.scene.rootNode addChildNode:tireNode];

        

        tireNode.physicsBody = [SCNPhysicsBody dynamicBody];

        tireNode.physicsBody.physicsShape = [SCNPhysicsShape shapeWithGeometry:tire options:nil];

        SCNPhysicsVehicleWheel *w = [SCNPhysicsVehicleWheel wheelWithNode:tireNode];

        w.radius = 0.4;

        w.suspensionRestLength = 1.2;

        [tires addObject:w];

    }

    

    SCNPhysicsVehicle *vehicle = [SCNPhysicsVehicle vehicleWithChassisBody:bodyNode.physicsBody wheels:tires];

    [self.scene.physicsWorld addBehavior:vehicle];

    

    self.myCar = vehicle;

}

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

{

    [self.myCar applyEngineForce:5 forWheelAtIndex:1];

    

    [self.myCar setSteeringAngle:-M_PI*0.1 forWheelAtIndex:0];

    [self.myCar setSteeringAngle:-M_PI*0.1 forWheelAtIndex:1];

    [self.myCar setSteeringAngle:M_PI*0.1 forWheelAtIndex:2];

    [self.myCar setSteeringAngle:M_PI*0.1 forWheelAtIndex:3];

    

}

– (void)createCamera {

    SCNNode *camera = [SCNNode node];

    camera.camera = [SCNCamera camera];

    camera.position = SCNVector3Make(5, 20, 25);

    [self.scene.rootNode addChildNode:camera];

    

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

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end