Difference between revisions of "User:Blubex/Snippets"

From Robowiki
Jump to navigation Jump to search
m
m (reformat, cleanup, bug fix)
 
Line 3: Line 3:
  
 
<syntaxhighlight>
 
<syntaxhighlight>
double x = getX() - getWidth() / 2;
+
double x = getX() - getWidth() / 2;
    double y = getY() - getWidth() / 2;
+
double y = getY() - getHeight() / 2;
    double a = Math.toDegrees(Math.atan(x / y));  
+
 
double length = getBattleFieldWidth() - (getBattleFieldWidth() - getX());  
+
double a = Math.toDegrees(Math.atan(x / y));  
double height = getBattleFieldHeight() - (getBattleFieldHeight() - getY());
+
turnRight(180 - getHeading() + a); // turn to face lower-left corner
double hypotenuse = Math.sqrt(length*length + height*height); //pythagorean theorem
+
 
    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.
 
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)