Difference between revisions of "U"

From Robowiki
Jump to navigation Jump to search
m (no trig is even faster than fast trig!)
m (fix)
Line 12: Line 12:
  
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
  double absoluteBearing = this.getHeadingRadians() + event.getBearingRadians();
+
double absoluteBearing = this.getHeadingRadians() + event.getBearingRadians();
  double latVel = Math.sin(event.getHeadingRadians() - absoluteBearing) * event.getVelocity();
+
double latVel = Math.sin(event.getHeadingRadians() - absoluteBearing) * event.getVelocity();
  double advVel = -Math.cos(event.getHeadingRadians() - absoluteBearing) * event.getVelocity();
+
double advVel = -Math.cos(event.getHeadingRadians() - absoluteBearing) * event.getVelocity();
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 20: Line 20:
  
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
  V direct = displacement(robot.pos(), enemy.pos());
+
V direction = unit(displacement(robot.pos(), enemy.pos()));
  double latVel = cross(direct, enemy.vel());
+
double latVel = cross(direction, enemy.vel());
  double advVel = -dot(direct, enemy.vel());
+
double advVel = -dot(direction, enemy.vel());
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
the second one is also much faster because it involves no trig.
 
the second one is also much faster because it involves no trig.

Revision as of 08:09, 8 August 2018

This article is a stub. You can help RoboWiki by expanding it.

This page is dedicated for describing the aaa.util.math.U




U is a universal math library, where U stands for Universal.

The propose of U is to avoid code like this, which coupled business logic and math logic

double absoluteBearing = this.getHeadingRadians() + event.getBearingRadians();
double latVel = Math.sin(event.getHeadingRadians() - absoluteBearing) * event.getVelocity();
double advVel = -Math.cos(event.getHeadingRadians() - absoluteBearing) * event.getVelocity();

write like this:

V direction = unit(displacement(robot.pos(), enemy.pos()));
double latVel = cross(direction, enemy.vel());
double advVel = -dot(direction, enemy.vel());

the second one is also much faster because it involves no trig.