geometry - Moving Circle vs Line Segment swept test, calculating normal -


i have created method calculates time of impact between moving circle , line segment:

  function dist(px, py, x1, y1, x2, y2) {         var dx = x2 - x1;         var dy = y2 - y1;          var t = ((px - x1) * dx + (py - y1) * dy) / (dx * dx + dy * dy);          if (t > 1)             t = 1;         else if (t < 0)             t = 0;          return math.sqrt(math.pow(x1 + dx * t - px, 2) + math.pow(y1 + dy * t - py, 2));   }    this.sweep = function(x1, y1, x2, y2, vx, vy) {         var d0 = dist(this.x, this.y, x1, y1, x2, y2);         var d1 = dist(this.x + vx, this.y + vy, x1, y1, x2, y2);          if (math.abs(d0) <= this.radius) return 0;          if (d0 > this.radius && d1 < this.radius) return ((d0 - this.radius) / (d0 - d1));          return 1;   } 

https://output.jsbin.com/daxafaxuse

instead of returning number, want sweep function return normalx , normaly, return {time: 0 - 1, nx: -1 - +1, ny: -1 - +1};

what efficient way aswell calculate hitnormal , not time of impact, without doing unnecessary calculations in sweep function?


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -