gpt4 book ai didi

ios - Scenekit 中的节点旋转

转载 作者:行者123 更新时间:2023-11-30 12:36:52 25 4
gpt4 key购买 nike

我无法弄清楚如何将我的播放器节点旋转到每次点击。我已经获得了 x-z 平面上单击的截距,并将节点设置为移动到该平面,并且我已经找到了与角色移动的 x 和 z 平面相比的单击弧度。在角色不在 0 y 旋转之后,我的方法不起作用。我猜有一种更简单的方法可以做到这一点,只是希望有人能给我指出一些文档,或者告诉我如何找出点击角度和玩家当前方向之间的差异,并使他面向运动。

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

if game.state == .tapToPlay {
startGame()
}

if game.state == .playing {

let touch = touches.first!
let location = touch.location(in: scnView)
let hitResults = scnView.hitTest(location, options: nil)

if hitResults.first?.node.name == "Grass" {
//pigNode.runAction(SCNAction.move(to: (hitResults.first?.localCoordinates)!, duration: 1.0))
let moveAction = SCNAction.move(to: (hitResults.first?.localCoordinates)!, duration: 1.0)
let location = hitResults.first?.localCoordinates

/*
pigNode.runAction(SCNAction.rotateTo(x: 0.0,
y: CGFloat(offset.y),
z: 0.0, duration: 1.0, usesShortestUnitArc: true))

let rotateAction = SCNAction.rotateTo(x: CGFloat((locationOnPlane?.x)!),
y: CGFloat((locationOnPlane?.y)!),
z: CGFloat((locationOnPlane?.z)!),
duration: 1.0, usesShortestUnitArc: true)
*/
let locationOnPlane = CGPoint(x: Double((location?.x)!), y: Double((location?.z)!))
let offset = CGPoint(x: Double(locationOnPlane.x) - Double(pigNode.position.x), y: Double(locationOnPlane.y) - Double(pigNode.position.z))
let length = sqrt(offset.x * offset.x + offset.y * offset.y)
let direction = CGPoint(x: offset.x / length, y: offset.y / length)

let rotationAngle = CGFloat(atan2(direction.y, direction.x))

let rotateAction = SCNAction.rotate(by: rotationAngle, around: SCNVector3(x: 0.0, y: 1.0, z: 0.0), duration: 1.0)
//let rotateAction = SCNAction.rotateBy(x: 0.0, y: rotationAngle, z: 0.0, duration: 1.0)
//let rotateAction = SCNAction.rotateTo(x: 0.0, y: rotationAngle, z: 0.0, duration: 1.0, usesShortestUnitArc: true)

print(rotationAngle)
print(pigNode.eulerAngles.y)



let groupAction = SCNAction.group([moveAction, rotateAction])
pigNode.runAction(groupAction)
}
}
}

最佳答案

检查这个答案Rotate a sprite to sprite position not exact in SpriteKit with Swift这是旋转的代码

let angle = atan2(location.y - cannon.position.y , location.x - cannon.position.x)
cannon.zRotation = angle - CGFloat(M_PI_2)

希望对你有帮助

关于ios - Scenekit 中的节点旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42755684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com