use of spacesettlers.utilities.Movement in project spacesettlers by amymcgovern.
the class RawAction method getMovement.
/**
* Set the raw translational accelerations
*/
@Override
public Movement getMovement(Toroidal2DPhysics space, Ship ship) {
Movement movement = new Movement();
movement.setAngularAccleration(angularAcceleration);
movement.setTranslationalAcceleration(translationalAcceleration);
return movement;
}
use of spacesettlers.utilities.Movement in project spacesettlers by amymcgovern.
the class Toroidal2DPhysics method advanceTime.
/**
* Move all moveable objects and handle power ups.
*/
public void advanceTime(int currentTimeStep, Map<UUID, SpaceSettlersPowerupEnum> powerups) {
this.currentTimeStep = currentTimeStep;
// heal any base injuries
for (Base base : bases) {
base.updateEnergy(base.getHealingIncrement());
}
// detect collisions across all objects
detectCollisions();
// get the power ups and create any objects (weapons) as necessary
for (UUID key : powerups.keySet()) {
AbstractObject swobject = getObjectById(key);
// if the object is not alive or it is not actionable, then ignore this
if (!swobject.isAlive() || (!(swobject instanceof AbstractActionableObject))) {
continue;
}
// otherwise, handle the power up
handlePowerup((AbstractActionableObject) swobject, powerups.get(key));
}
// now move all objects that are moveable (which may include weapons)
for (AbstractObject object : allObjects) {
// skip non-moveable objects or dead object
if (!object.isMoveable() || !object.isAlive()) {
continue;
}
Position currentPosition = object.getPosition();
// is it a ship that can be controlled?
if (object.isControllable()) {
Ship ship = (Ship) object;
AbstractAction action = ship.getCurrentAction();
// handle a null action
if (action == null) {
action = new DoNothingAction();
}
// need to clone the ship and space because otherwise the ship can affect
// itself inside AbstractAction
Movement actionMovement = action.getMovement(this.deepClone(), ship.deepClone());
Position newPosition = applyMovement(currentPosition, actionMovement, timeStep);
if (newPosition.isValid()) {
ship.setPosition(newPosition);
} else {
ship.setPosition(currentPosition);
}
// spend ship energy proportional to its acceleration (old formula used velocity) and mass (new for space settlers
// since resources cost mass)
// double penalty = ENERGY_PENALTY * -Math.abs(ship.getPosition().getTotalTranslationalVelocity());
double angularAccel = Math.abs(actionMovement.getAngularAccleration());
double angularInertia = (3.0 * ship.getMass() * ship.getRadius() * angularAccel) / 2.0;
double linearAccel = actionMovement.getTranslationalAcceleration().getMagnitude();
double linearInertia = ship.getMass() * linearAccel;
int penalty = (int) Math.floor(ENERGY_PENALTY * (angularInertia + linearInertia));
ship.updateEnergy(-penalty);
// this isn't the most general fix but it will work for now (also has to be done for bases)
if (ship.isShielded()) {
ship.updateEnergy(-PowerupToggleShield.SHIELD_STEP_COST);
}
// if (!ship.isAlive()) {
// System.out.println("Ship " + ship.getTeamName() + ship.getId() + " is dead");
// }
} else {
// move all other types of objects
Position newPosition = moveOneTimestep(currentPosition);
object.setPosition(newPosition);
}
// if any ships or bases are frozen, decrement their frozen count
if (object instanceof AbstractActionableObject && !object.isControllable()) {
AbstractActionableObject actionable = (AbstractActionableObject) object;
actionable.decrementFreezeCount();
}
}
// go through and see if any bases have died
Set<Base> basesClone = new LinkedHashSet<Base>(bases);
for (Base base : basesClone) {
// this isn't the most general fix but it will work for now (also has to be done for bases)
if (base.isShielded()) {
base.updateEnergy(-PowerupToggleShield.SHIELD_STEP_COST);
}
if (!base.isAlive()) {
base.setAlive(false);
removeObject(base);
base.getTeam().removeBase(base);
}
}
// from when it was called inside updateEnergy
for (Ship ship : ships) {
if (ship.getEnergy() <= 0 && ship.isAlive() == true) {
// drop any resources that the ship was carrying
ResourcePile resources = ship.getResources();
// Spawn a new AiCore with the same velocity magnitude and direction as its parent ship.
// handle dropping the core if the ship died
Position corePosition = ship.getPosition();
corePosition.setTranslationalVelocity(ship.getPosition().getTranslationalVelocity());
corePosition.setAngularVelocity(ship.getPosition().getAngularVelocity());
AiCore shipCore = new AiCore(corePosition, ship.getTeamName(), ship.getTeamColor());
this.addObject(shipCore);
if (resources.getTotal() > 0) {
// Position newPosition = ship.getPosition();
// newPosition.setTranslationalVelocity(new Vector2D(0,0));
// newPosition.setAngularVelocity(0.0);
// Asteroid newAsteroid = new Asteroid(newPosition, true, ship.getRadius(), true, resources);
// this.addObject(newAsteroid);
// distributeResourcesToNearbyAsteroids(ship.getPosition(), resources);
// System.out.println("Adding a new asteroid with resources " + newAsteroid.getResources().getTotal() +
// " due to death, total is " + asteroids.size());
// System.out.println("Ship died and " + resources.getTotal() + " has been added to an asteroid");
}
// set the ship to dead last (so we can grab its resources first)
ship.setAlive(false);
}
}
}
use of spacesettlers.utilities.Movement in project spacesettlers by amymcgovern.
the class TestMoveAction method testpdControlMoveToAlongY.
/**
* Test moving to the goal along the y dimension
* (50, 40)
* (50, 50)
* (50,60)
*
* @throws SpaceSettlersActionException
*/
@Test
public void testpdControlMoveToAlongY() throws SpaceSettlersActionException {
// first positive y (50, 60)
Position currentLoc = new Position(50, 50);
Position goalLoc = new Position(50, 60);
currentLoc.setOrientation(Math.PI / 2);
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(), Math.PI / 2, 0.01);
assertEquals(currentLoc.getX(), 50, 0.05);
assertEquals(currentLoc.getY(), 60, 0.05);
// then to the negative y
currentLoc = new Position(50, 50);
currentLoc.setOrientation(0);
goalLoc = new Position(50, 40);
currentLoc.setOrientation(-Math.PI / 2);
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 / 2, 0.01);
assertEquals(currentLoc.getX(), 50, 0.05);
assertEquals(currentLoc.getY(), 40, 0.05);
}
use of spacesettlers.utilities.Movement in project spacesettlers by amymcgovern.
the class TestMoveActionWithOrientation 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 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(), -(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 TestMoveActionWithOrientation 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, 0);
Position goalLoc = new Position(40, 40, Math.PI);
moveAction = new MoveActionWithOrientation();
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(), Math.PI, 0.01);
// then to 3pi/4
currentLoc = new Position(50, 50, 0);
currentLoc.setOrientation(0);
goalLoc = new Position(40, 60, -Math.PI);
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(), -Math.PI, 0.01);
}
Aggregations