use of spacesettlers.utilities.Vector2D in project spacesettlers by amymcgovern.
the class SpaceSettlersSimulator method createNewRandomAsteroid.
/**
* Create a new asteroid following all rules of the config files
*
* Asteroids can either be fixed location or randomly generated. If they are
* fixed, they need x, y, and radius.
*
* @param asteroidConfig
* @return
*/
private Asteroid createNewRandomAsteroid(RandomAsteroidConfig asteroidConfig) {
// choose if the asteroid is mine-able
double prob = random.nextDouble();
boolean mineable = false;
if (prob < asteroidConfig.getProbabilityMineable()) {
mineable = true;
}
// asteroids
// choose the radius randomly for random asteroids
int radius = random.nextInt(Asteroid.MAX_ASTEROID_RADIUS - Asteroid.MIN_ASTEROID_RADIUS) + Asteroid.MIN_ASTEROID_RADIUS;
// choose if the asteroid is moving or stationary
prob = random.nextDouble();
boolean moveable = false;
if (prob < asteroidConfig.getProbabilityMoveable()) {
moveable = true;
}
// choose the asteroid mixture
double fuel = random.nextDouble() * asteroidConfig.getProbabilityFuelType();
double water = random.nextDouble() * asteroidConfig.getProbabilityWaterType();
double metals = random.nextDouble() * asteroidConfig.getProbabilityMetalsType();
// renormalize so it all adds to 1
double normalize = fuel + water + metals;
fuel = fuel / normalize;
water = water / normalize;
metals = metals / normalize;
// create the asteroid
Asteroid asteroid = new Asteroid(simulatedSpace.getRandomFreeLocation(random, radius * 2), mineable, radius, moveable, fuel, water, metals);
if (asteroid.isMoveable()) {
Vector2D randomMotion = Vector2D.getRandom(random, asteroidConfig.getMaxInitialVelocity());
asteroid.getPosition().setTranslationalVelocity(randomMotion);
}
return asteroid;
}
use of spacesettlers.utilities.Vector2D in project spacesettlers by amymcgovern.
the class Toroidal2DPhysics method applyMovement.
/**
* Takes an acceleration and a simulation time step and moves the object
*
* @param actionMovement
* @param timeStep
* @return
*/
public Position applyMovement(Position position, Movement movement, double timeStep) {
double translationalAccelX = movement.getTranslationalAcceleration().getXValue();
double translationalAccelY = movement.getTranslationalAcceleration().getYValue();
double angularAccel = movement.getAngularAccleration();
// velocity is acceleration times time
double translationalVelocityX = position.getTranslationalVelocityX() + (translationalAccelX * timeStep);
double translationalVelocityY = position.getTranslationalVelocityY() + (translationalAccelY * timeStep);
double angularVelocity = position.getAngularVelocity() + (angularAccel * timeStep);
// ensure the max/mins are respected
translationalVelocityX = checkTranslationalVelocity(translationalVelocityX);
translationalVelocityY = checkTranslationalVelocity(translationalVelocityY);
angularVelocity = checkAngularVelocity(angularVelocity);
Position newPosition = new Position(position.getX(), position.getY(), position.getOrientation());
newPosition.setTranslationalVelocity(new Vector2D(translationalVelocityX, translationalVelocityY));
newPosition.setAngularVelocity(angularVelocity);
return moveOneTimestep(newPosition);
}
use of spacesettlers.utilities.Vector2D in project spacesettlers by amymcgovern.
the class Toroidal2DPhysics method isPathClearOfObstructions.
/**
* Check to see if following a straight line path between two given locations would result in a collision with a provided set of obstructions
*
* @author Andrew and Thibault
*
* @param startPosition the starting location of the straight line path
* @param goalPosition the ending location of the straight line path
* @param obstructions an Set of AbstractObject obstructions (i.e., if you don't wish to consider mineable asteroids or beacons obstructions)
* @param freeRadius used to determine free space buffer size
* @return Whether or not a straight line path between two positions contains obstructions from a given set
*/
public boolean isPathClearOfObstructions(Position startPosition, Position goalPosition, Set<AbstractObject> obstructions, int freeRadius) {
// Shortest straight line path from startPosition to goalPosition
Vector2D pathToGoal = findShortestDistanceVector(startPosition, goalPosition);
// Distance of straight line path
double distanceToGoal = pathToGoal.getMagnitude();
// Boolean showing whether or not the path is clear
boolean pathIsClear = true;
// Calculate distance between obstruction center and path (including buffer for ship movement)
// Uses hypotenuse * sin(theta) = opposite (on a right hand triangle)
// Vector from start position to obstruction
Vector2D pathToObstruction;
// Angle between vector from start position to obstruction
double angleBetween;
// Loop through obstructions
for (AbstractObject obstruction : obstructions) {
// If the distance to the obstruction is greater than the distance to the end goal, ignore the obstruction
pathToObstruction = findShortestDistanceVector(startPosition, obstruction.getPosition());
if (pathToObstruction.getMagnitude() > distanceToGoal) {
continue;
}
// Ignore angles > 90 degrees
angleBetween = Math.abs(pathToObstruction.angleBetween(pathToGoal));
if (angleBetween > Math.PI / 2) {
continue;
}
// Compare distance between obstruction and path with buffer distance
if (pathToObstruction.getMagnitude() * Math.sin(angleBetween) < obstruction.getRadius() + freeRadius * 1.5) {
pathIsClear = false;
break;
}
}
return pathIsClear;
}
use of spacesettlers.utilities.Vector2D in project spacesettlers by amymcgovern.
the class Toroidal2DPhysics method findShortestDistanceVector.
/**
* Finds the shortest distance in toroidal space. Returns a vector
* pointing from the start to the target location and getMagnitude can be used
* to find the distance and the angle.
*
* @param location1
* @param location2
* @param width
* @param height
* @param halfWidth
* @param halfHeight
* @return
*/
private Vector2D findShortestDistanceVector(Position location1, Position location2, float width, float height, float halfWidth, float halfHeight) {
double x = location2.getX() - location1.getX();
double y = location2.getY() - location1.getY();
if (x > halfWidth) {
if (y > halfHeight) {
return new Vector2D(x - width, y - height);
} else if (y < -halfHeight) {
return new Vector2D(x - width, y + height);
} else {
return new Vector2D(x - width, y);
}
} else if (x < -halfWidth) {
if (y > halfHeight) {
return new Vector2D(x + width, y - height);
} else if (y < -halfHeight) {
return new Vector2D(x + width, y + height);
} else {
return new Vector2D(x + width, y);
}
} else if (y > halfHeight) {
return new Vector2D(x, y - height);
} else if (y < -halfHeight) {
return new Vector2D(x, y + height);
} else {
return new Vector2D(x, y);
}
}
use of spacesettlers.utilities.Vector2D 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);
}
Aggregations