Skip to content

Commit dd42cc0

Browse files
seemkkripken
authored andcommitted
Avoid division by zero when using relative OpenAL sources. (#6887)
1 parent be5f44a commit dd42cc0

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,4 @@ a license to everyone to use it as detailed in LICENSE.)
352352
* Ashleigh Thomas <[email protected]>
353353
* Veniamin Petrenko <[email protected]>
354354
* Ian Henderson <[email protected]>
355+
* Siim Kallas <[email protected]>

src/library_openal.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,14 +539,24 @@ var LibraryOpenAL = {
539539
var lUpY = listener.up[1];
540540
var lUpZ = listener.up[2];
541541

542+
function inverseMagnitude(x, y, z) {
543+
var length = Math.sqrt(x * x + y * y + z * z);
544+
545+
if (length < Number.EPSILON) {
546+
return 0.0;
547+
}
548+
549+
return 1.0 / length;
550+
}
551+
542552
// Normalize the Back vector
543-
var invMag = 1.0 / Math.sqrt(lBackX * lBackX + lBackY * lBackY + lBackZ * lBackZ);
553+
var invMag = inverseMagnitude(lBackX, lBackY, lBackZ);
544554
lBackX *= invMag;
545555
lBackY *= invMag;
546556
lBackZ *= invMag;
547557

548558
// ...and the Up vector
549-
var invMag = 1.0 / Math.sqrt(lUpX * lUpX + lUpY * lUpY + lUpZ * lUpZ);
559+
var invMag = inverseMagnitude(lUpX, lUpY, lUpZ);
550560
lUpX *= invMag;
551561
lUpY *= invMag;
552562
lUpZ *= invMag;
@@ -557,7 +567,7 @@ var LibraryOpenAL = {
557567
var lRightZ = (lUpX * lBackY - lUpY * lBackX);
558568

559569
// Back and Up might not be exactly perpendicular, so the cross product also needs normalization
560-
var invMag = 1.0 / Math.sqrt(lRightX * lRightX + lRightY * lRightY + lRightZ * lRightZ);
570+
var invMag = inverseMagnitude(lRightX, lRightY, lRightZ);
561571
lRightX *= invMag;
562572
lRightY *= invMag;
563573
lRightZ *= invMag;

0 commit comments

Comments
 (0)