Search in sources :

Example 26 with Position

use of spacesettlers.utilities.Position in project spacesettlers by amymcgovern.

the class TestToroidal2DPhysics method testApplyOrientationMoveFromStop.

@Test
public void testApplyOrientationMoveFromStop() {
    Movement movement = new Movement();
    double angularAccel = Math.PI / 60.0;
    movement.setAngularAccleration(angularAccel);
    movement.setTranslationalAcceleration(new Vector2D());
    double expectedOrientation = angularAccel;
    Position newPosition = space.applyMovement(position, movement, timestep);
    assertEquals(newPosition.getOrientation(), expectedOrientation, 0.01);
    assertEquals(newPosition.getAngularVelocity(), angularAccel, 0.01);
    assertEquals(newPosition.getX(), 0, 0.0);
    assertEquals(newPosition.getY(), 0, 0.0);
    assertEquals(newPosition.getTotalTranslationalVelocity(), 0, 0);
}
Also used : Movement(spacesettlers.utilities.Movement) Vector2D(spacesettlers.utilities.Vector2D) Position(spacesettlers.utilities.Position) Test(org.junit.Test)

Example 27 with Position

use of spacesettlers.utilities.Position in project spacesettlers by amymcgovern.

the class TestToroidal2DPhysics method testApplyTranslationalMovementVerticalFromStop.

@Test
public void testApplyTranslationalMovementVerticalFromStop() {
    Movement movement = new Movement();
    movement.setAngularAccleration(0);
    movement.setTranslationalAcceleration(new Vector2D(0, 10));
    position.setOrientation(Math.PI / 2);
    double expectedX = 0;
    double expectedY = 10;
    Position newPosition = space.applyMovement(position, movement, timestep);
    assertEquals(newPosition.getX(), expectedX, 0.01);
    assertEquals(newPosition.getY(), expectedY, 0.01);
    assertEquals(newPosition.getOrientation(), Math.PI / 2, 0.01);
    assertEquals(newPosition.getAngularVelocity(), 0, 0.01);
    assertEquals(newPosition.getTranslationalVelocityX(), 0, 0.01);
    assertEquals(newPosition.getTranslationalVelocityY(), 10, 0.01);
}
Also used : Movement(spacesettlers.utilities.Movement) Vector2D(spacesettlers.utilities.Vector2D) Position(spacesettlers.utilities.Position) Test(org.junit.Test)

Example 28 with Position

use of spacesettlers.utilities.Position in project spacesettlers by amymcgovern.

the class TestToroidal2DPhysics method testApplyTranslationalMovementAngledFromStop.

@Test
public void testApplyTranslationalMovementAngledFromStop() {
    Movement movement = new Movement();
    movement.setAngularAccleration(0);
    movement.setTranslationalAcceleration(new Vector2D(10, 10));
    position.setOrientation(Math.PI / 4);
    double expectedX = 10;
    double expectedY = 10;
    Position newPosition = space.applyMovement(position, movement, timestep);
    assertEquals(newPosition.getX(), expectedX, 0.01);
    assertEquals(newPosition.getY(), expectedY, 0.01);
    assertEquals(newPosition.getOrientation(), Math.PI / 4, 0.01);
    assertEquals(newPosition.getAngularVelocity(), 0, 0.01);
    assertEquals(newPosition.getTranslationalVelocityX(), 10, 0.01);
    assertEquals(newPosition.getTranslationalVelocityY(), 10, 0.01);
}
Also used : Movement(spacesettlers.utilities.Movement) Vector2D(spacesettlers.utilities.Vector2D) Position(spacesettlers.utilities.Position) Test(org.junit.Test)

Example 29 with Position

use of spacesettlers.utilities.Position in project spacesettlers by amymcgovern.

the class TestToroidal2DPhysics method testFindShortestDistance.

/**
 * Tests the distances around a square using known distances from a triangle.
 * Ensures wrap-around works
 *
 * -10, -10		0, -10		10, -10
 * -10, 0		0, 0		10, 0
 * -10, 10		0, 10		10, 10
 */
@Test
public void testFindShortestDistance() {
    Position center = new Position(0, 0);
    // do all the diagonals
    double expectedDist = Math.sqrt(200);
    Position other = new Position(-10, -10);
    double dist = space.findShortestDistance(center, other);
    assertEquals(dist, expectedDist, 0.01);
    other = new Position(10, -10);
    dist = space.findShortestDistance(center, other);
    assertEquals(dist, expectedDist, 0.01);
    other = new Position(10, 10);
    dist = space.findShortestDistance(center, other);
    assertEquals(dist, expectedDist, 0.01);
    other = new Position(-10, 10);
    dist = space.findShortestDistance(center, other);
    assertEquals(dist, expectedDist, 0.01);
    // now the square ones
    expectedDist = 10;
    other = new Position(0, -10);
    dist = space.findShortestDistance(center, other);
    assertEquals(dist, expectedDist, 0.01);
    other = new Position(10, 0);
    dist = space.findShortestDistance(center, other);
    assertEquals(dist, expectedDist, 0.01);
    other = new Position(0, 10);
    dist = space.findShortestDistance(center, other);
    assertEquals(dist, expectedDist, 0.01);
    other = new Position(-10, 0);
    dist = space.findShortestDistance(center, other);
    assertEquals(dist, expectedDist, 0.01);
    // and itself
    dist = space.findShortestDistance(center, center);
    assertEquals(dist, 0, 0.01);
}
Also used : Position(spacesettlers.utilities.Position) Test(org.junit.Test)

Example 30 with Position

use of spacesettlers.utilities.Position in project spacesettlers by amymcgovern.

the class HumanTeamClient method getMovementStart.

/**
 * Look at the last key pressed by the human and do its movement
 */
@Override
public Map<UUID, AbstractAction> getMovementStart(Toroidal2DPhysics space, Set<AbstractActionableObject> actionableObjects) {
    HashMap<UUID, AbstractAction> actions = new HashMap<UUID, AbstractAction>();
    for (AbstractObject actionable : actionableObjects) {
        if (actionable instanceof Ship) {
            // get the current position
            Ship ship = (Ship) actionable;
            Position myPosition = ship.getPosition();
            Vector2D currentVelocity = myPosition.getTranslationalVelocity();
            RawAction action = null;
            double angularVel = myPosition.getAngularVelocity();
            // if the key was up or down, accelerate along the current line
            if (lastKeyPressed == HumanKeyPressed.UP) {
                Vector2D newVel = new Vector2D(HUMAN_ACCEL * Math.cos(myPosition.getOrientation()), HUMAN_ACCEL * Math.sin(myPosition.getOrientation()));
                newVel.add(currentVelocity);
                action = new RawAction(newVel, 0);
            } else if (lastKeyPressed == HumanKeyPressed.DOWN) {
                Vector2D newVel = new Vector2D(-HUMAN_ACCEL * Math.cos(myPosition.getOrientation()), -HUMAN_ACCEL * Math.sin(myPosition.getOrientation()));
                newVel.add(currentVelocity);
                action = new RawAction(newVel, 0);
            }
            // if the key was right or left, turn
            if (lastKeyPressed == HumanKeyPressed.RIGHT) {
                action = new RawAction(0, HUMAN_TURN_ACCEL);
            } else if (lastKeyPressed == HumanKeyPressed.LEFT) {
                action = new RawAction(0, -HUMAN_TURN_ACCEL);
            }
            // was the mouse clicked?
            if (lastMouseClick != null) {
                if (mouseClickMove == null || mouseClickMove.isMovementFinished(space) || space.findShortestDistance(lastMouseClick, myPosition) > CLICK_DISTANCE) {
                    mouseClickMove = new MoveAction(space, myPosition, lastMouseClick);
                    graphicsToAdd.add(new StarGraphics(3, super.teamColor, lastMouseClick));
                    LineGraphics line = new LineGraphics(myPosition, lastMouseClick, space.findShortestDistanceVector(myPosition, lastMouseClick));
                    line.setLineColor(super.teamColor);
                    graphicsToAdd.add(line);
                }
                actions.put(actionable.getId(), mouseClickMove);
            } else {
                actions.put(actionable.getId(), action);
            }
        } else {
            // can't really control anything but the ship
            actions.put(actionable.getId(), new DoNothingAction());
        }
    }
    return actions;
}
Also used : HashMap(java.util.HashMap) Position(spacesettlers.utilities.Position) RawAction(spacesettlers.actions.RawAction) LineGraphics(spacesettlers.graphics.LineGraphics) MoveAction(spacesettlers.actions.MoveAction) Vector2D(spacesettlers.utilities.Vector2D) AbstractObject(spacesettlers.objects.AbstractObject) Ship(spacesettlers.objects.Ship) UUID(java.util.UUID) StarGraphics(spacesettlers.graphics.StarGraphics) AbstractAction(spacesettlers.actions.AbstractAction) DoNothingAction(spacesettlers.actions.DoNothingAction)

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