Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created September 17, 2015 14:08
Show Gist options
  • Save mzsima/e21548d17c7c02451e1e to your computer and use it in GitHub Desktop.
Save mzsima/e21548d17c7c02451e1e to your computer and use it in GitHub Desktop.
cylinder elevator
//
// ViewController.m
// CylinderElevator
//
// Created by MizushimaYusuke on 9/17/15.
// Copyright (c) 2015 MizushimaYusuke. All rights reserved.
//
#import "ViewController.h"
@import SceneKit;
@interface ViewController () <SCNPhysicsContactDelegate, SCNSceneRendererDelegate>
@property (nonatomic, weak) SCNView *sceneView;
@property (nonatomic, weak) SCNNode *player;
@property (nonatomic, weak) SCNNode *elevator;
@property (nonatomic, weak) SCNNode *camera;
@property (nonatomic) BOOL up;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createGround];
[self createCylinder];
[self createElevator];
[self createPlayer];
[self createCamera];
}
- (void)setupScene {
SCNView *sv = [[SCNView alloc] initWithFrame:self.view.bounds];
sv.backgroundColor = [UIColor colorWithHue:0.6 saturation:0.1 brightness:1 alpha:1];
sv.scene = [SCNScene scene];
sv.autoenablesDefaultLighting = YES;
sv.allowsCameraControl = YES;
[self.view addSubview:sv];
sv.delegate = self;
sv.scene.physicsWorld.contactDelegate = self;
self.sceneView = sv;
}
- (void)createGround {
SCNBox *b = [SCNBox boxWithWidth:10 height:1 length:10 chamferRadius:0];
b.firstMaterial.diffuse.contents = [UIColor lightGrayColor];
SCNNode *bNode = [SCNNode nodeWithGeometry:b];
bNode.position = SCNVector3Make(0, -4, 0);
bNode.physicsBody = [SCNPhysicsBody staticBody];
[self.sceneView.scene.rootNode addChildNode:bNode];
}
- (void)createCylinder {
SCNCylinder *c = [SCNCylinder cylinderWithRadius:2 height:8];
c.firstMaterial.diffuse.contents = [UIColor lightGrayColor];
SCNNode *cNode = [SCNNode nodeWithGeometry:c];
cNode.physicsBody = [SCNPhysicsBody staticBody];
cNode.physicsBody.friction = 1;
SCNPhysicsHingeJoint *pin = [SCNPhysicsHingeJoint jointWithBody:cNode.physicsBody axis:SCNVector3Make(0, 1, 0) anchor:SCNVector3Zero];
[self.sceneView.scene.physicsWorld addBehavior:pin];
[self.sceneView.scene.rootNode addChildNode:cNode];
}
- (void)createElevator {
SCNBox *box = [SCNBox boxWithWidth:2 height:0.02 length:2 chamferRadius:0];
box.firstMaterial.diffuse.contents = [UIColor redColor];
SCNNode *elevator = [SCNNode nodeWithGeometry:box];
elevator.position = SCNVector3Make(0, -3.3, 3);
elevator.physicsBody = [SCNPhysicsBody dynamicBody];
elevator.physicsBody.angularVelocityFactor = SCNVector3Make(0, 0, 0);
[self.sceneView.scene.rootNode addChildNode:elevator];
self.elevator = elevator;
}
- (void)createPlayer {
SCNBox *p = [SCNBox boxWithWidth:1 height:1 length:1 chamferRadius:0.1];
p.firstMaterial.diffuse.contents = [UIColor yellowColor];
SCNNode *pNode = [SCNNode nodeWithGeometry:p];
pNode.position = SCNVector3Make(-4, 0, 3);
pNode.physicsBody = [SCNPhysicsBody dynamicBody];
pNode.physicsBody.friction = 1;
[self.sceneView.scene.rootNode addChildNode:pNode];
self.player = pNode;
}
- (void)createCamera {
SCNNode *camera = [SCNNode node];
camera.camera = [SCNCamera camera];
camera.rotation = SCNVector4Make(1, 0, 0, -0.4);
[self.sceneView.scene.rootNode addChildNode:camera];
self.camera = camera;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.player.physicsBody applyForce:SCNVector3Make(2.2, 3, 0) impulse:YES];
}
- (void)physicsWorld:(SCNPhysicsWorld *)world didBeginContact:(SCNPhysicsContact *)contact {
if ((contact.nodeA == self.player && contact.nodeB == self.elevator)
|| (contact.nodeB == self.player && contact.nodeA == self.elevator)) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.up = YES;
});
}
}
- (void)renderer:(id<SCNSceneRenderer>)aRenderer updateAtTime:(NSTimeInterval)time {
if (self.up) {
float vx = self.elevator.presentationNode.position.z;
float vz = -self.elevator.presentationNode.position.x;
self.elevator.physicsBody.velocity = SCNVector3Make(vx, 1, vz);
}
self.camera.position = SCNVector3Make(0, self.player.presentationNode.position.y + 5, 10);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment