From 215621f15e8ea238d8ef961a5f6d54bd93b11292 Mon Sep 17 00:00:00 2001 From: David Reid Date: Fri, 2 Apr 2021 10:06:52 +1000 Subject: [PATCH] Fix an spatialization edge case relating to listener direction. When the listener is looking at exactly the same direction as the world up vector the 3D math breaks down due to a cross product evaluating to a zero length vector. --- research/miniaudio_engine.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/research/miniaudio_engine.h b/research/miniaudio_engine.h index 3d54e5fd..d66837e1 100644 --- a/research/miniaudio_engine.h +++ b/research/miniaudio_engine.h @@ -9347,6 +9347,17 @@ MA_API ma_result ma_spatializer_process_pcm_frames(ma_spatializer* pSpatializer, */ axisZ = ma_vec3f_normalize(pListener->direction); /* Normalization required here because we can't trust the caller. */ axisX = ma_vec3f_normalize(ma_vec3f_cross(axisZ, pListener->config.worldUp)); /* Normalization required here because the world up vector may not be perpendicular with the forward vector. */ + + /* + The calculation of axisX above can result in a zero-length vector if the listener is + looking straight up on the Y axis. We'll need to fall back to a +X in this case so that + the calculations below don't fall apart. This is where a quaternion based listener and + sound orientation would come in handy. + */ + if (ma_vec3f_len2(axisX) == 0) { + axisX = ma_vec3f_init_3f(1, 0, 0); + } + axisY = ma_vec3f_cross(axisX, axisZ); /* No normalization is required here because axisX and axisZ are unit length and perpendicular. */ /*