Created
August 30, 2015 14:08
-
-
Save mzsima/161863862b7178ca1c32 to your computer and use it in GitHub Desktop.
tower camera
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// TowerCamera | |
// | |
// Created by MizushimaYusuke on 8/30/15. | |
// Copyright (c) 2015 MizushimaYusuke. All rights reserved. | |
// | |
#import "ViewController.h" | |
@import SceneKit; | |
@interface ViewController () <SCNSceneRendererDelegate> | |
@property (nonatomic, weak) SCNView *sceneView; | |
@property (nonatomic, weak) SCNNode *player; | |
@property (nonatomic, weak) SCNNode *cameraScene; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
[self setupScene]; | |
[self createTower]; | |
[self createPlayer]; | |
[self createCamera]; | |
} | |
- (void)setupScene { | |
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds]; | |
sv.backgroundColor = [UIColor colorWithHue:0.6 saturation:0.3 brightness:1 alpha:1]; | |
sv.scene = [SCNScene scene]; | |
sv.autoenablesDefaultLighting = YES; | |
sv.delegate = self; | |
[self.view addSubview:sv]; | |
self.sceneView = sv; | |
} | |
- (void)createTower { | |
SCNCylinder *ground = [SCNCylinder cylinderWithRadius:5 height:0.2]; | |
ground.firstMaterial.diffuse.contents = [UIColor brownColor]; | |
SCNNode *groundNode = [SCNNode nodeWithGeometry:ground]; | |
groundNode.position = SCNVector3Make(0, -5, 0); | |
groundNode.physicsBody = [SCNPhysicsBody staticBody]; | |
[self.sceneView.scene.rootNode addChildNode:groundNode]; | |
SCNCylinder *tower = [SCNCylinder cylinderWithRadius:3.5 height:10]; | |
tower.firstMaterial.diffuse.contents = [UIColor brownColor]; | |
SCNNode *towerNode = [SCNNode nodeWithGeometry:tower]; | |
towerNode.name = @"tower"; | |
towerNode.physicsBody = [SCNPhysicsBody staticBody]; | |
[self.sceneView.scene.rootNode addChildNode:towerNode]; | |
float dw = M_PI / 5.0; | |
for (int i=0; i<9; i++) { | |
float y = i - 3.5; | |
SCNBox *step = [SCNBox boxWithWidth:12 height:0.1 length:2 chamferRadius:0]; | |
step.firstMaterial.diffuse.contents = [UIColor brownColor]; | |
SCNNode *stepNode = [SCNNode nodeWithGeometry:step]; | |
stepNode.position = SCNVector3Make(0, y, 0); | |
stepNode.rotation = SCNVector4Make(0, 1, 0, dw * i); | |
stepNode.physicsBody = [SCNPhysicsBody staticBody]; | |
[self.sceneView.scene.rootNode addChildNode:stepNode]; | |
} | |
} | |
- (void)createPlayer { | |
SCNSphere *player = [SCNSphere sphereWithRadius:0.3]; | |
player.firstMaterial.diffuse.contents = [UIColor greenColor]; | |
SCNNode *playerNode = [SCNNode nodeWithGeometry:player]; | |
playerNode.name = @"player"; | |
playerNode.position = SCNVector3Make(0, -4, 4.5); | |
playerNode.physicsBody = [SCNPhysicsBody dynamicBody]; | |
[self.sceneView.scene.rootNode addChildNode:playerNode]; | |
SCNPhysicsSliderJoint *motor = [SCNPhysicsSliderJoint jointWithBody:playerNode.physicsBody axis:SCNVector3Make(0, 1, 0) anchor:SCNVector3Make(0, -4, -4.5)]; | |
[self.sceneView.scene.physicsWorld addBehavior:motor]; | |
motor.motorMaximumTorque = 0.01; | |
motor.motorTargetAngularVelocity = -0.3; | |
self.player = playerNode; | |
} | |
- (void)createCamera { | |
SCNNode *cs = [SCNNode node]; | |
[self.sceneView.scene.rootNode addChildNode:cs]; | |
SCNNode *camera = [SCNNode node]; | |
camera.camera = [SCNCamera camera]; | |
camera.position = SCNVector3Make(0, 0, 12); | |
[cs addChildNode:camera]; | |
SCNLookAtConstraint *lc = [SCNLookAtConstraint lookAtConstraintWithTarget:self.player]; | |
lc.influenceFactor = 0.1; | |
lc.gimbalLockEnabled = YES; | |
camera.constraints = @[lc]; | |
self.cameraScene = cs; | |
} | |
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { | |
[self.player.physicsBody applyForce:SCNVector3Make(0, 5, 0) impulse:YES]; | |
} | |
- (void)renderer:(id<SCNSceneRenderer>)aRenderer updateAtTime:(NSTimeInterval)time { | |
self.cameraScene.rotation = self.player.presentationNode.rotation; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment