I have two quaternions:
SCNVector4(x: -0.554488897, y: -0.602368534, z: 0.57419008, w: 2.0878818)
SCNVector4(x: 0.55016619, y: 0.604441643, z: -0.576166153, w: 4.18851328)
and if we create two objects the orientation will be quite similar
but if we try to Lerp from first to second then the position changing quite weird (and looking on the values it's expected but not correct)
[Lerp progress demo][1]
I've googled and found many functions to do lerp e.g. simple one:
extension SCNVector4 {
func lerp(to: SCNVector4, v: Float) -> SCNVector4 {
let aX = x + (to.x - x) * v
let aY = y + (to.y - y) * v
let aZ = z + (to.z - z) * v
let aW = w + (to.w - w) * v
return SCNVector4Make(aX, aY, aZ, aW)
}
}
But how to avoid such weird flipping?
PS: I've tried different functions from GLKit but the result is the same [1]: https://i.stack.imgur.com/8jEvm.png
- As suggested tried to flip sign but the issue is that I get dot product greater then 0
extension SCNVector4 {
func glk() -> GLKQuaternion {
return GLKQuaternion(q: (x, y, z, w))
}
func lerp(to: SCNVector4, v: Float) -> SCNVector4 {
let a = GLKQuaternionNormalize(glk())
let b = GLKQuaternionNormalize(to.glk())
let dot =
a.x * b.x +
a.y * b.y +
a.z * b.z +
a.w * b.w
var target = b
if dot < 0 {
target = GLKQuaternionInvert(b)
}
let norm = GLKQuaternionNormalize(GLKQuaternionSlerp(a, target, v))
return norm.scn()
}
}
extension GLKQuaternion {
func scn() -> SCNVector4 {
return SCNVector4Make(x, y, z, w)
}
}