Difference between revisions of "User:Chase-san/MovSim"

From Robowiki
Jump to navigation Jump to search
m
m (don't actually need this limit here (assuming a user sets maxVelocity to something sane (that is +- <= MAX_VELOCITY)))
 
(7 intermediate revisions by the same user not shown)
Line 5: Line 5:
  
 
<syntaxhighlight>
 
<syntaxhighlight>
package chase.s2.move;
 
 
 
import java.awt.geom.Point2D;
 
import java.awt.geom.Point2D;
 
 
import robocode.Rules;
 
import robocode.Rules;
 
import robocode.util.Utils;
 
import robocode.util.Utils;
 
+
public class MovSim2 {
+
/**
//New DeAccel Rule, cannot go from -1 to 1, would go from -1 to 0.5
+
* A simulator class I wrote to make simulation simple.
public static final boolean useNewDeaccelRule = false;
+
*
+
* @author Chase
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);
+
public final class Simulate {
 +
public Point2D.Double position;
 +
public double heading;
 +
public double velocity;
 +
public double headingDelta;
 +
public double maxVelocity;
 +
public double angleToTurn;
 +
public int direction;
 +
 +
/**
 +
* Create a new Simulate class
 +
*/
 +
public Simulate() {
 +
position = new Point2D.Double();
 +
maxVelocity = Rules.MAX_VELOCITY;
 +
direction = 1;
 
}
 
}
+
//For Dead Code
+
/**
@SuppressWarnings("all")
+
* We can easily set the position with this.
public static final MovSim2Stat step(double x, double y, double velocity, double maxVelocity,
+
*/
int direction, double heading, double angleToTurn, double fieldW, double fieldH) {
+
public void setLocation(double x, double y) {
+
position.x = x;
 +
position.y = y;
 +
}
 +
 +
/**
 +
* Here we just make a copy of the simulator.
 +
*/
 +
public Simulate copy() {
 +
Simulate copy = new Simulate();
 +
copy.position.setLocation(this.position);
 +
copy.heading = this.heading;
 +
copy.velocity = this.velocity;
 +
copy.headingDelta = this.headingDelta;
 +
copy.maxVelocity = this.maxVelocity;
 +
copy.angleToTurn = this.angleToTurn;
 +
copy.direction = this.direction;
 +
return copy;
 +
}
 +
 +
/**
 +
* We calculate one step or turn into the future, and update the values accordingly
 +
*/
 +
public void step() {
 
////////////////
 
////////////////
 
//Heading
 
//Heading
 
double lastHeading = heading;
 
double lastHeading = heading;
double turnRate = Rules.getTurnRateRadians(velocity);
+
double turnRate = Rules.getTurnRateRadians(Math.abs(velocity));
if(Math.abs(angleToTurn) < turnRate) heading += angleToTurn;
+
double turn = Math.min(turnRate, Math.max(angleToTurn, -turnRate));
else heading += turnRate * Math.signum(angleToTurn);
+
heading = Utils.normalNearAbsoluteAngle(heading + turn);
heading = Utils.normalAbsoluteAngle(heading);
+
angleToTurn -= turn;
+
 
////////////////
 
////////////////
 
//Movement
 
//Movement
Line 39: Line 73:
 
//Acceleration
 
//Acceleration
 
double acceleration = 0;
 
double acceleration = 0;
double absVelocity = Math.abs(velocity);
+
double speed = Math.abs(velocity);
+
maxVelocity = Math.abs(maxVelocity);
//Check if we are stopping!
+
 +
//Determine the current direction
 +
int velDirection = (velocity > 0 ? (int)1 : (int)-1);
 +
 +
//Handles the zero direction, which means stop
 
if(direction == 0) {
 
if(direction == 0) {
//Slow to a stop!
+
maxVelocity = 0;
acceleration = Rules.DECELERATION;
+
direction = velDirection;
if(absVelocity < acceleration)
+
}
acceleration = -velocity;
+
else
+
//Handles speedup from zero
acceleration *= (velocity < 0 ? 1 : -1);  
+
if(speed < 0.000001) {
 +
velDirection = direction;
 +
}
 +
 +
//Check if we are speeding up or slowing down
 +
if(velDirection == direction) {
 +
//We are speeding up or maintaining speed
 +
if(speed <= maxVelocity) {
 +
//We are speeding up
 +
acceleration = Math.min(Rules.ACCELERATION, maxVelocity - speed);
 +
} else {
 +
//We are slowing down in the same direction
 +
if(speed > maxVelocity)
 +
acceleration = Math.max(-Rules.DECELERATION, maxVelocity - speed);
 +
//else we are maintaining speed (do nothing)
 +
}
 
} else {
 
} else {
if(absVelocity < 1e-6) {
+
//We are slowing down or stopping
velocity = 0;
+
if(speed < Rules.DECELERATION) {
//We can only speed up from here
+
//Limit pass over zero, special rules are here for this
acceleration = Rules.ACCELERATION;
+
double beyondZero = Math.abs(speed - Rules.DECELERATION);
if(acceleration > maxVelocity)
+
acceleration = speed + (beyondZero /= 2.0);
acceleration = maxVelocity;
+
 +
//Limit our acceleration so it does not go beyond max when passing over zero
 +
if(beyondZero > maxVelocity)
 +
acceleration = speed + maxVelocity;
 
} else {
 
} else {
//Check if we are speeding up
+
//Otherwise
if(Math.signum(velocity) == direction
+
acceleration = Rules.DECELERATION;
&& 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;
 
 
}
 
}
+
 +
//Apply the direction to the acceleration, so we don't have
 +
//to have a case for both directions
 +
acceleration *= direction;
 +
 
////////////////
 
////////////////
 
//Velocity
 
//Velocity
velocity = Math.min(Math.max(-maxVelocity, velocity + acceleration), maxVelocity);
+
velocity += acceleration;
+
 
 
////////////////
 
////////////////
 
//Position
 
//Position
x += velocity * Math.sin(heading);
+
position.x += Math.sin(heading) * velocity;
y += velocity * Math.cos(heading);
+
position.y += Math.cos(heading) * velocity;
 
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;
 
 
}
 
}
 +
 +
headingDelta = Utils.normalRelativeAngle(heading - lastHeading);
 
}
 
}
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 08:10, 16 July 2011

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 java.awt.geom.Point2D;
import robocode.Rules;
import robocode.util.Utils;
 
/**
 * A simulator class I wrote to make simulation simple.
 * 
 * @author Chase
 */
public final class Simulate {
	public Point2D.Double position;
	public double heading;
	public double velocity;
	public double headingDelta;
	public double maxVelocity;
	public double angleToTurn;
	public int direction;
 
	/**
	 * Create a new Simulate class
	 */
	public Simulate() {
		position = new Point2D.Double();
		maxVelocity = Rules.MAX_VELOCITY;
		direction = 1;
	}
 
	/**
	 * We can easily set the position with this.
	 */
	public void setLocation(double x, double y) {
		position.x = x;
		position.y = y;
	}
 
	/**
	 * Here we just make a copy of the simulator.
	 */
	public Simulate copy() {
		Simulate copy = new Simulate();
		copy.position.setLocation(this.position);
		copy.heading = this.heading;
		copy.velocity = this.velocity;
		copy.headingDelta = this.headingDelta;
		copy.maxVelocity = this.maxVelocity;
		copy.angleToTurn = this.angleToTurn;
		copy.direction = this.direction;
		return copy;
	}
 
	/**
	 * We calculate one step or turn into the future, and update the values accordingly
	 */
	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 speed = Math.abs(velocity);
			maxVelocity = Math.abs(maxVelocity);
 
			//Determine the current direction
			int velDirection = (velocity > 0 ? (int)1 : (int)-1);
 
			//Handles the zero direction, which means stop
			if(direction == 0) {
				maxVelocity = 0;
				direction = velDirection;
			}
 
			//Handles speedup from zero
			if(speed < 0.000001) {
				velDirection = direction;
			}
 
			//Check if we are speeding up or slowing down			
			if(velDirection == direction) {
				//We are speeding up or maintaining speed
				if(speed <= maxVelocity) {
					//We are speeding up
					acceleration = Math.min(Rules.ACCELERATION, maxVelocity - speed);
				} else {
					//We are slowing down in the same direction
					if(speed > maxVelocity)
						acceleration = Math.max(-Rules.DECELERATION, maxVelocity - speed);
					//else we are maintaining speed (do nothing)
				}
			} else {
				//We are slowing down or stopping
				if(speed < Rules.DECELERATION) {
					//Limit pass over zero, special rules are here for this
					double beyondZero = Math.abs(speed - Rules.DECELERATION);
					acceleration = speed + (beyondZero /= 2.0);
 
					//Limit our acceleration so it does not go beyond max when passing over zero
					if(beyondZero > maxVelocity)
						acceleration = speed + maxVelocity;
				} else {
					//Otherwise
					acceleration = Rules.DECELERATION;
				}
			}
 
			//Apply the direction to the acceleration, so we don't have
			//to have a case for both directions
			acceleration *= direction;
 
			////////////////
			//Velocity
			velocity += acceleration;
 
			////////////////
			//Position
			position.x += Math.sin(heading) * velocity;
			position.y += Math.cos(heading) * velocity;
		}
 
		headingDelta = Utils.normalRelativeAngle(heading - lastHeading);
	}
}