Difference between revisions of "User:Chase-san/MovSim"
Jump to navigation
Jump to search
m (license (these are all ZLIB btw)) |
(Fixing a lot of bugs (whoops :)) |
||
Line 5: | Line 5: | ||
<syntaxhighlight> | <syntaxhighlight> | ||
− | package chase. | + | package chase.s2.move; |
+ | |||
+ | import java.awt.geom.Point2D; | ||
import robocode.Rules; | import robocode.Rules; | ||
Line 13: | Line 15: | ||
//New DeAccel Rule, cannot go from -1 to 1, would go from -1 to 0.5 | //New DeAccel Rule, cannot go from -1 to 1, would go from -1 to 0.5 | ||
public static final boolean useNewDeaccelRule = false; | public static final boolean useNewDeaccelRule = false; | ||
+ | |||
+ | public static final MovSim2Stat step(Point2D p, double velocity, int direction, double heading, double angle) { | ||
+ | return step(p.getX(), p.getY(), velocity, Rules.MAX_VELOCITY, direction, heading, angle, 800, 600); | ||
+ | } | ||
+ | |||
//For Dead Code | //For Dead Code | ||
@SuppressWarnings("all") | @SuppressWarnings("all") | ||
Line 33: | Line 40: | ||
double acceleration = 0; | double acceleration = 0; | ||
double absVelocity = Math.abs(velocity); | double absVelocity = Math.abs(velocity); | ||
+ | |||
//Check if we are stopping! | //Check if we are stopping! | ||
if(direction == 0) { | if(direction == 0) { | ||
Line 39: | Line 47: | ||
if(absVelocity < acceleration) | if(absVelocity < acceleration) | ||
acceleration = -velocity; | acceleration = -velocity; | ||
+ | else | ||
+ | acceleration *= (velocity < 0 ? 1 : -1); | ||
} else { | } else { | ||
− | if(velocity = | + | if(absVelocity < 1e-6) { |
+ | velocity = 0; | ||
//We can only speed up from here | //We can only speed up from here | ||
acceleration = Rules.ACCELERATION; | acceleration = Rules.ACCELERATION; | ||
Line 48: | Line 59: | ||
//Check if we are speeding up | //Check if we are speeding up | ||
if(Math.signum(velocity) == direction | if(Math.signum(velocity) == direction | ||
− | && absVelocity < maxVelocity) { | + | && absVelocity <= maxVelocity) { |
acceleration = Rules.ACCELERATION; | acceleration = Rules.ACCELERATION; | ||
− | |||
− | |||
} else { | } else { | ||
//We are slowing down, this is more complicated | //We are slowing down, this is more complicated | ||
if(absVelocity < Rules.DECELERATION && useNewDeaccelRule) { | if(absVelocity < Rules.DECELERATION && useNewDeaccelRule) { | ||
double beyondZero = Math.abs(absVelocity - Rules.DECELERATION); | double beyondZero = Math.abs(absVelocity - Rules.DECELERATION); | ||
− | acceleration = | + | acceleration = absVelocity + beyondZero / 2.0; |
+ | //acceleration = ((absVelocity - beyondZero) + beyondZero / 2.0); | ||
} else { | } else { | ||
− | acceleration = | + | acceleration = Rules.DECELERATION; |
//If we are slowing down in the same direction | //If we are slowing down in the same direction | ||
− | if(Math.signum(velocity) == direction) | + | if(Math.signum(velocity) == direction) { |
− | acceleration = maxVelocity - absVelocity; | + | if(absVelocity > maxVelocity) { |
+ | acceleration = Math.max(-Rules.DECELERATION, maxVelocity - absVelocity); | ||
+ | } else { | ||
+ | //We shouldn't get here, but in case we do | ||
+ | acceleration = 0; | ||
+ | } | ||
+ | } | ||
} | } | ||
} | } | ||
− | |||
} | } | ||
+ | acceleration *= direction; | ||
} | } | ||
− | |||
//////////////// | //////////////// | ||
//Velocity | //Velocity | ||
− | velocity = Math.min(Math.max(- | + | velocity = Math.min(Math.max(-maxVelocity, velocity + acceleration), maxVelocity); |
− | + | ||
+ | System.out.printf("NV: %.4f\n", velocity); | ||
+ | |||
//////////////// | //////////////// | ||
Line 88: | Line 105: | ||
return new MovSim2Stat(x,y,velocity,heading,Utils.normalRelativeAngle(heading - lastHeading)); | return new MovSim2Stat(x,y,velocity,heading,Utils.normalRelativeAngle(heading - lastHeading)); | ||
} | } | ||
− | + | ||
− | + | public static final class MovSim2Stat { | |
− | class MovSim2Stat { | + | public double x, y; |
− | + | public double v, h, w; //w = heading change | |
− | + | public MovSim2Stat(double x, double y, double v, double h, double w) { | |
− | + | this.x = x; | |
− | + | this.y = y; | |
− | + | this.v = v; | |
− | + | this.h = h; | |
− | + | this.w = w; | |
− | + | } | |
} | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 10:35, 7 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.
package chase.s2.move;
import java.awt.geom.Point2D;
import robocode.Rules;
import robocode.util.Utils;
public class MovSim2 {
//New DeAccel Rule, cannot go from -1 to 1, would go from -1 to 0.5
public static final boolean useNewDeaccelRule = false;
public static final MovSim2Stat step(Point2D p, double velocity, int direction, double heading, double angle) {
return step(p.getX(), p.getY(), velocity, Rules.MAX_VELOCITY, direction, heading, angle, 800, 600);
}
//For Dead Code
@SuppressWarnings("all")
public static final MovSim2Stat step(double x, double y, double velocity, double maxVelocity,
int direction, double heading, double angleToTurn, double fieldW, double fieldH) {
////////////////
//Heading
double lastHeading = heading;
double turnRate = Rules.getTurnRateRadians(velocity);
if(Math.abs(angleToTurn) < turnRate) heading += angleToTurn;
else heading += turnRate * Math.signum(angleToTurn);
heading = Utils.normalAbsoluteAngle(heading);
////////////////
//Movement
if(direction != 0 || velocity != 0.0) {
////////////////
//Acceleration
double acceleration = 0;
double absVelocity = Math.abs(velocity);
//Check if we are stopping!
if(direction == 0) {
//Slow to a stop!
acceleration = Rules.DECELERATION;
if(absVelocity < acceleration)
acceleration = -velocity;
else
acceleration *= (velocity < 0 ? 1 : -1);
} else {
if(absVelocity < 1e-6) {
velocity = 0;
//We can only speed up from here
acceleration = Rules.ACCELERATION;
if(acceleration > maxVelocity)
acceleration = maxVelocity;
} else {
//Check if we are speeding up
if(Math.signum(velocity) == direction
&& absVelocity <= maxVelocity) {
acceleration = Rules.ACCELERATION;
} else {
//We are slowing down, this is more complicated
if(absVelocity < Rules.DECELERATION && useNewDeaccelRule) {
double beyondZero = Math.abs(absVelocity - Rules.DECELERATION);
acceleration = absVelocity + beyondZero / 2.0;
//acceleration = ((absVelocity - beyondZero) + beyondZero / 2.0);
} else {
acceleration = Rules.DECELERATION;
//If we are slowing down in the same direction
if(Math.signum(velocity) == direction) {
if(absVelocity > maxVelocity) {
acceleration = Math.max(-Rules.DECELERATION, maxVelocity - absVelocity);
} else {
//We shouldn't get here, but in case we do
acceleration = 0;
}
}
}
}
}
acceleration *= direction;
}
////////////////
//Velocity
velocity = Math.min(Math.max(-maxVelocity, velocity + acceleration), maxVelocity);
System.out.printf("NV: %.4f\n", velocity);
////////////////
//Position
x += velocity * Math.sin(heading);
y += velocity * Math.cos(heading);
if (x < 18 || y < 18 || x > fieldW - 18 || y > fieldH - 18) {
angleToTurn = 0;
velocity = 0;
x = Math.max(18, Math.min(fieldW - 18, x));
y = Math.max(18, Math.min(fieldH - 18, y));
}
}
return new MovSim2Stat(x,y,velocity,heading,Utils.normalRelativeAngle(heading - lastHeading));
}
public static final class MovSim2Stat {
public double x, y;
public double v, h, w; //w = heading change
public MovSim2Stat(double x, double y, double v, double h, double w) {
this.x = x;
this.y = y;
this.v = v;
this.h = h;
this.w = w;
}
}
}