저는 첫 번째 장면 키트 프로젝트를 작성하고 있습니다. 팬 제스처를 사용하여 내 장면에서 간단한 오브젝트를 회전하려고합니다 (오브젝트는 .dae 파일에서 가져온 간단한 L 큐브 모양 임). , 피벗 포인트가 올바르게 설정 됨).SceneKit - 방향을 기준으로 팬 제스처로 SCNNode를 회전하십시오.
여러 SO 솔루션 및 자습서를 거쳤으며 일부 코드를 조합했지만 회전이 올바르게 작동하지 않습니다. 만약 내가 반복적으로 하나의 축을 따라 객체를 회전 시키려고한다면, 그것은 올바르게 작동하지만, 다른 방향을 시도 할 때, 팬의 시작에서 객체는 그것의 초기 위치로 재설정됩니다. 때로는 회전이 무작위로 버벅 거리거나 점프하는 경우도 있습니다. 내가 조언을 해주십시오에 대한 올바른 접근 방식을 사용하면 내가 여기에 내 코드입니다 .. 확실하지 않다 : 연구와 헤드 뱅잉 벽의 몇 시간의 LOT 후
func handlePan(sender: UIPanGestureRecognizer){
// determine pan direction
let velocity: CGPoint = sender.velocity(in: sender.view!)
if self.panDirection == nil {
if velocity.x > 0 && velocity.x > abs(velocity.y) { self.panDirection = "right" }
if velocity.x < 0 && abs(velocity.x) > abs(velocity.y) { self.panDirection = "left" }
if velocity.y < 0 && abs(velocity.y) > abs(velocity.x) { self.panDirection = "up" }
if velocity.y > 0 && velocity.y > abs(velocity.x) { self.panDirection = "down" }
}
// do rotation only on selected SCNNode
if self.selectedBrickNode != nil {
// start of pan gesture
if sender.state == UIGestureRecognizerState.began{
// remember initial rotation angle
self.initRot = self.selectedBrickNode.rotation.w
}
let translation = sender.translation(in: sender.view!)
let pan_x = Float(translation.x)
let pan_y = Float(-translation.y)
// add rotation angle to initial rotation
var anglePan = self.initRot + (Float)(sqrt(pow(pan_x,2)+pow(pan_y,2)))*(Float)(Double.pi)/180.0
var rotVector = SCNVector4()
// if left/right, rotate on Y axis
rotVector.x = (self.panDirection == "left" || self.panDirection == "right") ? 0 : -pan_y
// if up/down, rotate on X axis
rotVector.y = (self.panDirection == "up" || self.panDirection == "down") ? 0 : pan_x
rotVector.z = 0
rotVector.w = anglePan
// set SCNNode's rotation
self.selectedBrickNode.rotation = rotVector
// end of pan gesture
if(sender.state == UIGestureRecognizerState.ended) {
// reset initial rotation
self.initRot = 0.0
// calculate degrees so we can snap to 90deg increments
var angle = anglePan * (Float) (180.0/Double.pi)
// snap to 90deg increments
let diff = angle.truncatingRemainder(dividingBy: 90.0)
if diff <= 45 {
angle = angle - diff
}else{
angle = (angle - diff) + 90
}
// set new rotation to snap
rotVector.w = angle * (Float)(Double.pi)/180.0
self.selectedBrickNode.rotation = rotVector
self.selectedBrickNode = nil
}
}
}