use of spacesettlers.utilities.Movement in project spacesettlers by amymcgovern.
the class AbstractAction method getMovement.
/**
* Calls the abstract getMovement with supplied timeout for response. If
* nothing is returned in that time, simple return DoNothingAction (new Movement())
*
* @return
*/
public Movement getMovement(Toroidal2DPhysics space, Ship ship, int timeout) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Movement> future = executor.submit(new SpacewarActionCallable(this, space, ship));
Movement movement = null;
try {
// start
movement = future.get(timeout, TimeUnit.MILLISECONDS);
// finished in time
} catch (TimeoutException e) {
// was terminated
// return no movement
movement = new Movement();
} catch (InterruptedException e) {
// we were interrupted (should not happen but lets be good programmers)
// return no movement
movement = new Movement();
e.printStackTrace();
} catch (ExecutionException e) {
// the executor threw and exception (should not happen but lets be good programmers)
// return no movement
movement = new Movement();
}
executor.shutdownNow();
return movement;
}
use of spacesettlers.utilities.Movement in project spacesettlers by amymcgovern.
the class TestMoveAction method testpdControlMoveToGoal.
/**
* Test moving to the goal
*
* (40, 40)
* (50,50)
* (40, 60)
*
* @throws SpaceSettlersActionException
*/
@Test
public void testpdControlMoveToGoal() throws SpaceSettlersActionException {
// first to -3pi/4
Position currentLoc = new Position(50, 50);
Position goalLoc = new Position(40, 40);
currentLoc.setOrientation(-(3 * Math.PI) / 4);
moveAction = new MoveAction();
Vector2D accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
Movement movement = new Movement();
while (accel.getMagnitude() > MoveAction.TARGET_REACHED_ACCEL) {
movement.setTranslationalAcceleration(accel);
currentLoc = space.applyMovement(currentLoc, movement, timestep);
accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
}
assertEquals(currentLoc.getOrientation(), -(3 * Math.PI) / 4, 0.01);
assertEquals(currentLoc.getX(), 40, 0.05);
assertEquals(currentLoc.getY(), 40, 0.05);
// then to 3pi/4
currentLoc = new Position(50, 50);
currentLoc.setOrientation(0);
goalLoc = new Position(40, 60);
currentLoc.setOrientation((3 * Math.PI) / 4);
accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
while (accel.getMagnitude() > MoveAction.TARGET_REACHED_ACCEL) {
movement.setTranslationalAcceleration(accel);
currentLoc = space.applyMovement(currentLoc, movement, timestep);
accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
}
assertEquals(currentLoc.getOrientation(), (3 * Math.PI) / 4, 0.01);
assertEquals(currentLoc.getX(), 40, 0.05);
assertEquals(currentLoc.getY(), 60, 0.05);
}
use of spacesettlers.utilities.Movement in project spacesettlers by amymcgovern.
the class TestMoveAction method testpdControlMoveToAlongX.
/**
* Test moving to the goal
*
* (30, 50) (50,50) (70, 50)
*
* @throws SpaceSettlersActionException
*/
@Test
public void testpdControlMoveToAlongX() throws SpaceSettlersActionException {
// first positive x (60,50)
Position currentLoc = new Position(50, 50);
Position goalLoc = new Position(70, 50);
currentLoc.setOrientation(0);
moveAction = new MoveAction();
Vector2D accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
Movement movement = new Movement();
while (accel.getMagnitude() > MoveAction.TARGET_REACHED_ACCEL) {
movement.setTranslationalAcceleration(accel);
currentLoc = space.applyMovement(currentLoc, movement, timestep);
accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
}
assertEquals(currentLoc.getOrientation(), 0, 0.01);
assertEquals(currentLoc.getX(), 70, 0.05);
assertEquals(currentLoc.getY(), 50, 0.05);
// then to the negative x
currentLoc = new Position(50, 50);
currentLoc.setOrientation(0);
goalLoc = new Position(30, 50);
currentLoc.setOrientation(-Math.PI);
accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
while (accel.getMagnitude() > MoveAction.TARGET_REACHED_ACCEL) {
movement.setTranslationalAcceleration(accel);
currentLoc = space.applyMovement(currentLoc, movement, timestep);
accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
}
assertEquals(currentLoc.getOrientation(), -Math.PI, 0.01);
assertEquals(currentLoc.getX(), 30, 0.05);
assertEquals(currentLoc.getY(), 50, 0.05);
}
use of spacesettlers.utilities.Movement in project spacesettlers by amymcgovern.
the class TestMoveAction method testpdControlOrientToGoal.
/**
* Test orienting in both directions
*
* (40, 40)
* (50,50)
* (40, 60)
*
* @throws SpaceSettlersActionException
*/
@Test
public void testpdControlOrientToGoal() throws SpaceSettlersActionException {
// first to -3pi/4
Position currentLoc = new Position(50, 50);
Position goalLoc = new Position(40, 40);
moveAction = new MoveAction();
double accel = moveAction.pdControlOrientToGoal(space, goalLoc, currentLoc, 0);
Movement movement = new Movement();
while (Math.abs(accel) > MoveAction.TARGET_REACHED_ACCEL) {
movement.setAngularAccleration(accel);
currentLoc = space.applyMovement(currentLoc, movement, timestep);
accel = moveAction.pdControlOrientToGoal(space, goalLoc, currentLoc, 0);
}
assertEquals(currentLoc.getOrientation(), -(3 * Math.PI) / 4, 0.01);
// then to 3pi/4
currentLoc = new Position(50, 50);
currentLoc.setOrientation(0);
goalLoc = new Position(40, 60);
accel = moveAction.pdControlOrientToGoal(space, goalLoc, currentLoc, 0);
movement = new Movement();
while (Math.abs(accel) > MoveAction.TARGET_REACHED_ACCEL) {
movement.setAngularAccleration(accel);
currentLoc = space.applyMovement(currentLoc, movement, timestep);
accel = moveAction.pdControlOrientToGoal(space, goalLoc, currentLoc, 0);
}
assertEquals(currentLoc.getOrientation(), (3 * Math.PI) / 4, 0.01);
}
use of spacesettlers.utilities.Movement in project spacesettlers by amymcgovern.
the class TestMoveActionWithOrientation method testpdControlMoveToAlongX.
/**
* Test moving to the goal
*
* (30, 50) (50,50) (70, 50)
*
* @throws SpaceSettlersActionException
*/
@Test
public void testpdControlMoveToAlongX() throws SpaceSettlersActionException {
// first positive x (60,50)
Position currentLoc = new Position(50, 50);
Position goalLoc = new Position(70, 50);
currentLoc.setOrientation(0);
moveAction = new MoveActionWithOrientation();
Vector2D accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
Movement movement = new Movement();
while (accel.getMagnitude() > MoveAction.TARGET_REACHED_ACCEL) {
movement.setTranslationalAcceleration(accel);
currentLoc = space.applyMovement(currentLoc, movement, timestep);
accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
}
assertEquals(currentLoc.getOrientation(), 0, 0.01);
assertEquals(currentLoc.getX(), 70, 0.05);
assertEquals(currentLoc.getY(), 50, 0.05);
// then to the negative x
currentLoc = new Position(50, 50);
currentLoc.setOrientation(0);
goalLoc = new Position(30, 50);
currentLoc.setOrientation(-Math.PI);
accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
while (accel.getMagnitude() > MoveAction.TARGET_REACHED_ACCEL) {
movement.setTranslationalAcceleration(accel);
currentLoc = space.applyMovement(currentLoc, movement, timestep);
accel = moveAction.pdControlMoveToGoal(space, goalLoc, currentLoc, targetVelocity);
}
assertEquals(currentLoc.getOrientation(), -Math.PI, 0.01);
assertEquals(currentLoc.getX(), 30, 0.05);
assertEquals(currentLoc.getY(), 50, 0.05);
}
Aggregations