Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. vec2 Min(vec2 a, vec2 b) {
  2. vec2 r;
  3. r.x = Min(a.x, b.x);
  4. r.y = Min(a.y, b.y);
  5. return r;
  6. }
  7.  
  8. vec2 Max(vec2 a, vec2 b) {
  9. vec2 r;
  10. r.x = Max(a.x, b.x);
  11. r.y = Max(a.y, b.y);
  12. return r;
  13. }
  14.  
  15.  
  16. int32 Sign(real32 a) {
  17. if (a < 0) { return -1; }
  18. else if (a > 0) { return 1; }
  19. else { return 0; }
  20. }
  21.  
  22.  
  23. // This function takes a pointer to a vec2. That means when we change the values of dir, we are
  24. // changing the values at the memory address we passed in!
  25. // We WANT the value of dir to change based on the computation of this function. IMPORTANT
  26. // This uses the SeparatingAxisTheorem
  27. // The dir value will tell us which direction to move to push rectangle A out of rectangle B
  28. bool RectTest(Rect a, Rect b, vec2 aPosition, vec2 bPosition, vec2 *dir) {
  29.  
  30. Rect aGlobal;
  31. aGlobal.min = Add(a.min, aPosition);
  32. aGlobal.max = Add(a.max, aPosition);
  33.  
  34. Rect bGlobal;
  35. bGlobal.min = Add(b.min, bPosition);
  36. bGlobal.max = Add(b.max, bPosition);
  37.  
  38. r32 lengthX = Min(aGlobal.max.x, bGlobal.max.x) - Max(aGlobal.min.x, bGlobal.min.x);
  39. r32 lengthY = Min(aGlobal.max.y, bGlobal.max.y) - Max(aGlobal.min.y, bGlobal.min.y);
  40.  
  41. // This tells us if there is separation on either axis
  42. if (lengthX < 0) { return false; }
  43. if (lengthY < 0) { return false; }
  44. // If we get here there is no separation, and we want to find the axis with the least length
  45.  
  46. r32 posDiffX = bPosition.x - aPosition.x;
  47. r32 posDiffY = bPosition.y - aPosition.y;
  48.  
  49. if (lengthX < lengthY) {
  50. dir->x = lengthX * -Sign(posDiffX);
  51. }
  52. else {
  53. dir->y = lengthY * -Sign(posDiffY);
  54. }
  55.  
  56. return true;
  57. }
  58.  
  59. / *
  60. USAGE CODE FOR RectTest()
  61.  
  62. // @NOTE: this is the rectangle defined RELATIVE to the position of our guy
  63. // We can use these relative min/max to compute the global min and max points using them
  64. // and position
  65. vec2 dir;
  66. bool collides = RectTest(gameMem->guy1.rect, gameMem->guy2.rect,
  67. gameMem->guy1.pos, gameMem->guy2.pos, &dir);
  68.  
  69. // @TODO: draw guy2 with a different color, if it's colldiing with guy1
  70. uint8 guy2Color = 128;
  71.  
  72. // The dir variable tells us which direction to move to push the first rectangle out of the second.
  73. // so if we want to push guy2 away from guy1 we would subtract the direction, if we want to push guy1
  74. // away from guy2 we'd Add the direction to the position.
  75. if (collides) {
  76. //gameMem->guy1.pos = Add(gameMem->guy1.pos, dir);
  77. gameMem->guy2.pos = Sub(gameMem->guy2.pos, dir);
  78. }
  79. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement