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

From Robowiki
Jump to navigation Jump to search
m (removing decel toggle (since rumble has been updated))
m (Making it easier to read/understand)
Line 5: Line 5:
  
 
<syntaxhighlight>
 
<syntaxhighlight>
 +
import java.awt.geom.Point2D;
 
import robocode.Rules;
 
import robocode.Rules;
 
import robocode.util.Utils;
 
import robocode.util.Utils;
 
+
public final class Simulator {
+
/**
 +
* A simulator class I wrote to make simulation simple.
 +
*
 +
* @author Chase
 +
*/
 +
public final class Simulate {
 
public Point2D.Double position;
 
public Point2D.Double position;
 
public double heading;
 
public double heading;
Line 16: Line 22:
 
public double angleToTurn;
 
public double angleToTurn;
 
public int direction;
 
public int direction;
+
public Simulator() {
+
/**
 +
* Create a new Simulate class
 +
*/
 +
public Simulate() {
 
position = new Point2D.Double();
 
position = new Point2D.Double();
 
maxVelocity = Rules.MAX_VELOCITY;
 
maxVelocity = Rules.MAX_VELOCITY;
 
direction = 1;
 
direction = 1;
 
}
 
}
 
+
 +
/**
 +
* We can easily set the position with this.
 +
*/
 
public void setLocation(double x, double y) {
 
public void setLocation(double x, double y) {
 
position.x = x;
 
position.x = x;
 
position.y = y;
 
position.y = y;
 
}
 
}
 
+
public Simulator copy() {
+
/**
Simulator copy = new Simulator();
+
* Here we just make a copy of the simulator.
 +
*/
 +
public Simulate copy() {
 +
Simulate copy = new Simulate();
 
copy.position.setLocation(this.position);
 
copy.position.setLocation(this.position);
 
copy.heading = this.heading;
 
copy.heading = this.heading;
Line 39: Line 54:
 
return copy;
 
return copy;
 
}
 
}
 
+
 +
/**
 +
* We calculate one step or turn into the future, and update the values accordingly
 +
*/
 
public void step() {
 
public void step() {
 
////////////////
 
////////////////
Line 48: Line 66:
 
heading = Utils.normalNearAbsoluteAngle(heading + turn);
 
heading = Utils.normalNearAbsoluteAngle(heading + turn);
 
angleToTurn -= turn;
 
angleToTurn -= turn;
+
 
////////////////
 
////////////////
 
//Movement
 
//Movement
Line 55: Line 73:
 
//Acceleration
 
//Acceleration
 
double acceleration = 0;
 
double acceleration = 0;
double absVelocity = Math.abs(velocity);
+
double speed = Math.abs(velocity);
 
maxVelocity = Math.abs(maxVelocity);
 
maxVelocity = Math.abs(maxVelocity);
+
//Stop and ask for directions
+
//Determine the current direction
 
int velDirection = (velocity > 0 ? (int)1 : (int)-1);
 
int velDirection = (velocity > 0 ? (int)1 : (int)-1);
+
//Handles direction zero stop
+
//Handles the zero direction, which means stop
 
if(direction == 0) {
 
if(direction == 0) {
 
maxVelocity = 0;
 
maxVelocity = 0;
 
direction = velDirection;
 
direction = velDirection;
 
}
 
}
+
 
//Handles speedup from zero
 
//Handles speedup from zero
if(absVelocity < 0.000001) {
+
if(speed < 0.000001) {
 
velDirection = direction;
 
velDirection = direction;
 
}
 
}
+
//Check directions
+
//Check if we are speeding up or slowing down
 
if(velDirection == direction) {
 
if(velDirection == direction) {
if(absVelocity <= maxVelocity) {
+
//We are speeding up or maintaining speed
 +
if(speed <= maxVelocity) {
 
//We are speeding up
 
//We are speeding up
acceleration = Math.min(Rules.ACCELERATION, maxVelocity - absVelocity);
+
acceleration = Math.min(Rules.ACCELERATION, maxVelocity - speed);
 
} else {
 
} else {
 
//We are slowing down in the same direction
 
//We are slowing down in the same direction
if(absVelocity > maxVelocity)
+
if(speed > maxVelocity)
acceleration = Math.max(-Rules.DECELERATION, maxVelocity - absVelocity);
+
acceleration = Math.max(-Rules.DECELERATION, maxVelocity - speed);
 +
//else we are maintaining speed (do nothing)
 
}
 
}
 
} else {
 
} else {
//If this is the case then we are always slowing down
+
//We are slowing down or stopping
if(absVelocity < Rules.DECELERATION) {
+
if(speed < Rules.DECELERATION) {
 
//Limit pass over zero, special rules are here for this
 
//Limit pass over zero, special rules are here for this
double beyondZero = Math.abs(absVelocity - Rules.DECELERATION);
+
double beyondZero = Math.abs(speed - Rules.DECELERATION);
acceleration = absVelocity + (beyondZero /= 2.0);
+
acceleration = speed + (beyondZero /= 2.0);
 
+
 
//Limit our acceleration so it does not go beyond max when passing over zero
 
//Limit our acceleration so it does not go beyond max when passing over zero
 
if(beyondZero > maxVelocity)
 
if(beyondZero > maxVelocity)
acceleration = absVelocity + maxVelocity;
+
acceleration = speed + maxVelocity;
 
} else {
 
} else {
 
//Otherwise
 
//Otherwise
Line 97: Line 117:
 
}
 
}
 
}
 
}
+
 +
//Apply the direction to the acceleration, so we don't have
 +
//to have a case for both directions
 
acceleration *= direction;
 
acceleration *= direction;
+
 
////////////////
 
////////////////
 
//Velocity
 
//Velocity
 
velocity = Math.min(Math.max(-Rules.MAX_VELOCITY, velocity + acceleration), Rules.MAX_VELOCITY);
 
velocity = Math.min(Math.max(-Rules.MAX_VELOCITY, velocity + acceleration), Rules.MAX_VELOCITY);
+
 
////////////////
 
////////////////
 
//Position
 
//Position
Line 109: Line 131:
 
position.y += Math.cos(heading) * velocity;
 
position.y += Math.cos(heading) * velocity;
 
}
 
}
+
 
headingDelta = Utils.normalRelativeAngle(heading - lastHeading);
 
headingDelta = Utils.normalRelativeAngle(heading - lastHeading);
 
}
 
}
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 08:00, 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 = 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);
	}
}