Guest User

Untitled

a guest
Jan 21st, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. package fch;
  2.  
  3. import robocode.Robot;
  4.  
  5. /**
  6. * A Robot object with the ability to move to a specified point.
  7. * @author Christopher Foo
  8. *
  9. */
  10. public abstract class MovingRobot extends Robot{
  11.  
  12. /**
  13. * Moves the robot to the specified point.
  14. * @param x The x-coordinate of the point to move to.
  15. * @param y The y-coordinate of the point to move to.
  16. */
  17. public void moveToPoint(double x, double y){
  18.  
  19. while(Math.round(getX()) != Math.round(x) || Math.round(getY()) != Math.round(y)){
  20.  
  21. //Account for the robot's width to prevent attempts to send the robot to an unreachable point.
  22. if(x < getWidth()/2){
  23. x = getWidth()/2;
  24. }
  25. else if(x > getBattleFieldWidth()-getWidth()/2){
  26. x = getBattleFieldWidth()-getWidth()/2;
  27. }
  28.  
  29. //Account for the robot's height to prevent attempts to sent the robot to an unreachable
  30. //point.
  31. if(y < getHeight()/2){
  32. y = getHeight()/2;
  33. }
  34. else if(y > getBattleFieldHeight()-getHeight()/2){
  35. y = getBattleFieldHeight()-getHeight()/2;
  36. }
  37.  
  38. moveToPointAttempt(x, y);
  39. }
  40. }
  41.  
  42. /**
  43. * Attempts to move the robot to the specified point.
  44. * @param x The x-coordinate of the point to move to.
  45. * @param y The y-coordinate of the point to move to.
  46. */
  47. public void moveToPointAttempt(double x, double y){
  48. double currentHeading = getHeading(); //The robot's current heading.
  49. double desiredHeading; //The heading to the point to move to.
  50. double headingDifference; //The difference between the current and the desired headings.
  51. double currentX = getX(); //The robot's current x position.
  52. double currentY = getY(); //The robot's current y position.
  53.  
  54. //Comparison of the robot's current x position to the desired x position.
  55. //>0 if current is larger
  56. // 0 if equal
  57. //<0 if smaller
  58. int compareX = ((Double) currentX).compareTo(x);
  59.  
  60. //Comparison of the robot's current y position
  61. //to the desired y position.
  62. //>0 if current is larger
  63. // 0 if equal
  64. //<0 if smaller
  65. int compareY = ((Double) currentY).compareTo(y);
  66.  
  67. if(compareY == 0){
  68.  
  69. //On Point
  70. if(compareX == 0){
  71. return;
  72. }
  73.  
  74. //East of Point
  75. else if(compareX > 0){
  76. desiredHeading = 270;
  77. }
  78.  
  79. //West of Point
  80. else{
  81. desiredHeading = 90;
  82. }
  83. }
  84. else if(compareY > 0){
  85.  
  86. //Directly North of Point
  87. if(compareX == 0){
  88. desiredHeading = 180;
  89. }
  90.  
  91. //North-East of Point
  92. else if(compareX > 0){
  93. desiredHeading = Math.toDegrees(Math.abs(Math.atan((currentX-x)/(currentY-y))))
  94. + 180;
  95. }
  96.  
  97. //North-West of Point
  98. else {
  99. desiredHeading = 180
  100. - Math.toDegrees(Math.abs(Math.atan((x-currentX)/(currentY-y))));
  101.  
  102. }
  103. }
  104.  
  105. //Else compareY must be < 0
  106. else {
  107.  
  108. //Directly South of Point
  109. if(compareX == 0){
  110. desiredHeading = 180;
  111. }
  112.  
  113. //South-East of Point
  114. else if(compareX > 0){
  115. desiredHeading = Math.toDegrees(Math.abs(Math.atan((y-currentY)/(currentX-x))))
  116. + 270;
  117. }
  118.  
  119. //South-West of Point
  120. else{
  121. desiredHeading = 90
  122. - Math.toDegrees(Math.abs(Math.atan((y-currentY)/(x-currentX))));
  123. }
  124. }
  125.  
  126. //Negative if desiredHeading is more clockwise
  127. //Positive if desiredHeading is more counter-clockwise
  128. //Absolute Value = difference in degree angles
  129. headingDifference = currentHeading - desiredHeading;
  130.  
  131. //Would be large counter-clockwise turn so make smaller clockwise turn
  132. if(headingDifference > 180){
  133. turnRight(360-headingDifference);
  134. }
  135.  
  136. //Would be large clockwise turn so make smaller counter-clockwise turn
  137. else if(headingDifference < -180){
  138. //headingDifference is negative so add instead of subtract to get turning angle
  139. turnLeft(360+headingDifference);
  140. }
  141.  
  142. else {
  143. turnLeft(headingDifference);
  144. }
  145.  
  146. ahead(Math.hypot(currentX-x, currentY-y));
  147. }
  148. }
Add Comment
Please, Sign In to add comment