我正在尝试旋转我的对象(SCNNode)以面对它正在移动的点。我只穿过x和y轴,到目前为止我试过了:
// Action to move node
let action1 = SCNAction.moveTo(SCNVector3(x: 4.0, y: 0.0, z: 6.0), duration: 3)
// Get the tan angle:
let angle = CGFloat(atan2(z, x))
if angle - previousAngle > Pi {
playerAngle += 2 * Pi
} else if previousAngle - angle > Pi {
playerAngle -= 2 * Pi
}
previousAngle = angle
playerAngle = angle * RotationBlendFactor + playerAngle * (1 - RotationBlendFactor)
let rot = playerAngle - 90 * DegreesToRadians
flyNode.rotation = SCNVector4(x: 0, y: 1, z: 0, w: Float(rot))
它适用于某些目标点,但不适用于所有目标点。
我也尝试添加约束数组SCNLookAtConstraint,它完美地旋转我的节点,但它停止我移动到动画:
let targerNode = SCNNode()
targerNode.position = SCNVector3(x: -4.0, y: 0.0, z: -2.0)
let con = SCNLookAtConstraint(target: targerNode)
flyNode.constraints = [con]
let action1 = SCNAction.moveTo(SCNVector3(x: 4.0, y: 0.0, z: -2.0), duration: 3)
你能帮我解决这个问题吗?
答案 0 :(得分:0)
最简单的解决方案(尽管可能不是最优雅的)是将moveTo
SCNAction
应用于节点,并将SCNLookAtConstraint
应用于该节点的子节点。这应该使两者都能在没有冲突的情况下运作。
答案 1 :(得分:0)
尝试使用:(它是客观的C,但它应该很容易转换)
float x, z;// these are your vars you assign these so you can delete this line
float offset = 0; // use this to offset your final output
// because your output might be off by
// a quarter of a turn or so, or more
// but once you got the offset right
// than it will stay right
float aQuarterOfAturn = 1.570795;// since scenekit uses radians
// this needs to be pi/2
// if it was degrees it would be 90
if (x < 0) {
offset += aQuarterOfAturn;// turn it to compensate for the lack of negetive x
x *= -1;// convert x to positive so we can calculate it
}
if (z < 0) {
offset += aQuarterOfAturn;// turn it to compensate for the lack of negetive z
z *= -1;// convert z to positive so we can calculate it
}
xzDelta = x + z// add x and z
zPercent = z * 100 / xzDelta // so we can calculate how much percent z is
yRot = aQuarterOfAturn * zPercent / 100 // and finally convert it to a rotation
yRot += offset // add the offset and you can implement the rotation now
// a note, this code has not been tested you may need to offset it (above)
// and possibly multiply it by negitive 1
// if it works fine but the rotation is off by however many degrees than just offset it
// if it works fine but your move up and it rotates down than multiply it by -1
让我知道它是如何运作的!