Guest User

Untitled

a guest
Jul 19th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. bool collides(unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2,
  2. unsigned short left, unsigned short top, unsigned short right, unsigned short bottom,
  3. float &averageX, float &averageY) {
  4. //Vertical and horizontal lines handling
  5. if (x1 == x2) {
  6. if (x1 >= left && x1 <= right) {
  7. bool y1in = y1 >= top && y1 <= bottom;
  8. bool y2in = y2 >= top && y2 <= bottom;
  9.  
  10. if (y1in && !y2in) {
  11. averageX = x1;
  12. float secondY = y1 < y2 ? (bottom) : (top);
  13. averageY = (secondY + y1) / 2;
  14. return true;
  15. } else if (!y1in && y2in) {
  16. averageX = x1;
  17. float secondY = y2 < y1 ? (bottom) : (top);
  18. averageY = (secondY + y2) / 2;
  19. return true;
  20. } else if (y1in && y2in) {
  21. averageX = x1;
  22. averageY = (y1 + y2) / 2;
  23. return true;
  24. } else {
  25. if (y1 < y2) {
  26. if (y1 < top && y2 > bottom) {
  27. averageX = x1;
  28. averageY = (bottom + top) / 2;
  29. return true;
  30. }
  31. } else {
  32. if (y2 < top && y2 > bottom) {
  33. averageX = x1;
  34. averageY = (bottom + top) / 2;
  35. }
  36. }
  37. }
  38. return false;
  39. }
  40. return false;
  41. } else if (y1 == y2) {
  42. if (y1 >= top && y1 <= bottom) {
  43. bool x1in = x1 >= left && x1 <= right;
  44. bool x2in = x2 >= left && x2 <= right;
  45.  
  46. if (x1in && !x2in) {
  47. averageY = y1;
  48. float secondX = x1 < x2 ? (right) : (left);
  49. averageX = (secondX + x1) / 2;
  50. return true;
  51. } else if (!x1in && x2in) {
  52. averageY = y1;
  53. float secondX = x2 < x1 ? (right) : (left);
  54. averageX = (secondX + x2) / 2;
  55. return true;
  56. } else if (x1in && x2in) {
  57. averageY = y1;
  58. averageX = (x1 + x2) / 2;
  59. return true;
  60. } else {
  61. if (x1 < x2) {
  62. if (x1 < left && x2 > right) {
  63. averageY = y1;
  64. averageX = (left + right) / 2;
  65. return true;
  66. }
  67. } else {
  68. if (x2 < left && x1 > right) {
  69. averageY = y1;
  70. averageX = (left + right) / 2;
  71. }
  72. }
  73. }
  74. return false;
  75. }
  76. return false;
  77. }
  78.  
  79. //Start of Liang-Barsky
  80. float p2 = (x2 - x1);
  81. float p1 = -p2;
  82. float p4 = (y2 - y1);
  83. float p3 = -p4;
  84.  
  85. float q1 = x1 - left;
  86. float q2 = right - x1;
  87. float q3 = y1 - top;
  88. float q4 = bottom - y1;
  89.  
  90. float max = 0.0f;
  91. float min = 1.0f;
  92.  
  93. float r1 = q1 / p1;
  94. float r2 = q2 / p2;
  95. if (p1 < 0) {
  96. if (r1 > max) {
  97. max = r1;
  98. }
  99. if (r2 < min) {
  100. min = r2;
  101. }
  102. } else {
  103. if (r2 > max) {
  104. max = r2;
  105. }
  106. if (r1 < min) {
  107. min = r1;
  108. }
  109. }
  110.  
  111. float r3 = q3 / p3;
  112. float r4 = q4 / p4;
  113. if (p3 < 0) {
  114. if (r3 > max) {
  115. max = r3;
  116. }
  117. if (r4 < min) {
  118. min = r4;
  119. }
  120. } else {
  121. if (r4 > max) {
  122. max = r4;
  123. }
  124. if (r3 < min) {
  125. min = r3;
  126. }
  127. }
  128.  
  129. if (max > min) {
  130. return false;
  131. }
  132.  
  133. float ax = ((x1 + p2 * max) + (x1 + p2 * min)) / 2;
  134. float ay = ((y1 + p4 * max) + (y1 + p4 * min)) / 2;
  135. averageX = ax;
  136. averageY = ay;
  137.  
  138. return true;
  139. }
  140.  
  141. //oldX and oldY is a coordinates before move
  142. if (oldX != this->x || oldY != this->y) {
  143. PhysicsLine* lines = world->getLines();
  144. unsigned int i;
  145. unsigned int count = world->getLinesCount();
  146. unsigned short left = this->x - this->halfWidth;
  147. unsigned short right = this->x + this->halfWidth;
  148. unsigned short top = this->y - this->halfHeight;
  149. unsigned short bottom = this->y;
  150. float averageX, averageY;
  151. float dx = this->x - oldX;
  152. float dy = this->y - oldY;
  153. float* vectorsX = new float[count];
  154. float* vectorsY = new float[count];
  155. unsigned int vectorsN = 0;
  156.  
  157. //check collisions
  158. for (i = 0; i < count; i++) {
  159. PhysicsLine line = lines[i];
  160. if (collides(
  161. line.x1, line.y1,
  162. line.x2, line.y2,
  163. left, top, right, bottom,
  164. averageX, averageY //response from collision function
  165. )) {
  166. vectorsX[vectorsN] = this->x - averageX;
  167. vectorsY[vectorsN] = this->y - averageY;
  168. vectorsN++;
  169. }
  170. }
  171. //apply vectors
  172. for (i = 0; i < vectorsN; i++) {
  173. //normalize vector
  174. float vx = vectorsX[i];
  175. float vy = vectorsY[i];
  176. float len = sqrt(vx * vx + vy * vy);
  177. vx /= len;
  178. vy /= len;
  179.  
  180. dx += vx * this->speed / vectorsN;
  181. dy += vy * this->speed / vectorsN;
  182. }
  183. delete[] vectorsX;
  184. delete[] vectorsY;
  185.  
  186. //move player to result velocity vector
  187. this->x = (unsigned short) (oldX + dx);
  188. this->y = (unsigned short) (oldY + dy);
  189. }
Add Comment
Please, Sign In to add comment