/**
* class MySubAnimatorSource
- my illustrative
* specification of methods called by the SubAnimator to get the
* simulation information necessary for animation. This is just to
* give you an idea of how your code can interact with the SubAnimator.
*
* @author Todd W. Neller
* @version 1.0
* @since 1.0
* @see SubAnimatorSource */
public class MySubAnimatorSource implements SubAnimatorSource
{
static final double MIN_X = -100.0;
static final double MIN_Y = -100.0;
static final double MAX_X = 100.0;
static final double MAX_Y = 100.0;
static final double[] BOUNDS = {MIN_X, MIN_Y, MAX_X, MAX_Y};
static final int FRAMES = 1000;
static final double MAX_TIME = 10.0;
final double FIXED_VELOCITY = 50.0;
int enemies = 5;
double[][] detectionRadii = null;
double[] times = null;
double[][] subPos = null;
double[][][] enemyPos = null;
// The first ship is the sub, the rest are enemies.
double[][] shipPos = null;
double[][] shipVel = null;
java.util.Random rng = new java.util.Random();
/**
* generateNewScenario
- creates a new problem initial
* state. */
public void generateNewScenario()
{
// For this code, you don't need to actually generate
// animation information. Here, it's important to establish a
// new initial problem scenario.
times = null;
subPos = null;
enemyPos = null;
// Initialize detection radii.
final double[] RADII = {5.0, 10.0};
detectionRadii = new double[enemies][];
for (int i=0; isolveNewScenario - solves the current problem
* (implemented in the stage after simulation). */
public void solveNewScenario()
{
// For the simulation assignment, you don't actually try to
// solve your problem. Instead, play out a single scenario to
// demonstrate the functionality of your simulation. For this
// demo, I'll simply create random ship positions and have
// them bounce off the area bounds.
// Check that scenario exists.
if (shipPos == null) {
System.out.println("Scenario not yet created - exiting solver.");
return;
}
// Set up sample times
times = new double[FRAMES];
for (int i=0; i MAX_X) {
currShipPos[i][j][0] = MAX_X - (currShipPos[i][j][0] - MAX_X);
currShipVel[j][0] *= -1;
}
currShipPos[i][j][1] = currShipPos[i-1][j][1] + deltaT * currShipVel[j][1];
if (currShipPos[i][j][1] < MIN_Y) {
currShipPos[i][j][1] = MIN_Y + (MIN_Y - currShipPos[i][j][1]);
currShipVel[j][1] *= -1;
}
if (currShipPos[i][j][1] > MAX_Y) {
currShipPos[i][j][1] = MAX_Y - (currShipPos[i][j][1] - MAX_Y);
currShipVel[j][1] *= -1;
}
}
}
// Copy to output arrays
subPos = new double[FRAMES][2];
enemyPos = new double[FRAMES][enemies][2];
for (int i=0; igetCoordinateBounds - returns xMin yMin xMax yMax
* for animation coordinate system. The aspect ratio will be
* maintained within the animation frame.
*
* @return double[]
- array of length 4 with xMin
* yMin xMax yMax for animation coordinate system. */
public double[] getCoordinateBounds()
{
return BOUNDS;
}
/**
* getDetectionRadii
- returns an n by 2 array which,
* for each of n enemy ships, gives inner and outer detection
* radii (in that order).
*
* @return double[][]
- an n by 2 array which, for
* each of n enemy ships, gives inner and outer detection radii
* (in that order). */
public double[][] getDetectionRadii()
{
return detectionRadii;
}
/**
* getTimeVector
- returns an array with all time
* point to be displayed in simulation. Time is assumed to be in
* seconds. You aren't confined to real-time simulation - you may
* scale your time points as desired for viewing speed.
*
* @return double[]
- ordered array of time points
* for animation. */
public double[] getTimeVector()
{
return times;
}
/**
* getSubPositions
- returns the position of the sub
* at each time point returned by getTimeVector.
*
* @return double[][]
- ordered n x 2 array with
* (x,y) coordinates of the submarine at each of the n time points
* returned by getTimeVector. */
public double[][] getSubPositions()
{
return subPos;
}
/**
* getSubPositions
- returns the positions of enemy
* ships at each time point returned by getTimeVector.
*
* @return double[][][]
- ordered n x m x 2 array
* with (x,y) coordinates of each of m enemy ships at each of the
* n time points returned by getTimeVector. */
public double[][][] getEnemyPositions()
{
return enemyPos;
}
/**
* increaseNextDifficulty
- increase the difficulty
* level for the next generated problem. */
public void increaseNextDifficulty()
{
enemies++;
}
/**
* decreaseNextDifficulty
- decrease the difficulty
* level for the next generated problem. */
public void decreaseNextDifficulty()
{
if (enemies > 0)
enemies--;
}
}