Difference between revisions of "User:Chase-san/MovSim"
Jump to navigation
Jump to search
m (removing a debug print) |
m (updating) |
||
| Line 5: | Line 5: | ||
<syntaxhighlight> | <syntaxhighlight> | ||
| − | |||
| − | |||
| − | |||
| − | |||
import robocode.Rules; | import robocode.Rules; | ||
import robocode.util.Utils; | import robocode.util.Utils; | ||
| − | public class | + | public final class Simulator { |
| − | |||
public static final boolean useNewDeaccelRule = true; | public static final boolean useNewDeaccelRule = true; | ||
| − | public | + | public Point2D.Double position; |
| − | + | public double heading; | |
| + | public double velocity; | ||
| + | public double headingDelta; | ||
| + | public double maxVelocity; | ||
| + | public double angleToTurn; | ||
| + | public int direction; | ||
| + | |||
| + | public Simulator() { | ||
| + | position = new Point2D.Double(); | ||
| + | maxVelocity = Rules.MAX_VELOCITY; | ||
| + | direction = 1; | ||
| + | } | ||
| + | |||
| + | public void setPos(double x, double y) { | ||
| + | position.x = x; | ||
| + | position.y = y; | ||
} | } | ||
| − | + | ||
| − | + | public void step() { | |
| − | |||
| − | public | ||
| − | |||
| − | |||
//////////////// | //////////////// | ||
//Heading | //Heading | ||
double lastHeading = heading; | double lastHeading = heading; | ||
| − | double turnRate = Rules.getTurnRateRadians | + | double turnRate = Rules.getTurnRateRadians(Math.abs(velocity)); |
| − | + | double turn = Math.min(turnRate, Math.max(angleToTurn, -turnRate)); | |
| − | + | heading = Utils.normalNearAbsoluteAngle(heading + turn); | |
| − | heading = Utils. | + | angleToTurn -= turn; |
//////////////// | //////////////// | ||
| Line 45: | Line 51: | ||
int velDirection = (velocity > 0 ? (int)1 : (int)-1); | int velDirection = (velocity > 0 ? (int)1 : (int)-1); | ||
| + | //Handles direction zero stop | ||
if(direction == 0) { | if(direction == 0) { | ||
maxVelocity = 0; | maxVelocity = 0; | ||
| Line 50: | Line 57: | ||
} | } | ||
| + | //Handles speedup from zero | ||
if(absVelocity < 0.000001) { | if(absVelocity < 0.000001) { | ||
velDirection = direction; | velDirection = direction; | ||
| Line 91: | Line 99: | ||
//////////////// | //////////////// | ||
//Position | //Position | ||
| − | x += | + | position.x += Math.sin(heading) * velocity; |
| − | y += | + | position.y += Math.cos(heading) * velocity; |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
} | } | ||
| + | |||
| + | headingDelta = Utils.normalRelativeAngle(heading - lastHeading); | ||
} | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 09:42, 27 July 2010
This is the move simulator I created, for use in Precise Prediction, it was influenced by Albert's Precise Prediction code. Unlike his it doesn't handle distance, but that is a very complicated set of code (which I might add later if there is a need).
This and all my other code in which I display on the robowiki falls under the ZLIB License.
import robocode.Rules;
import robocode.util.Utils;
public final class Simulator {
public static final boolean useNewDeaccelRule = true;
public Point2D.Double position;
public double heading;
public double velocity;
public double headingDelta;
public double maxVelocity;
public double angleToTurn;
public int direction;
public Simulator() {
position = new Point2D.Double();
maxVelocity = Rules.MAX_VELOCITY;
direction = 1;
}
public void setPos(double x, double y) {
position.x = x;
position.y = y;
}
public void step() {
////////////////
//Heading
double lastHeading = heading;
double turnRate = Rules.getTurnRateRadians(Math.abs(velocity));
double turn = Math.min(turnRate, Math.max(angleToTurn, -turnRate));
heading = Utils.normalNearAbsoluteAngle(heading + turn);
angleToTurn -= turn;
////////////////
//Movement
if(direction != 0 || velocity != 0.0) {
////////////////
//Acceleration
double acceleration = 0;
double absVelocity = Math.abs(velocity);
maxVelocity = Math.abs(maxVelocity);
//Stop and ask for directions
int velDirection = (velocity > 0 ? (int)1 : (int)-1);
//Handles direction zero stop
if(direction == 0) {
maxVelocity = 0;
direction = velDirection;
}
//Handles speedup from zero
if(absVelocity < 0.000001) {
velDirection = direction;
}
//Check directions
if(velDirection == direction) {
if(absVelocity <= maxVelocity) {
//We are speeding up
acceleration = Math.min(Rules.ACCELERATION, maxVelocity - absVelocity);
} else {
//We are slowing down in the same direction
if(absVelocity > maxVelocity)
acceleration = Math.max(-Rules.DECELERATION, maxVelocity - absVelocity);
}
} else {
//If this is the case then we are always slowing down
if(absVelocity < Rules.DECELERATION) {
//Do we pass over zero, special rules are here for this
double beyondZero = Math.abs(absVelocity - Rules.DECELERATION);
if(useNewDeaccelRule) {
acceleration = absVelocity + (beyondZero /= 2.0);
} else {
acceleration = Rules.DECELERATION;
}
//Limit our acceleration so it does not go beyond max when passing over zero
if(beyondZero > maxVelocity)
acceleration = absVelocity + maxVelocity;
} else {
//Otherwise
acceleration = Rules.DECELERATION;
}
}
acceleration *= direction;
////////////////
//Velocity
velocity = Math.min(Math.max(-Rules.MAX_VELOCITY, velocity + acceleration), Rules.MAX_VELOCITY);
////////////////
//Position
position.x += Math.sin(heading) * velocity;
position.y += Math.cos(heading) * velocity;
}
headingDelta = Utils.normalRelativeAngle(heading - lastHeading);
}
}