Character is not moving in desired scene in scenekit

柔情痞子 提交于 2019-12-24 16:22:51

问题


i want to move my character in scene kit .i have a map as at github Fpscontroller and i want my character move in this map but when ever i touch to move character it jumps and don,t know where is goes here is my code.`

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        if CGRectContainsPoint(gameView.virtualDPadBounds(), touch.locationInView(gameView)) {

            if padTouch == nil {
                padTouch = touch
                controllerStoredDirection = float2(0.0)
            }
        } else if panningTouch == nil {

            panningTouch = touches.first
        }

        if padTouch != nil && panningTouch != nil {
            break 
        }
    }
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if let touch = panningTouch {
        let displacement = (float2(touch.locationInView(view)) - float2(touch.previousLocationInView(view)))
        panCamera(displacement)
    }

    if let touch = padTouch {
        let displacement = (float2(touch.locationInView(view)) - float2(touch.previousLocationInView(view)))
        controllerStoredDirection = clamp(mix(controllerStoredDirection, displacement, t: ViewController.controllerAcceleration), min: -ViewController.controllerDirectionLimit, max: ViewController.controllerDirectionLimit)
    }
}

func commonTouchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if let touch = panningTouch {
        if touches.contains(touch) {
            panningTouch = nil
        }
    }

    if let touch = padTouch {
        if touches.contains(touch) || event?.touchesForView(view)?.contains(touch) == false {
            padTouch = nil
            controllerStoredDirection = float2(0.0)
        }
    }
}

override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
    commonTouchesEnded(touches!, withEvent: event)
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    commonTouchesEnded(touches, withEvent: event)
}

i followed apple fox example for moving with touch and in my viewcontroller

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {

    return true
}
func walkGestureRecognized(gesture: UIPanGestureRecognizer) {

    if gesture.state == UIGestureRecognizerState.Ended || gesture.state == UIGestureRecognizerState.Cancelled {
        gesture.setTranslation(CGPointZero, inView: self.view)
    }
}
func renderer(aRenderer: SCNSceneRenderer, updateAtTime time: NSTimeInterval) {

    //get walk gesture translation
    let translation = walkGesture.translationInView(self.view)

//create impulse vector for hero
    let angle = heroNode.presentationNode.rotation.w * heroNode.presentationNode.rotation.y
    var impulse = SCNVector3(x: max(-1, min(1, Float(translation.x) / 50)), y: 0, z: max(-1, min(1, Float(-translation.y) / 50)))
    impulse = SCNVector3(
        x: impulse.x * cos(angle) - impulse.z * sin(angle),
        y: 0,
        z: impulse.x * -sin(angle) - impulse.z * cos(angle)
    )
    heroNode.physicsBody?.applyForce(impulse, impulse: true)
    let scene = gameView.scene!
    let direction = characterDirection()

    let groundNode = character.walkInDirection(direction, time: time, scene: scene, groundTypeFromMaterial:groundTypeFromMaterial)

setting character position and direction....

 private func characterDirection() -> float3 {
    let controllerDirection = self.controllerDirection()
    var direction = float3(controllerDirection.x, 0.0, controllerDirection.y)

    if let pov = gameView.pointOfView {
        let p1 = pov.presentationNode.convertPosition(SCNVector3(direction), toNode: nil)
        let p0 = pov.presentationNode.convertPosition(SCNVector3Zero, toNode: nil)
        direction = float3(Float(p1.x - p0.x), 0.0, Float(p1.z - p0.z))

        if direction.x != 0.0 || direction.z != 0.0 {
            direction = normalize(direction)
        }
    }

    return direction
        }

there are actually two scenes called in view controller class one is empty scene called for mapNode as hereoNode and other is character sceneNode

let scene = SCNScene()
scene.rootNode.addChildNode(heroNode)
let personScene = SCNScene(named: "game.scnassets/person.scn")!
        personNode = personScene.rootNode.childNodeWithName("person", recursively: true)
        personNode?.position = SCNVector3(x:4.5,y:0,z:25)
        node.addChildNode(personNode)
scene.rootNode.addChildNode(character.node)

i am updating my question with some more details as you can see in this screen shot image i m attatching here enter image description here i want to move this fox in map with touch but it is not working it just jump and go out from map when i touch .....plz someone tell me where is my fault in code

来源:https://stackoverflow.com/questions/36539980/character-is-not-moving-in-desired-scene-in-scenekit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!