Home Forums iOS Development how can i make a walking man in scenekit swift?

Tagged: , ,

This topic contains 5 replies, has 2 voices, and was last updated by  David 1 year, 7 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2687 Reply

    David

    Hey i have to make a 3d game in scenekit for that i have to make a walking man which i created in blender (collada file).how i import it into scenekit with swift??should the man animated in blender or in scenekit??

    #2697 Reply

    Malek
    Keymaster

    Hi David,

    The best way is to make all animations in blender and then load your character in scenekit.

    What have you tried so far?

    • This reply was modified 1 year, 10 months ago by  Malek.
    #2703 Reply

    David

    i make an animated character in blender whenever i import my .dae file in scenekit it give me a run time error that is “unexpectedly an optional is found when unwraping a nil value” what should i have to do to avoid from that error??

    #2714 Reply

    Malek
    Keymaster

    Hi David,

    Can you post the relevant code that import the .dae in your project? or better can you make a sample project and send it over to me so I can see what’s wrong.

    • This reply was modified 1 year, 10 months ago by  Malek.
    #2733 Reply

    David

    Hi Malek here is my full project sample code.i want to move my character hero from one position to other but it is not working please check my project code and make correction where is fault . i am new in swift please help me

    `import UIKit
    import QuartzCore
    import SceneKit

    class GameViewController: UIViewController,SCNSceneRendererDelegate{

    var scene = SCNScene(named: “art.scnassets/explorer_skinned.dae”)!
    var camera: SCNNode!
    var ground: SCNNode!
    var light: SCNNode!

    override func viewDidLoad() {
    super.viewDidLoad()
    let sceneView = self.view as! SCNView
    sceneView.scene = scene
    sceneView.delegate = self
    sceneView.showsStatistics = true
    sceneView.allowsCameraControl = true
    let groundGeometry = SCNFloor()
    groundGeometry.reflectivity = 0
    let groundMaterial = SCNMaterial()
    groundMaterial.diffuse.contents = “wood.png”
    groundGeometry.materials = [groundMaterial]
    ground = SCNNode(geometry: groundGeometry)
    let camera = SCNCamera()
    camera.zFar = 10000
    self.camera = SCNNode()
    self.camera.camera = camera
    self.camera.position = SCNVector3(x: 10, y: 15, z: 20)
    let constraint = SCNLookAtConstraint(target: ground)
    constraint.gimbalLockEnabled = true
    self.camera.constraints = [constraint]

    let ambientLight = SCNLight()
    ambientLight.color = UIColor.darkGrayColor()
    ambientLight.type = SCNLightTypeAmbient
    self.camera.light = ambientLight

    let spotLight = SCNLight()
    spotLight.type = SCNLightTypeSpot
    spotLight.castsShadow = true
    spotLight.spotInnerAngle = 55.0
    spotLight.spotOuterAngle = 100.0
    spotLight.zFar = 500
    light = SCNNode()
    light.light = spotLight
    light.position = SCNVector3(x: 0, y: 25, z: 25)
    light.constraints = [constraint]
    sceneView.backgroundColor = UIColor.blackColor()
    sceneView.scene?.rootNode.addChildNode(self.camera)
    sceneView.scene?.rootNode.addChildNode(ground)
    sceneView.scene?.rootNode.addChildNode(light)

    let tapGesture = UITapGestureRecognizer(target: self, action: “handleTap:”)
    var gestureRecognizers = [AnyObject]()
    gestureRecognizers.append(tapGesture)
    if let existingGestureRecognizers = sceneView.gestureRecognizers {
    gestureRecognizers.extend(existingGestureRecognizers)
    }
    sceneView.gestureRecognizers = gestureRecognizers

    }

    }
    in my Gameview.swift file

    class GameView : SCNView {
    var camera:SCNNode!
    var light:SCNNode!
    var heroNode:SCNNode!
    var touchCount:Int?
    required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    }
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    var taps = event.allTouches()
    touchCount = taps?.count
    }
    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    touchCount = 0

    }

    func positionCameraWithHero() {
    let hero = heroNode.presentationNode()
    var heroPosition = hero.position
    let cameraDamping:Float = 0.3
    var targetPosition = SCNVector3Make(heroPosition.x, 30.0, heroPosition.z + 20.0)
    var cameraPosition = camera.position
    var cameraXPos = cameraPosition.x * (1.0 – cameraDamping) + targetPosition.x *
    cameraDamping
    var cameraYPos = cameraPosition.y * (1.0 – cameraDamping) + targetPosition.y *
    cameraDamping
    var cameraZPos = cameraPosition.z * (1.0 – cameraDamping) + targetPosition.z *
    cameraDamping
    cameraPosition = SCNVector3(x: cameraXPos, y: cameraYPos, z: cameraZPos)
    camera.position = cameraPosition
    light.position = SCNVector3(x: heroPosition.x, y: 90, z: heroPosition.z +
    40.0)
    light.rotation = SCNVector4(x: 1, y: 0, z: 0, w: Float(-M_PI/2.8))

    }

    func renderer(aRenderer: SCNSceneRenderer, didSimulatePhysicsAtTime time: NSTimeInterval) {
    let moveDistance = Float(10.0)
    let moveSpeed = NSTimeInterval(1.0)
    let currentX = heroNode.position.x
    let currentY = heroNode.position.y
    let currentZ = heroNode.position.z

    if touchCount == 1{
    let action = SCNAction.moveTo(SCNVector3Make(currentX, currentY, currentZ –
    moveDistance), duration: moveSpeed);
    heroNode.runAction(action)
    }

    else if touchCount == 2 {
    let action = SCNAction.moveTo(SCNVector3Make(currentX, currentY, currentZ +
    moveDistance), duration: moveSpeed)
    heroNode.runAction(action)
    }

    else if touchCount == 4 {
    let action = SCNAction.moveTo(SCNVector3Make(0, 0, 0), duration: moveSpeed)
    heroNode.runAction(action)
    }

    positionCameraWithHero()
    }
    }

    #2974 Reply

    David

    when getting SCNNode() from SCNScene() my scenekit camera not working with SCNNode() i have this piece of code let scene = SCNScene(named: “art.scnassets/check.dae”)!
    var nodeArray = scene.rootNode.childNodes
    scene.rootNode.addChildNode(node)
    for childNode in nodeArray {
    node.addChildNode(childNode as SCNNode)
    }
    The variable node is the all of child nodes of my collada 3D file and i want my scenekit camera should work with node but it is not working
    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    scene.rootNode.addChildNode(cameraNode)
    node.addChildNode(cameraNode)
    Can somebody help me

Viewing 6 posts - 1 through 6 (of 6 total)
Reply To: how can i make a walking man in scenekit swift?
Your information: