ray2.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008, Luke Benstead.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without modification,
  6. are permitted provided that the following conditions are met:
  7. Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  13. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  16. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  19. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. cc.kmRay2 = function(start, dir){
  24. this.start = start || new cc.kmVec2();
  25. this.start = start || new cc.kmVec2();
  26. };
  27. cc.kmRay2Fill = function(ray, px, py,vx,vy){
  28. ray.start.x = px;
  29. ray.start.y = py;
  30. ray.dir.x = vx;
  31. ray.dir.y = vy;
  32. };
  33. cc.kmRay2IntersectLineSegment = function(ray, p1, p2, intersection){
  34. var x1 = ray.start.x;
  35. var y1 = ray.start.y;
  36. var x2 = ray.start.x + ray.dir.x;
  37. var y2 = ray.start.y + ray.dir.y;
  38. var x3 = p1.x;
  39. var y3 = p1.y;
  40. var x4 = p2.x;
  41. var y4 = p2.y;
  42. var denom = (y4 -y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
  43. var ua, x, y;
  44. //If denom is zero, the lines are parallel
  45. if(denom > -cc.kmEpsilon && denom < cc.kmEpsilon) {
  46. return cc.KM_FALSE;
  47. }
  48. ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denom;
  49. // var ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denom;
  50. x = x1 + ua * (x2 - x1);
  51. y = y1 + ua * (y2 - y1);
  52. if(x < cc.kmMin(p1.x, p2.x) - cc.kmEpsilon ||
  53. x > cc.kmMax(p1.x, p2.x) + cc.kmEpsilon ||
  54. y < cc.kmMin(p1.y, p2.y) - cc.kmEpsilon ||
  55. y > cc.kmMax(p1.y, p2.y) + cc.kmEpsilon) {
  56. //Outside of line
  57. //printf("Outside of line, %f %f (%f %f)(%f, %f)\n", x, y, p1.x, p1.y, p2.x, p2.y);
  58. return cc.KM_FALSE;
  59. }
  60. if(x < cc.kmMin(x1, x2) - cc.kmEpsilon ||
  61. x > cc.kmMax(x1, x2) + cc.kmEpsilon ||
  62. y < cc.kmMin(y1, y2) - cc.kmEpsilon ||
  63. y > cc.kmMax(y1, y2) + cc.kmEpsilon) {
  64. //printf("Outside of ray, %f %f (%f %f)(%f, %f)\n", x, y, x1, y1, x2, y2);
  65. return cc.KM_FALSE;
  66. }
  67. intersection.x = x;
  68. intersection.y = y;
  69. return cc.KM_TRUE;
  70. };
  71. cc.calculate_line_normal = function(p1, p2, normal_out){
  72. var tmp = new cc.kmVec2();
  73. cc.kmVec2Subtract(tmp, p2, p1); //Get direction vector
  74. normal_out.x = -tmp.y;
  75. normal_out.y = tmp.x;
  76. cc.kmVec2Normalize(normal_out, normal_out);
  77. //TODO: should check that the normal is pointing out of the triangle
  78. };
  79. cc.kmRay2IntersectTriangle = function(ray, p1, p2, p3, intersection, normal_out){
  80. var intersect = new cc.kmVec2();
  81. var final_intersect = new cc.kmVec2();
  82. var normal = new cc.kmVec2();
  83. var distance = 10000.0;
  84. var intersected = cc.KM_FALSE;
  85. var tmp,this_distance;
  86. if(cc.kmRay2IntersectLineSegment(ray, p1, p2, intersect)) {
  87. tmp = new cc.kmVec2();
  88. intersected = cc.KM_TRUE;
  89. this_distance = cc.kmVec2Length(cc.kmVec2Subtract(tmp, intersect, ray.start));
  90. if(this_distance < distance) {
  91. final_intersect.x = intersect.x;
  92. final_intersect.y = intersect.y;
  93. distance = this_distance;
  94. cc.calculate_line_normal(p1, p2, normal);
  95. }
  96. }
  97. if(cc.kmRay2IntersectLineSegment(ray, p2, p3, intersect)) {
  98. tmp = new cc.kmVec2();
  99. intersected = cc.KM_TRUE;
  100. this_distance = cc.kmVec2Length(cc.kmVec2Subtract(tmp, intersect, ray.start));
  101. if(this_distance < distance) {
  102. final_intersect.x = intersect.x;
  103. final_intersect.y = intersect.y;
  104. distance = this_distance;
  105. cc.calculate_line_normal(p2, p3, normal);
  106. }
  107. }
  108. if(cc.kmRay2IntersectLineSegment(ray, p3, p1, intersect)) {
  109. tmp = new cc.kmVec2();
  110. intersected = cc.KM_TRUE;
  111. this_distance = cc.kmVec2Length(cc.kmVec2Subtract(tmp, intersect, ray.start));
  112. if(this_distance < distance) {
  113. final_intersect.x = intersect.x;
  114. final_intersect.y = intersect.y;
  115. distance = this_distance;
  116. cc.calculate_line_normal(p3, p1, normal);
  117. }
  118. }
  119. if(intersected) {
  120. intersection.x = final_intersect.x;
  121. intersection.y = final_intersect.y;
  122. if(normal_out) {
  123. normal_out.x = normal.x;
  124. normal_out.y = normal.y;
  125. }
  126. }
  127. return intersected;
  128. };
  129. cc.kmRay2IntersectCircle = function(ray, centre, radius, intersection) {
  130. cc.log("cc.kmRay2IntersectCircle() has not been implemented.");
  131. };