Difference between revisions of "U"

From Robowiki
Jump to navigation Jump to search
m (fix)
m (fix)
Line 17: Line 17:
  
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
Vec dis = displacement(bot.pos(), enemy.pos());
+
Direction dir = direction(displacement(bot.pos(), enemy.pos()));
double latVel = cross(dis, enemy.vel());
+
double latVel = cross(dir, enemy.vel());
double advVel = -dot(dis, enemy.vel());
+
double advVel = -dot(dir, enemy.vel());
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 63: Line 63:
 
@Override
 
@Override
 
protected void onUpdated() {
 
protected void onUpdated() {
   Vec dis = displacement(bot.pos(), enemy.pos());
+
   Direction dir = direction(displacement(bot.pos(), enemy.pos()));
   double latVel = cross(dis, enemy.vel());
+
   double latVel = cross(dir, enemy.vel());
   double advVel = -dot(dis, enemy.vel());
+
   double advVel = -dot(dir, enemy.vel());
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 07:18, 15 August 2017

This page is dedicated for describing the xor.util.ds.U




U is a immutable universal data-structure library, where U stands for both Util and Universal.

With U, you can write code just like this:

Vec dis = displacement(bot.pos(), enemy.pos());
dis = rotate(dis, radians(PI / 2));
Point projected = project(bot.pos(), dis);

Or even this:

Direction dir = direction(displacement(bot.pos(), enemy.pos()));
double latVel = cross(dir, enemy.vel());
double advVel = -dot(dir, enemy.vel());

And if you like:

import xor.util.ds.*;
import static xor.util.ds.U.*;

class Wave {
  private final double fireTime;
  private final Point source;
  private final Direction direction;

  Wave(double fireTime, @NotNull Point source, @NotNull Direction direction) {
    this.fireTime = fireTime;
    this.source = source;
    this.direction = direction;
  }

  public @NotNull Vec displacement(double now, double speed) {
    return mul(direction, speed * (now - fireTime));
  }
}

With a immutable universal data-structure library, you can easily build data abstraction on top.

No longer messy code like this:

@Override
public final void onScannedRobot(@NotNull ScannedRobotEvent event) {
  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:

@Override
protected void onUpdated() {
  Direction dir = direction(displacement(bot.pos(), enemy.pos()));
  double latVel = cross(dir, enemy.vel());
  double advVel = -dot(dir, enemy.vel());
}

A side benefit of using U is that the need of massive trigs is automatically eliminated, as trigs are calculated only when retrieving data from robocode API.

You can write PrecisePrediction even without using trigs!

Point pos = bot.pos();
Vec vel = bot.vel();
Angle turn = bot.turnRate(); // trigs are pre-calculated. 

for (int i = 0; i < 100; ++i) {
  vel = rotate(vel, turn); // rotate is done with only "*" and "+"
  pos = project(pos, vel); // project is done with only "+"
}