Search in sources :

Example 26 with Vector2D

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

the class CollisionHandler method elasticCollision2DWithNonMoveableObject.

/**
 * Elastically collide all objects following the vector formulation found at:
 *
 * http://www.vobarian.com/collisions/2dcollisions2.pdf
 *
 * @param object1 first object in the collision
 * @param object2 second object in the collision
 * @param space handle to space for distance calculations
 */
private void elasticCollision2DWithNonMoveableObject(AbstractObject movingObject, AbstractObject stationaryObject, Toroidal2DPhysics space) {
    // handle overlapping objects
    adjustCentersAtCollision(movingObject, stationaryObject, space);
    // get the masses
    double m1 = movingObject.getMass();
    double m2 = stationaryObject.getMass();
    // now get the vector from the first to the second, get the unit normal and tangent
    Vector2D distanceVec = space.findShortestDistanceVector(movingObject.getPosition(), stationaryObject.getPosition());
    Vector2D unitNormal = distanceVec.getUnitVector();
    Vector2D unitTangent = new Vector2D(-unitNormal.getYValue(), unitNormal.getXValue());
    // get the velocity vectors
    Vector2D velocity1 = movingObject.getPosition().getTranslationalVelocity();
    // get the scalars in each direction
    double u1 = velocity1.dot(unitNormal);
    double t1 = velocity1.dot(unitTangent);
    // now just reverse the velocity for the first object
    double v1 = -u1;
    // now get it back to the original space
    Vector2D vel1Normal = unitNormal.multiply(v1);
    Vector2D vel1Tangent = unitTangent.multiply(t1);
    // add the normal and tangential parts
    Vector2D newVelocity1 = vel1Normal.add(vel1Tangent);
    movingObject.getPosition().setTranslationalVelocity(newVelocity1);
// ensureObjectsNotStillColliding(movingObject, stationaryObject, space);
}
Also used : Vector2D(spacesettlers.utilities.Vector2D)

Example 27 with Vector2D

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

the class Toroidal2DPhysics method respawnDeadObjects.

/**
 * Respawns any dead objects in new random locations.  Ships
 * have a delay before they can respawn.
 */
public void respawnDeadObjects(Random random, double asteroidMaxVelocity) {
    for (AbstractObject object : allObjects) {
        if (!object.isAlive() && object.canRespawn()) {
            Position newPosition = null;
            // flags should re-spawn at a randomly chosen starting location
            if (object instanceof Flag) {
                Flag flag = (Flag) object;
                newPosition = flag.getNewStartingPosition(random);
                // ensure their starting location is free (to handle the thought bug the class
                // introduced of putting a ship or a base where the flag should spawn)
                newPosition = getRandomFreeLocationInRegion(random, flag.getRadius() * 2, (int) newPosition.getX(), (int) newPosition.getY(), 75);
            } else {
                // note this is time 2 in order to ensure objects don't spawn touching (and just to get
                // them a bit farther apart
                newPosition = getRandomFreeLocation(random, object.getRadius() * 2);
            }
            object.setPosition(newPosition);
            object.setAlive(true);
            object.setDrawable(true);
            // reset the UUID if it is a asteroid or beacon
            if (object instanceof Asteroid || object instanceof Beacon) {
                object.resetId();
            }
            // make moveable asteroids move again when they respawn
            if (object.isMoveable() && !(object instanceof Flag)) {
                Vector2D randomMotion = Vector2D.getRandom(random, asteroidMaxVelocity);
                object.getPosition().setTranslationalVelocity(randomMotion);
            }
        }
    }
}
Also used : Asteroid(spacesettlers.objects.Asteroid) Vector2D(spacesettlers.utilities.Vector2D) Position(spacesettlers.utilities.Position) AbstractObject(spacesettlers.objects.AbstractObject) Beacon(spacesettlers.objects.Beacon) Flag(spacesettlers.objects.Flag)

Example 28 with Vector2D

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

the class TestMoveAction method setUp.

@Before
public void setUp() throws Exception {
    space = new Toroidal2DPhysics(480, 640, timestep);
    targetVelocity = new Vector2D();
}
Also used : Vector2D(spacesettlers.utilities.Vector2D) Toroidal2DPhysics(spacesettlers.simulator.Toroidal2DPhysics) Before(org.junit.Before)

Example 29 with Vector2D

use of spacesettlers.utilities.Vector2D 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 30 with Vector2D

use of spacesettlers.utilities.Vector2D 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)

Aggregations

Vector2D (spacesettlers.utilities.Vector2D)49 Test (org.junit.Test)33 Position (spacesettlers.utilities.Position)26 Movement (spacesettlers.utilities.Movement)15 Asteroid (spacesettlers.objects.Asteroid)8 Ship (spacesettlers.objects.Ship)8 MoveAction (spacesettlers.actions.MoveAction)4 Before (org.junit.Before)3 AbstractObject (spacesettlers.objects.AbstractObject)3 Toroidal2DPhysics (spacesettlers.simulator.Toroidal2DPhysics)2 HashMap (java.util.HashMap)1 Random (java.util.Random)1 UUID (java.util.UUID)1 AbstractAction (spacesettlers.actions.AbstractAction)1 DoNothingAction (spacesettlers.actions.DoNothingAction)1 RawAction (spacesettlers.actions.RawAction)1 LineGraphics (spacesettlers.graphics.LineGraphics)1 StarGraphics (spacesettlers.graphics.StarGraphics)1 Beacon (spacesettlers.objects.Beacon)1 Flag (spacesettlers.objects.Flag)1