バックに回転する車を表示するのiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SceneKit;
@interface ViewController () <SCNSceneRendererDelegate>
@property (nonatomic, weak) SCNView *sceneView;
@property (nonatomic) int status;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createGround];
[self createCar];
[self createCamera];
}
– (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.scene = [SCNScene scene];
sv.delegate = self;
sv.backgroundColor = [UIColor purpleColor];
sv.allowsCameraControl = YES;
sv.autoenablesDefaultLighting = YES;
[self.view addSubview:sv];
self.sceneView = sv;
}
– (void)createGround {
SCNBox *ground = [SCNBox boxWithWidth:30 height:0.2 length:30 chamferRadius:0];
ground.firstMaterial.diffuse.contents = [UIColor brownColor];
SCNNode *groundNode = [SCNNode nodeWithGeometry:ground];
groundNode.physicsBody = [SCNPhysicsBody staticBody];
groundNode.position = SCNVector3Make(0, –2, 0);
[self.sceneView.scene.rootNode addChildNode:groundNode];
}
– (void)createCar {
SCNNode *car = [SCNNode node];
car.name = @”car”;
[self.sceneView.scene.rootNode addChildNode:car];
UIBezierPath *path = [UIBezierPath bezierPath];
[path addArcWithCenter:CGPointZero radius:1 startAngle:0 endAngle:M_PI clockwise:NO];
[path closePath];
path.flatness = 0.01;
SCNShape *body = [SCNShape shapeWithPath:path extrusionDepth:1];
body.firstMaterial.diffuse.contents = [UIColor redColor];
SCNNode *bodyNode = [SCNNode nodeWithGeometry:body];
[car addChildNode:bodyNode];
for (int i=0; i<4; i++) {
SCNCylinder *c = [SCNCylinder cylinderWithRadius:0.2 height:0.1];
c.firstMaterial.diffuse.contents = [UIColor darkGrayColor];
SCNNode *tire = [SCNNode nodeWithGeometry:c];
tire.pivot = SCNMatrix4MakeRotation(M_PI * 0.5, 1, 0, 0);
float x = (i % 2) == 0 ? 0.7 : –0.7;
float z = (i / 2) == 0 ? 0.4 : –0.4;
tire.position = SCNVector3Make(x, 0, z);
[car addChildNode:tire];
}
car.physicsBody = [SCNPhysicsBody dynamicBody];
car.physicsBody.physicsShape = [SCNPhysicsShape shapeWithNode:car options:@{SCNPhysicsShapeTypeKey : SCNPhysicsShapeTypeConvexHull}];
car.physicsBody.friction = 0.1;
car.position = SCNVector3Make(-10, 2, 0);
[car.physicsBody applyTorque:SCNVector4Make(0, 0, 1, 1.5) impulse:YES];
}
– (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.position = SCNVector3Make(-5, 0, 30);
[self.sceneView.scene.rootNode addChildNode:camera];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.status = (self.status + 1) % 3;
self.sceneView.playing = YES;
}
– (void)renderer:(id<SCNSceneRenderer>)aRenderer updateAtTime:(NSTimeInterval)time
{
SCNNode *car = [self.sceneView.scene.rootNode childNodeWithName:@”car” recursively:NO];
if (self.status == 1) {
car.physicsBody.velocity = SCNVector3Make(2, 0, 0);
} else if (self.status == 2) {
self.status = 0;
[car.physicsBody applyForce:SCNVector3Make(0, 2.2, 0) atPosition:SCNVector3Make(2, 0, 0) impulse:YES];
}
}
@end