Advertisement
Guest User

Untitled

a guest
Aug 8th, 2012
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // My method
  2. //
  3. // Things that are pre-computed:
  4. // local space edge normals and polygon projection onto edge.
  5. //
  6. // Things that are computed once per-update (on rotation change) and re-used in all
  7. // collision detection that occurs.
  8. //
  9. // world space edge normal (a rotation with cached sin/cos)
  10. // world space projection (gprojection = dot(object.pos, normal) + lprojection)
  11. // world space vertex perpdots: proj0 = perpdot(normal, vertex0)
  12.  
  13. function circle2poly(circle, polygon) {
  14.     var max_edge = null;
  15.     var max_overlap;
  16.    
  17.     for (edge in polygon.edges) {
  18.        var axis = edge.normal;
  19.         var overlap = edge.gprojection - dot(circle.centre, edge.normal) - circle.radius;
  20.         if (overlap < 0) return no_intersection; // early exit.
  21.  
  22.         if (max_edge == null || overlap > max_overlap) {
  23.             max_overlap = overlap;
  24.             max_edge = edge;
  25.         }
  26.     }
  27.  
  28.     var proj = perpdot(max_edge.normal,circle.centre);
  29.     if (proj < max_edge.proj0) return circle2circle(circle.centre, circle.radius,     max_edge.vertex0, 0);
  30.     if (proj > max_edge.proj1) return circle2circle(circle.centre, circle.radius,     max_edge.vertex1, 0);
  31.     else return { axis : max_edge.normal, overlap : max_overlap };
  32. }
  33.  
  34. function circle2circle(p0, r0, p1, r1) {
  35.     var del = p0 - p1;
  36.     var lsq = dot(del, del);
  37.     var rsum = r0 + r1;
  38.     if (lsq > rsum*rsum) return no_intersection;
  39.  
  40.     var dist = sqrt(lsq);
  41.     return { axis : (del / dist), overlap : rsum - dist };
  42. }
  43.  
  44. // Summary:
  45. // for each edge, we compute dot product, and 2 subtraction.
  46. // can get an early exit on any edge (possibly first one) -> quick to discard seperating case.
  47. //
  48. // 1 further dot product decides if we can exit with edge-circle intersection
  49. // or need to consider one of the two vertices.
  50. //
  51. // If we consider a vertex, we have subtraction, dot product, addition, multiplication
  52. // to get an exit.
  53. //
  54. // Otherwise in worst-case scenario, a division, 2 multiplications, sqrt and subtraction.
  55. //
  56.  
  57. // Your method
  58. //
  59. // Things that can be pre-computed:
  60. // reciprocal of squared length edge (ilengthsq)
  61. //
  62. // Things computed once per update (change in rotation)
  63. // world space edge vector
  64.  
  65. function circle2poly(circle, polygon) {
  66.    var closest_axis = null;
  67.    var closest_dist;
  68.    for (edge in polygon.edges) {
  69.        var t = dot(circle.centre - edge.vertex0, edge.edgeVector) * edge.ilengthsq;
  70.        if (t < 0) t = 0; elif (t > 1) t = 1;
  71.        var axis = circle.centre - (edge.vertex0 + (edge.edgeVector * t));
  72.        var axislsq = dot(axis, axis);
  73.        if (closest_axis == null || axislsq < closest_dist) {
  74.            closest_axis = axis;
  75.            closest_dist = axislsq;
  76.        }
  77.    }
  78.  
  79.    closest_axis = normalise(closest_axis);
  80.  
  81.    var circle_proj = dot(closest_axis, circle.centre) + circle.radius;
  82.    var poly_proj = -inf;
  83.    for (vertex in polygon.vertices) {
  84.        var vproj = dot(vertex, closest_axis);
  85.        if (vproj > poly_proj) poly_proj = vproj;
  86.    }
  87.  
  88.    var overlap = poly_proj - circle_proj;
  89.    if (overlap < 0) return no_intersection;
  90.    else return { axis : closest_axis, overlap : overlap }
  91. }
  92.  
  93. // Summary:
  94. // We have NO early exits!
  95. //
  96. // For each edge we compute 2 subtractions, a dot product, and a multiplication
  97. // A further 2 subtractions, 2 additions, and 2 multiplications
  98. // A further dot product.
  99. //
  100. // for normalisation, a dot product, sqrt, division and 2 multiplications
  101. //
  102. // another dot product and an addition.
  103. // For each vertex we compute a dot product
  104. //
  105. // Then a final subtraction at which point we determine intersection, or success.
  106.  
  107. // Comparison: My method wins.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement