DGArt Scenekit Playgrounds mini Game Tutorial 2
Create Plane and Tap Control : iPadOS
The example PolyPlane was modeled using DGArt and used in the Playground app. And the 3D scene contains a "Plane" node and a "Propeller" node.
Import the .scn file into the Playgrounds app using the following steps:
- Obtain the PolyPlane.scn file here for practicing the exercise mentioned below. Download PolyPlane.zip
- In the Playgrounds app, tap on "Insert from..." in the side menu.
- Navigate to File Locations, and select PolyPlane.scn.
- You will see PolyPlane.scn appear under Resources.
Edit the ContentView code as shown below:
// Sample provide from www.nitrio.com for DGArt iPad
import SwiftUI
import SceneKit
struct ContentView: View {
var body: some View {
VStack {
GameViewControllerRepresentable()
VStack {
Text("DGArt Mini Game | Plane")
}.padding(10)
}
}
}
struct GameViewControllerRepresentable: UIViewControllerRepresentable {
func makeUIViewController (context: Context) -> GameViewController {
GameViewController ()
}
func updateUIViewController(_ uiViewController: GameViewController, context: Context) {
}
}
Edit the GameViewController code as shown below:
// Sample provide from www.nitrio.com for DGArt iPad
import UIKit
import SceneKit
public class GameViewController: UIViewController {
private var scene: SCNScene!
private var sceneView = SCNView()
private var screenSize = CGSize(width: 500, height: 1200)
private var point: CGPoint = .zero
private var prepoint: CGPoint = .zero
private var plane:SCNNode = SCNNode()
public override func viewDidLoad() {
super.viewDidLoad ()
setup()
}
func setup () {
sceneView.frame = CGRect (origin: .zero, size: screenSize)
scene = SCNScene(named: "PolyPlane.scn")
// load aeroplane
plane = (scene?.rootNode.childNode(withName: "Plane", recursively: true))!
plane.scale = SCNVector3(x: 0.4, y: 0.4, z: 0.4)
plane.eulerAngles = SCNVector3(x: 0, y: .pi , z: 0)
plane.position = SCNVector3(x: 0, y: 0, z: 0)
// add and animate propeller
let propeller = scene?.rootNode.childNode(withName: "Propeller", recursively: true)
propeller?.runAction(SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: 0, z: 30, duration: 1)))
// Add Light
let myLight = SCNNode()
myLight.light = SCNLight()
myLight.position = SCNVector3(x: -5, y: 10, z: 10)
myLight.look(at: SCNVector3(x: 0, y: 0, z: 0))
myLight.light?.intensity = 800
myLight.light?.type = SCNLight.LightType.directional
myLight.light?.color = UIColor.white
myLight.light?.castsShadow = true
scene?.rootNode.addChildNode(myLight)
// Add Camera
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.camera?.usesOrthographicProjection = true
cameraNode.camera?.orthographicScale = 8
cameraNode.position = SCNVector3(x: 0, y: 10, z: 15)
cameraNode.look(at: SCNVector3(x: 0, y: 0, z: 0))
sceneView.scene = scene
sceneView.pointOfView = cameraNode
sceneView.autoenablesDefaultLighting = true
//sceneView.showsStatistics = true
//sceneView.allowsCameraControl = true
//sceneView.debugOptions = [.showWireframe, .showBoundingBoxes, .showPhysicsShapes]
view.addSubview(sceneView)
}
// move aeroplane to touch position
func movePlane(_ snode: SCNNode) {
snode.position = SCNVector3(x: Float(prepoint.x), y: 0, z: Float(prepoint.y))
snode.runAction(SCNAction.move(to: SCNVector3(x: Float(point.x), y: 0, z: Float(point.y)), duration: 0.3))
}
public override func touchesBegan(_ touches: Set<UITouch>, with
event: UIEvent?) {
guard let touchLocation = touches.first?.location (in: view) else { return }
prepoint = point
let pointo = touchLocation
let pointx = screenSize.width/2 - pointo.x
let pointy = screenSize.height/2 - pointo.y
point = CGPoint(x: -pointx/50.0, y: -pointy/50.0)
// move aeroplane to touch position
movePlane(plane)
}
}
Finally, you will be able to see the animate plane appear beside.
Tap on screen to navigate plane.
Alternatively, you can obtain the playground file here for exercise mentioned above. Download miniGamePlane.swiftpm.zip