use of spacesettlers.utilities.Position in project spacesettlers by amymcgovern.
the class Toroidal2DPhysics method getRandomFreeLocationInRegion.
/**
* Returns a new random free location inside the specified box of space
*
* @param rand Random number generator
* @return
*/
public Position getRandomFreeLocationInRegion(Random rand, int freeRadius, int ULX, int ULY, int LRX, int LRY) {
int boxWidth = LRX - ULX;
int boxHeight = LRY - ULY;
// System.out.println("Making a random location inside UL (x,y) " + ULX + ", " + ULY +
// " to LR (x,y) " + LRY + ", " + LRY);
Position centerPosition = new Position(boxWidth / 2 + ULX, boxHeight / 2 + ULY);
// System.out.println("Center position is " + centerPosition);
double newX = ((2 * rand.nextDouble()) - 1) * (boxWidth / 2.0) + centerPosition.getX();
double newY = ((2 * rand.nextDouble()) - 1) * (boxHeight / 2.0) + centerPosition.getY();
Position randLocation = new Position(newX, newY);
toroidalWrap(randLocation);
while (!isLocationFree(randLocation, freeRadius)) {
newX = ((2 * rand.nextDouble()) - 1) * (boxWidth / 2.0) + centerPosition.getX();
newY = ((2 * rand.nextDouble()) - 1) * (boxHeight / 2.0) + centerPosition.getY();
randLocation = new Position(newX, newY);
toroidalWrap(randLocation);
}
// System.out.println("random location chosen is " + randLocation);
return randLocation;
}
use of spacesettlers.utilities.Position in project spacesettlers by amymcgovern.
the class Toroidal2DPhysics method getRandomFreeLocationInRegion.
/**
* Returns a new random free location in space
*
* @param rand Random number generator
* @param freeRadius the radius around the object that must be free
* @return
*/
public Position getRandomFreeLocationInRegion(Random rand, int freeRadius, int centerX, int centerY, double maxDistance) {
Position centerPosition = new Position(centerX, centerY);
double newX = ((2 * rand.nextDouble()) - 1) * maxDistance + centerX;
double newY = ((2 * rand.nextDouble()) - 1) * maxDistance + centerY;
Position randLocation = new Position(newX, newY);
toroidalWrap(randLocation);
while (!isLocationFree(randLocation, freeRadius) || findShortestDistance(centerPosition, randLocation) > maxDistance) {
newX = ((2 * rand.nextDouble()) - 1) * maxDistance + centerX;
newY = ((2 * rand.nextDouble()) - 1) * maxDistance + centerY;
randLocation = new Position(newX, newY);
toroidalWrap(randLocation);
}
return randLocation;
}
use of spacesettlers.utilities.Position 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.Position 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.Position 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);
}
Aggregations