Difference between revisions of "User:Robar/Code snippets"

From Robowiki
Jump to navigation Jump to search
(use <syntaxhighlight>)
 
Line 7: Line 7:
 
== Quick Linear Targeting with Angular Velocity ==
 
== Quick Linear Targeting with Angular Velocity ==
 
:This works quite well, but it's not smaller and a bit less accurate than the popular latvel-type. Otherwise, this shows very spectaculary what exactly [[Angular Velocity]] is.
 
:This works quite well, but it's not smaller and a bit less accurate than the popular latvel-type. Otherwise, this shows very spectaculary what exactly [[Angular Velocity]] is.
<pre>
+
<syntaxhighlight>
 
public double do_Angular_Velocity_Gun(ScannedRobotEvent e){
 
public double do_Angular_Velocity_Gun(ScannedRobotEvent e){
 
             double av = (e.getVelocity()*Math.sin(e.getHeadingRadians()-absB))/e.getDistance();
 
             double av = (e.getVelocity()*Math.sin(e.getHeadingRadians()-absB))/e.getDistance();
 
             return hot+av*(e.getDistance()/bulletVel);
 
             return hot+av*(e.getDistance()/bulletVel);
 
     }
 
     }
</pre>
+
</syntaxhighlight>
  
 
== Quick Linear Targeting with Maximum Escape Angle ==
 
== Quick Linear Targeting with Maximum Escape Angle ==
 
:This one is even less accurate but an interesting use of [[Maximum Escape Angle]]. ;)
 
:This one is even less accurate but an interesting use of [[Maximum Escape Angle]]. ;)
<pre>
+
<syntaxhighlight>
 
  public double do_MaximumEscapeAngle_gun(ScannedRobotEvent e){
 
  public double do_MaximumEscapeAngle_gun(ScannedRobotEvent e){
 
         double lateralDir = Math.signum(e.getVelocity()*Math.sin(e.getHeadingRadians()-absB));
 
         double lateralDir = Math.signum(e.getVelocity()*Math.sin(e.getHeadingRadians()-absB));
Line 22: Line 22:
 
         return hot + mea*lateralDir;
 
         return hot + mea*lateralDir;
 
         }
 
         }
</pre>
+
</syntaxhighlight>

Latest revision as of 07:20, 1 July 2010

My code snippets

During my researches in Robocode I created many educating and interesting codes, which I'd like to share with you.
Many places I use variables called 'hot' and 'absB'. They are exactly what their names mean: Head-on angle and Absolute Bearing, both in radians.
The codes are not squeezed, they are supposed to be readable and obvious.

Quick Linear Targeting with Angular Velocity

This works quite well, but it's not smaller and a bit less accurate than the popular latvel-type. Otherwise, this shows very spectaculary what exactly Angular Velocity is.
public double do_Angular_Velocity_Gun(ScannedRobotEvent e){
            double av = (e.getVelocity()*Math.sin(e.getHeadingRadians()-absB))/e.getDistance();
            return hot+av*(e.getDistance()/bulletVel);
     }

Quick Linear Targeting with Maximum Escape Angle

This one is even less accurate but an interesting use of Maximum Escape Angle. ;)
 public double do_MaximumEscapeAngle_gun(ScannedRobotEvent e){
        	double lateralDir = Math.signum(e.getVelocity()*Math.sin(e.getHeadingRadians()-absB));
        	double mea = Math.asin(8.0/bulletVel);
        	return hot + mea*lateralDir;
        }