Difference between revisions of "User:Blubex/Snippets"

From Robowiki
Jump to navigation Jump to search
(If you see anything that can be fixed with this, feel free. Thanks.)
 
m (reformat, cleanup, bug fix)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 +
== Some bits of code I've put together and posted ==
 +
[[Category:Code Snippets]]
  
== Some bits of code I've put together and posted ==
+
<syntaxhighlight>
 +
double x = getX() - getWidth() / 2;
 +
double y = getY() - getHeight() / 2;
  
 +
double a = Math.toDegrees(Math.atan(x / y));
 +
turnRight(180 - getHeading() + a); // turn to face lower-left corner
  
<syntaxhighlight>
+
// can be replaced by Point2D.distance(0, 0, x, y);
double x = getX() - getWidth() / 2;
+
double length = getX();  
    double y = getY() - getWidth() / 2;
+
double height = getY();
    double a = Math.toDegrees(Math.atan(x / y));  
+
double hypotenuse = Math.sqrt(length*length + height*height); //pythagorean theorem
double length = getBattleFieldWidth() - (getBattleFieldWidth() - getX());  
 
double height = getBattleFieldHeight() - (getBattleFieldHeight() - getY());
 
double hypotenuse = Math.sqrt(length*length + height*height); //pythagorean theorem
 
    turnRight(180 - getHeading() + a); // turn to face lower-left corner
 
 
ahead(hypotenuse - 40); //ahead -almost- to the corner. change the 40 to different numbers if you want.
 
ahead(hypotenuse - 40); //ahead -almost- to the corner. change the 40 to different numbers if you want.
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 13:06, 22 December 2011

Some bits of code I've put together and posted

double x = getX() - getWidth() / 2;
double y = getY() - getHeight() / 2;

double a = Math.toDegrees(Math.atan(x / y)); 
turnRight(180 - getHeading() + a); // turn to face lower-left corner

// can be replaced by Point2D.distance(0, 0, x, y);
double length = getX(); 
double height = getY();
double hypotenuse = Math.sqrt(length*length + height*height); //pythagorean theorem
ahead(hypotenuse - 40); //ahead -almost- to the corner. change the 40 to different numbers if you want.

This code sends a robot directly to the lower-left corner. How it works is that length and height are calculated and a hypotenuse is gathered. Then it turns current bearing - angle to lower-left corner. (If anyone can explain this better, please do. It's hard for me to understand, so I'm not very good at explaining this.) If you would like to use this code, put it in your run() method. There are a lot of ways you could advance this code for your robot. Some enhancements would be to make the robot choose a corner, such as sample.Corners does. You could also make something to turn a little while moving to the corner, to have more bullet avoidance.


--Blubex 04:59, 19 December 2011 (UTC)