use of spacesettlers.utilities.Vector2D in project spacesettlers by amymcgovern.
the class MoveAction method pdControlMoveToGoal.
/**
* Accelerate to the goal location using pd control. Assumes you are already pointed at the goal. Returns
* the linear/translational acceleration needed to move to the goal.
*
* @param goalLoc location that you are trying to get to
* @param currentLoc your current location
* @param goalVelocity velocity you want to be at when you reach the goal location
* @return the accleration vector needed to move to the goal
*/
public Vector2D pdControlMoveToGoal(Toroidal2DPhysics space, Position goalLoc, Position currentLoc, Vector2D goalVelocity) {
// take care of wrap-around
Vector2D shortestDist = space.findShortestDistanceVector(currentLoc, goalLoc);
double xError = shortestDist.getXValue();
double yError = shortestDist.getYValue();
// System.out.println("xerror is " + xError + " yError is " + yError);
// System.out.println("Goal velocity is " + goalVelocity);
// System.out.println("Current velocity is " + currentLoc.getTranslationalVelocity());
double velocityErrorX = (goalVelocity.getXValue() - currentLoc.getTranslationalVelocityX());
double velocityErrorY = (goalVelocity.getYValue() - currentLoc.getTranslationalVelocityY());
// System.out.println("Velocity error is " + velocityErrorX + " ," + velocityErrorY);
double xAccel = pdControlTranslate(xError, velocityErrorX);
double yAccel = pdControlTranslate(yError, velocityErrorY);
// System.out.println("Translation accel is " + xAccel + ", " + yAccel);
return new Vector2D(xAccel, yAccel);
}
use of spacesettlers.utilities.Vector2D in project spacesettlers by amymcgovern.
the class MoveAction method getOrientationError.
/**
* Returns the error in orientation space (separate function so we can use it to decide
* when it has reached the goal)
*
* @param space
* @param goalLoc
* @param currentLoc
* @return
*/
private double getOrientationError(Toroidal2DPhysics space, Position goalLoc, Position currentLoc) {
Vector2D shortdist = space.findShortestDistanceVector(currentLoc, goalLoc);
double angle = shortdist.getAngle();
double error = (angle - currentLoc.getOrientation());
if (error > Math.PI) {
error -= 2 * Math.PI;
} else if (error < -Math.PI) {
error += 2 * Math.PI;
}
return error;
}
use of spacesettlers.utilities.Vector2D in project spacesettlers by amymcgovern.
the class MoveAction method getMovement.
/**
* Move the ship to the goal by turning and moving to the goal at the same time.
*/
public Movement getMovement(Toroidal2DPhysics space, Ship ship) {
// isOrientedToGoal, isAtGoal, isOrientedAtGoal, isOrientingAtGoal
Movement movement = new Movement();
if (isFinished) {
return movement;
}
// set the angular and translational velocity at the same time
double angularAccel = pdControlOrientToGoal(space, targetLocation, ship.getPosition(), 0);
movement.setAngularAccleration(angularAccel);
Vector2D goalAccel = pdControlMoveToGoal(space, targetLocation, ship.getPosition(), targetVelocity);
movement.setTranslationalAcceleration(goalAccel);
// figure out if it has reached the goal
if ((goalAccel.getMagnitude() < TARGET_REACHED_ACCEL) || (space.findShortestDistance(targetLocation, ship.getPosition()) < TARGET_REACHED_ERROR)) {
isFinished = true;
}
return movement;
}
use of spacesettlers.utilities.Vector2D in project spacesettlers by amymcgovern.
the class MoveActionWithOrientation method getMovement.
/**
* Move the ship to the goal by turning and moving to the goal at the same time.
*/
public Movement getMovement(Toroidal2DPhysics space, Ship ship) {
// isOrientedToGoal, isAtGoal, isOrientedAtGoal, isOrientingAtGoal
Movement movement = new Movement();
if (isFinished) {
return movement;
}
// set the angular and translational velocity at the same time
double angularAccel = this.pdControlOrientToGoal(space, targetLocation, ship.getPosition(), 0);
movement.setAngularAccleration(angularAccel);
Vector2D goalAccel = super.pdControlMoveToGoal(space, targetLocation, ship.getPosition(), targetVelocity);
movement.setTranslationalAcceleration(goalAccel);
// figure out if it has reached the goal
if ((goalAccel.getMagnitude() < TARGET_REACHED_ACCEL) || (space.findShortestDistance(targetLocation, ship.getPosition()) < TARGET_REACHED_ERROR)) {
isFinished = true;
}
return movement;
}
use of spacesettlers.utilities.Vector2D in project spacesettlers by amymcgovern.
the class CollisionHandler method elasticCollision2D.
/**
* 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 elasticCollision2D(AbstractObject object1, AbstractObject object2, Toroidal2DPhysics space) {
// handle overlapping objects
adjustCentersAtCollision(object1, object2, space);
// get the masses
double m1 = object1.getMass();
double m2 = object2.getMass();
// now get the vector from the first to the second, get the unit normal and tangent
Vector2D distanceVec = space.findShortestDistanceVector(object1.getPosition(), object2.getPosition());
Vector2D unitNormal = distanceVec.getUnitVector();
Vector2D unitTangent = new Vector2D(-unitNormal.getYValue(), unitNormal.getXValue());
// get the velocity vectors
Vector2D velocity1 = object1.getPosition().getTranslationalVelocity();
Vector2D velocity2 = object2.getPosition().getTranslationalVelocity();
// get the scalars in each direction
double u1 = velocity1.dot(unitNormal);
double u2 = velocity2.dot(unitNormal);
double t1 = velocity1.dot(unitTangent);
double t2 = velocity2.dot(unitTangent);
// elastically collide in the one dimension
CollisionData result = elasticCollision1D(u1, m1, u2, m2);
// now get it back to the original space
Vector2D vel1Normal = unitNormal.multiply(result.v1);
Vector2D vel2Normal = unitNormal.multiply(result.v2);
Vector2D vel1Tangent = unitTangent.multiply(t1);
Vector2D vel2Tangent = unitTangent.multiply(t2);
// add the normal and tangential parts
Vector2D newVelocity1 = vel1Normal.add(vel1Tangent);
Vector2D newVelocity2 = vel2Normal.add(vel2Tangent);
object1.getPosition().setTranslationalVelocity(newVelocity1);
object2.getPosition().setTranslationalVelocity(newVelocity2);
}
Aggregations