Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7. using Engine;
  8.  
  9. namespace Collision
  10. {
  11. class Level : State
  12. {
  13. public override void Create()
  14. {
  15. base.Create(); //call the base create function
  16.  
  17. //create a new small ball and set its position
  18. GameObject sCircle = new SmallBall();
  19. sCircle.Position.X = -200;
  20. ObjectManager.AddGameObject(sCircle);
  21.  
  22. //Create the large circle, set it to the right of the screen
  23. GameObject Rect2 = new GameObject("Rect2", 64, 128, "RectVertical.png");
  24.  
  25. Rect2.Position.X = 200;
  26.  
  27. ObjectManager.AddGameObject(Rect2);
  28. }
  29.  
  30. bool CheckCircleCircleCollision(PointF c1Position, float c1Radius,
  31. PointF c2Position, float c2Radius)
  32. {
  33. //Calculate the squared sum of radius1 and radius2
  34. float radiiSquared = (c1Radius + c2Radius) * (c1Radius + c2Radius);
  35. //calculate the change in x by subtracting the x positions of the circles
  36. float deltaX = c1Position.X - c2Position.X;
  37. //calculate the change in y by subtracting the y positions of the circles
  38. float deltaY = c1Position.Y - c2Position.Y;
  39. //calculate the distance squared using pythagoreans theroem (x^2 + y^2 = c^2)
  40. float distanceSqaured = deltaX * deltaX + deltaY * deltaY;
  41. //If the distance squared is <= radii squared
  42. if (distanceSqaured <= radiiSquared)
  43. {
  44. //colliding, return true
  45. return true;
  46. }
  47.  
  48. //otherwise, return false.
  49. return false;
  50. }
  51.  
  52. bool CheckRectangleRectangleCollision(PointF r1Position, float r1HalfWidth, float r1HalfHeight,
  53. PointF r2Position, float r2HalfWidth, float r2HalfHeight)
  54. {
  55. //Left: Check if r1's right side is further left than r2's left side
  56. if (r1Position.X + r1HalfWidth < r2Position.X - r2HalfWidth)
  57. {
  58. //if it is, there can bo no collision, so return false.
  59. return false;
  60. }
  61.  
  62. //Right: check if r1's left side is further right than r2's right side
  63. if (r1Position.X - r1HalfWidth > r2Position.X + r1HalfWidth)
  64. {
  65. //if it is, there can be no collision, so return false.
  66. return false;
  67. }
  68.  
  69. //Bottom: Check if r1's top side is below r2's bottom side
  70. if (r1Position.Y + r1HalfHeight < r2Position.Y - r2HalfHeight)
  71. {
  72. //if it is, there can be no collision so return false.
  73. return false;
  74. }
  75.  
  76. //Top: Check r1's bottom side is above r2's top side.
  77. if (r1Position.Y - r1HalfHeight > r2Position.Y + r2HalfHeight)
  78. {
  79. //if it is, there can be no collision so return false
  80. return false;
  81. }
  82.  
  83. //if none of those conditions are true, return true, as anything left is a collision
  84. return true;
  85. }
  86.  
  87. bool CheckPointCircleCollision(PointF pPos, PointF cPos, float cRadius)
  88. {
  89. //Calculate the squared radius.
  90. float radiusSquared = cRadius * cRadius;
  91. //subtract the X positions of the point and the circle
  92. float deltaX = cPos.X - pPos.X;
  93. //subtract the y positions of the point and circle
  94. float deltaY = cPos.Y - pPos.Y;
  95. //Calculate the squared distance
  96. float distanceSquared = deltaX * deltaX + deltaY * deltaY;
  97. //if the distance is less than the radius
  98. if (distanceSquared < radiusSquared)
  99. {
  100. //we are colliding
  101. return true;
  102. }
  103.  
  104. //if we get here, then we are not collding.
  105. return false;
  106. }
  107.  
  108. public override void Update()
  109. {
  110. base.Update();
  111. //float smallRadius = 32;
  112. //float largeRadius = 64;
  113.  
  114. //half widths and heights of first rectangle
  115. float halfWidth1 = 64;
  116. float halfHeight1 = 32;
  117. //half widths and heights of second rectangle
  118. float halfWidth2 = 32;
  119. float halfHeight2 = 64;
  120. //bool to hold collision return
  121. bool collision = false;
  122. //find rect1 and rect2 and store them in my own variables
  123. GameObject Rect1 = ObjectManager.GetObjectByName("Rect1");
  124. GameObject Rect2 = ObjectManager.GetObjectByName("Rect2");
  125.  
  126. //check for collision between circles
  127. //collision = CheckCircleCircleCollision(sCircle.Position, smallRadius, lCircle.Position, largeRadius);
  128. //check for collision between rectangles
  129. collision = CheckRectangleRectangleCollision(Rect1.Position, halfWidth1, halfHeight1, Rect2.Position, halfWidth2, halfHeight2);
  130. //check if collision is true or false
  131. if (collision)
  132. {
  133. //reset the position of the small circle
  134. Rect1.Position.X = -200;
  135. Rect1.Position.Y = 0;
  136. }
  137. }
  138.  
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement