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

From Robowiki
Jump to navigation Jump to search
(minor improvement)
(fixing more bugs (yeah...))
Line 14: Line 14:
 
public class MovSim2 {
 
public class MovSim2 {
 
//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 = true;
 
 
 
public static final MovSim2Stat step(Point2D p, double velocity, int direction, double heading, double angle) {
 
public static final MovSim2Stat step(Point2D p, double velocity, int direction, double heading, double angle) {
Line 40: Line 40:
 
double acceleration = 0;
 
double acceleration = 0;
 
double absVelocity = Math.abs(velocity);
 
double absVelocity = Math.abs(velocity);
 +
maxVelocity = Math.abs(maxVelocity);
 +
 +
//Stop and ask for directions
 +
int velDirection = (velocity > 0 ? (int)1 : (int)-1);
 
 
//Check if we are stopping!
 
 
if(direction == 0) {
 
if(direction == 0) {
//Slow to a stop!
+
maxVelocity = 0;
acceleration = Rules.DECELERATION;
+
direction = velDirection;
if(absVelocity < acceleration)
+
}
acceleration = -velocity;
+
else
+
if(absVelocity < 0.000001) {
acceleration *= (velocity < 0 ? 1 : -1);  
+
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 {
 
} else {
if(absVelocity < 1e-6) {
+
//If this is the case then we are always slowing down
velocity = 0;
+
if(absVelocity < Rules.DECELERATION) {
//We can only speed up from here
+
//Do we pass over zero, special rules are here for this
acceleration = Rules.ACCELERATION;
+
double beyondZero = Math.abs(absVelocity - Rules.DECELERATION);
if(acceleration > maxVelocity)
+
if(useNewDeaccelRule) {
acceleration = maxVelocity;
+
acceleration = absVelocity + (beyondZero /= 2.0);
} else {
 
//Check if we are speeding up
 
if(Math.signum(velocity) == direction
 
&& absVelocity <= maxVelocity) {
 
acceleration = Rules.ACCELERATION;
 
 
} else {
 
} else {
//We are slowing down, this is more complicated
+
acceleration = Rules.DECELERATION;
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;
 
}
 
}
 
}
 
 
}
 
}
 +
//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;
 
 
}
 
}
 +
 +
acceleration *= direction;
 +
System.out.println("acceleration : " + acceleration);
 
 
 
////////////////
 
////////////////
 
//Velocity
 
//Velocity
velocity = Math.min(Math.max(-maxVelocity, velocity + acceleration), maxVelocity);
+
velocity = Math.min(Math.max(-Rules.MAX_VELOCITY, velocity + acceleration), Rules.MAX_VELOCITY);
 
 
 
////////////////
 
////////////////

Revision as of 01:13, 14 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 = true;
	
	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);
	}
	
	//For Dead Code
	@SuppressWarnings("all")
	public static final MovSim2Stat step(double x, double y, double velocity, double maxVelocity,
			int direction, double heading, double angleToTurn) {
		
		////////////////
		//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);
			maxVelocity = Math.abs(maxVelocity);
			
			//Stop and ask for directions
			int velDirection = (velocity > 0 ? (int)1 : (int)-1);
			
			if(direction == 0) {
				maxVelocity = 0;
				direction = velDirection;
			}
			
			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;
			System.out.println("acceleration : " + acceleration);
			
			////////////////
			//Velocity
			velocity = Math.min(Math.max(-Rules.MAX_VELOCITY, velocity + acceleration), Rules.MAX_VELOCITY);
			
			////////////////
			//Position
			x += velocity * Math.sin(heading);
			y += velocity * Math.cos(heading);
		}
		return new MovSim2Stat(x,y,velocity,heading,Utils.normalRelativeAngle(heading - lastHeading));
	}
	
	
	public static final MovSim2Stat stepWall(double x, double y, double velocity, double maxVelocity,
			int direction, double heading, double angleToTurn, double fieldW, double fieldH) {
		MovSim2Stat stat = step(x,y,velocity,maxVelocity,direction, heading, angleToTurn);
		if (stat.x < 18 || stat.y < 18 || stat.x > fieldW - 18 || stat.y > fieldH - 18) {
			stat.v = 0;
			stat.x = Math.max(18, Math.min(fieldW - 18, stat.x));
			stat.y = Math.max(18, Math.min(fieldH - 18, stat.y));
		}
		return stat;
	}
	
	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;
		}
	}
}