Search in sources :

Example 36 with Position

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;
}
Also used : Position(spacesettlers.utilities.Position)

Example 37 with Position

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;
}
Also used : Position(spacesettlers.utilities.Position)

Example 38 with Position

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);
}
Also used : MoveAction(spacesettlers.actions.MoveAction) Movement(spacesettlers.utilities.Movement) Vector2D(spacesettlers.utilities.Vector2D) Position(spacesettlers.utilities.Position) Test(org.junit.Test)

Example 39 with Position

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);
}
Also used : MoveAction(spacesettlers.actions.MoveAction) Movement(spacesettlers.utilities.Movement) Vector2D(spacesettlers.utilities.Vector2D) Position(spacesettlers.utilities.Position) Test(org.junit.Test)

Example 40 with Position

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);
}
Also used : MoveAction(spacesettlers.actions.MoveAction) Movement(spacesettlers.utilities.Movement) Position(spacesettlers.utilities.Position) Test(org.junit.Test)

Aggregations

Position (spacesettlers.utilities.Position)54 Test (org.junit.Test)28 Vector2D (spacesettlers.utilities.Vector2D)26 Ship (spacesettlers.objects.Ship)18 Movement (spacesettlers.utilities.Movement)16 Asteroid (spacesettlers.objects.Asteroid)14 AbstractAction (spacesettlers.actions.AbstractAction)12 DoNothingAction (spacesettlers.actions.DoNothingAction)12 Base (spacesettlers.objects.Base)11 Beacon (spacesettlers.objects.Beacon)10 MoveToObjectAction (spacesettlers.actions.MoveToObjectAction)9 UUID (java.util.UUID)6 MoveAction (spacesettlers.actions.MoveAction)6 AbstractObject (spacesettlers.objects.AbstractObject)6 AiCore (spacesettlers.objects.AiCore)5 HashMap (java.util.HashMap)4 Team (spacesettlers.clients.Team)2 AbstractActionableObject (spacesettlers.objects.AbstractActionableObject)2 Flag (spacesettlers.objects.Flag)2 ResourcePile (spacesettlers.objects.resources.ResourcePile)2