Skip to content

Commit

Permalink
Merge pull request #157 from DJBen/solar-touch
Browse files Browse the repository at this point in the history
Update calculation method of solar systme scene tap handler
  • Loading branch information
DJBen authored Apr 5, 2018
2 parents a40fe12 + f9dc6e1 commit 37febef
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
10 changes: 10 additions & 0 deletions Graviton/Common/Utils/SceneKit+Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ public let flipTextureContentsTransform: SCNMatrix4 = {
mtx = SCNMatrix4Translate(mtx, 0.5, 0.5, 0)
return mtx
}()

extension CGPoint {
init(_ vector: SCNVector3) {
self.init(x: CGFloat(vector.x), y: CGFloat(vector.y))
}

func distance(toPoint p: CGPoint) -> CGFloat {
return sqrt(pow(x - p.x, 2) + pow(y - p.y, 2))
}
}
2 changes: 1 addition & 1 deletion Graviton/SolarSystem/Scenes/SolarSystemScene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SolarSystemScene: SCNScene, CameraResponsive, FocusingSupport {

private var orbitalMotions = [(OrbitalMotion, UIColor, Int)]()
private var lineSegments = SCNNode()
private var spheres = SCNNode()
var spheres = SCNNode()
var celestialBodies: [CelestialBody] = []

var focusedNode: SCNNode?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import MathUtil

class SolarSystemViewController: SceneController {

static let focusDistanceThreshold: CGFloat = 30

var focusController: FocusingSupport?

var lastRenderTime: TimeInterval!
Expand Down Expand Up @@ -122,11 +124,13 @@ class SolarSystemViewController: SceneController {

@objc func handleTap(sender: UITapGestureRecognizer) {
let scnView = self.view as! SCNView
let p = sender.location(in: scnView)
let hitResults = scnView.hitTest(p, options: [.boundingBoxOnly: true])
let objectHit = hitResults.map { $0.node }.filter { $0.name != nil && $0.name!.contains("orbit") == false }
if objectHit.count > 0 {
let node = objectHit[0]
let point = sender.location(in: view)
let distances = solarSystemScene.spheres.childNodes.map { (sphere) -> (SCNNode, CGFloat) in
let screenPoint = CGPoint(scnView.projectPoint(sphere.position))
return (sphere, point.distance(toPoint: screenPoint))
}.sorted { $0.1 < $1.1 }.filter { $0.1 < SolarSystemViewController.focusDistanceThreshold }

if let (node, _) = distances.first {
SCNTransaction.begin()
SCNTransaction.animationDuration = 0.25
focusController?.focus(atNode: node)
Expand Down

0 comments on commit 37febef

Please sign in to comment.