Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fix incorrect rounding behavior for negative numbers. thanks @guybrush!

  • Loading branch information...
commit 9413543e7a1f42182cfdbb8309f8001eede6199f 1 parent 2fec60c
Chris Dickinson authored January 20, 2013

Showing 1 changed file with 16 additions and 11 deletions. Show diff stats Hide diff stats

  1. 27  index.js
27  index.js
... ...
@@ -1,10 +1,16 @@
1  
-module.exports = function(field, tilesize, dimensions) {
  1
+module.exports = function(field, tilesize, dimensions, offset) {
2 2
   dimensions = dimensions || [ 
3 3
     Math.sqrt(field.length) >> 0
4 4
   , Math.sqrt(field.length) >> 0
5 5
   , Math.sqrt(field.length) >> 0
6 6
   ] 
7 7
 
  8
+  offset = offset || [
  9
+    0
  10
+  , 0
  11
+  , 0
  12
+  ]
  13
+
8 14
   field = typeof field === 'function' ? field : function(x, y, z) {
9 15
     return this[x + y * dimensions[1] + (z * dimensions[1] * dimensions[2])]
10 16
   }.bind(field) 
@@ -29,12 +35,12 @@ module.exports = function(field, tilesize, dimensions) {
29 35
         , posi = vec[i_axis] > 0
30 36
         , leading = box[posi ? 'max' : 'base'][i_axis] 
31 37
         , dir = posi ? 1 : -1
32  
-        , i_start = (leading / tilesize) >> 0
33  
-        , i_end = (((leading + vec[i_axis]) / tilesize) >> 0) + dir
34  
-        , j_start = (box.base[j_axis] / tilesize) >> 0
35  
-        , j_end = Math.ceil(box.max[j_axis] / tilesize) >> 0
36  
-        , k_start = (box.base[k_axis] / tilesize) >> 0
37  
-        , k_end = Math.ceil(box.max[k_axis] / tilesize) >> 0
  38
+        , i_start = Math.floor(leading / tilesize)
  39
+        , i_end = (Math.floor((leading + vec[i_axis]) / tilesize)) + dir
  40
+        , j_start = Math.floor(box.base[j_axis] / tilesize)
  41
+        , j_end = Math.ceil(box.max[j_axis] / tilesize)
  42
+        , k_start = Math.floor(box.base[k_axis] / tilesize) 
  43
+        , k_end = Math.ceil(box.max[k_axis] / tilesize)
38 44
         , done = false
39 45
         , edge_vector
40 46
         , edge
@@ -50,11 +56,11 @@ module.exports = function(field, tilesize, dimensions) {
50 56
 
51 57
       var step = 0
52 58
       for(var i = i_start; !done && i !== i_end; ++step, i += dir) {
53  
-        if(i < 0 || i >= dimensions[i_axis]) continue
  59
+        if(i < offset[i_axis] || i >= dimensions[i_axis]) continue
54 60
         for(var j = j_start; !done && j !== j_end; ++j) {
55  
-          if(j < 0 || j >= dimensions[j_axis]) continue
  61
+          if(j < offset[j_axis] || j >= dimensions[j_axis]) continue
56 62
           for(var k = k_start; k !== k_end; ++k) {
57  
-            if(k < 0 || k >= dimensions[k_axis]) continue
  63
+            if(k < offset[k_axis] || k >= dimensions[k_axis]) continue
58 64
             coords[i_axis] = i
59 65
             coords[j_axis] = j
60 66
             coords[k_axis] = k
@@ -65,7 +71,6 @@ module.exports = function(field, tilesize, dimensions) {
65 71
             edge = dir > 0 ? i * tilesize : (i + 1) * tilesize
66 72
             edge_vector = edge - leading
67 73
 
68  
-            sign = function(x) { return x ? x / Math.abs(x) : 0 }
69 74
             if(oncollision(i_axis, tile, coords, dir, edge_vector)) {
70 75
               done = true
71 76
               break

0 notes on commit 9413543

Please sign in to comment.
Something went wrong with that request. Please try again.