import UIKit import SceneKit import ARKit class ViewController: UIViewController, ARSCNViewDelegate, SCNPhysicsContactDelegate { @IBOutlet var sceneView: ARSCNView! enum nesneTipi :Int { case mermi = 1 case hedef = 2 } override func viewDidLoad() { super.viewDidLoad() // Set the view's delegate sceneView.delegate = self sceneView.scene.physicsWorld.contactDelegate = self //sceneView.debugOptions = [ARSCNDebugOptions.showWorldOrigin, ARSCNDebugOptions.showFeaturePoints] let tikla = UITapGestureRecognizer(target: self, action: #selector(tiklamaSonucu)) sceneView.addGestureRecognizer(tikla) hedefEkle() } @objc func tiklamaSonucu(tiklama:UITapGestureRecognizer) { guard let ekran = tiklama.view as? ARSCNView else {return} guard let pov = ekran.pointOfView else {return} let transform = pov.transform let orientation = SCNVector3(-transform.m31, -transform.m32, -transform.m33) let lokasyon = SCNVector3(transform.m41, transform.m42, transform.m43) let pozisyon = SCNVector3(orientation.x + lokasyon.x, orientation.y + lokasyon.y, orientation.z + lokasyon.z) let mermi = SCNNode() mermi.name = "Mermi" mermi.geometry = SCNSphere(radius: 0.35) mermi.geometry?.firstMaterial?.diffuse.contents = UIColor.purple mermi.position = pozisyon mermi.physicsBody = SCNPhysicsBody(type: .dynamic, shape: SCNPhysicsShape(node: mermi, options: nil)) mermi.physicsBody?.isAffectedByGravity = false mermi.physicsBody?.categoryBitMask = nesneTipi.mermi.rawValue mermi.physicsBody?.contactTestBitMask = nesneTipi.hedef.rawValue let kuvvet: Float = 50 mermi.physicsBody?.applyForce(SCNVector3(orientation.x * kuvvet, orientation.y * kuvvet * (-2), orientation.z * kuvvet), asImpulse: true) sceneView.scene.rootNode.addChildNode(mermi) } func hedefEkle() { let piramid = SCNNode() piramid.name = "Piramid" piramid.geometry = SCNPyramid(width: 4, height: 4.5, length: 4) piramid.geometry?.firstMaterial?.diffuse.contents = UIColor.orange piramid.position = SCNVector3(-3,1,-15) piramid.physicsBody = SCNPhysicsBody(type: .static, shape: nil) piramid.physicsBody?.categoryBitMask = nesneTipi.hedef.rawValue sceneView.scene.rootNode.addChildNode(piramid) let simit = SCNNode() simit.name = "Simit" simit.geometry = SCNTorus(ringRadius: 2, pipeRadius: 0.5) simit.geometry?.firstMaterial?.diffuse.contents = UIColor.blue simit.position = SCNVector3(0,-2,-15) simit.physicsBody = SCNPhysicsBody(type: .static, shape: SCNPhysicsShape(node: simit, options: [SCNPhysicsShape.Option.type: SCNPhysicsShape.ShapeType.concavePolyhedron])) let doksanderece = GLKMathDegreesToRadians(90) simit.eulerAngles = SCNVector3(doksanderece,0,0) simit.physicsBody?.categoryBitMask = nesneTipi.hedef.rawValue sceneView.scene.rootNode.addChildNode(simit) let kutu = SCNNode() kutu.name = "Kutu" kutu.geometry = SCNBox(width: 3.5, height: 3.5, length: 3.5, chamferRadius: 0) kutu.geometry?.firstMaterial?.diffuse.contents = UIColor.green kutu.position = SCNVector3(5,1,-15) kutu.physicsBody = SCNPhysicsBody(type: .static, shape: nil) kutu.physicsBody?.categoryBitMask = nesneTipi.hedef.rawValue sceneView.scene.rootNode.addChildNode(kutu) } func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) { var kaynakNode : SCNNode! if contact.nodeA.name == "Mermi" { kaynakNode = contact.nodeB } else { kaynakNode = contact.nodeA } switch kaynakNode.name { case "Piramid": kaynakNode.geometry?.firstMaterial?.diffuse.contents = UIColor.magenta case "Simit": kaynakNode.geometry?.firstMaterial?.diffuse.contents = UIColor.yellow case "Kutu": kaynakNode.geometry?.firstMaterial?.diffuse.contents = UIColor.red default: return } } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // Create a session configuration let configuration = ARWorldTrackingConfiguration() // Run the view's session sceneView.session.run(configuration) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) // Pause the view's session sceneView.session.pause() } }