# slerp issues #29

Closed
opened this Issue Mar 1, 2014 · 2 comments

Projects
None yet
2 participants

### mbj2011 commented Mar 1, 2014

 I have absolutely no idea why this is happening, but apprantly there are certain magical quaternions that cannot be interpolated between. ``` quat a = quat.yrotation(-1.238796f); quat b = quat.yrotation(-1.238796f); quat bad = slerp(a, b, 0.5); quat a2 = quat.yrotation(-1.23879f); quat b2 = quat.yrotation(-1.23879f); quat good = slerp(a2, b2, 0.5); writeln(a, " ", b, " ", bad, " ", good, ".");``` Having done a bit of research on slerp it turns out that its "bad practice" anyway: http://physicsforgames.blogspot.com/2010/02/quaternions.html http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/ If it is not too much trouble, I'd like to see nlerp along with a fixed version of slerp in the interpolate module. My version looks like this: ```quat nlerp(quat a, quat b, float t) { float dot = a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z; quat result; if(dot < 0) { // Determine the "shortest route"... result = a - (b + a) * t; // use -b instead of b } else { result = a + (b - a) * t; } result.normalize(); return result; }``` , but I guarrantee nothing as the bug above is completely blowing my mind.
Owner

### Dav1dde commented Mar 1, 2014

 This looks another Dlang bug to me... ``` writefln("---------> %s | %s", a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z, acos(a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z)); real theta = acos(a.w * b.w + a.x * b.x + a.y * b.y + a.z * b.z); writefln("---------> %s", theta);``` produces: ```---------> 1 | -nan ---------> -nan``` Which should obviously be `0` not `-nan`. No matter what I change, the outcome doesn't change! Your version looks fine, I added it to the interpolation-module, thanks!

### Dav1dde added a commit that referenced this issue Mar 1, 2014

``` added a nlerp interpolation function #29 ```
``` 7fc066f ```

### Dav1dde added a commit that referenced this issue Mar 1, 2014

``` add workaround for issue #29 ```
``` 03e0fcd ```
Owner

### Dav1dde commented Mar 1, 2014

 Ok I think I figured it out, the result of the dot product is probably ever so slightly smaller than `-1` or bigger than `1`, making `acos` return `nan` (floating point errors). Clamping the values to `-1` and `1` seems to fix it. Please report back, if the issue still exists.