94 lines
1.9 KiB
Java
94 lines
1.9 KiB
Java
package break_out.model;
|
|
|
|
import break_out.Constants;
|
|
import break_out.model.Position;
|
|
|
|
/**
|
|
* This class represent a two dimensional vector.
|
|
*
|
|
* @author I. Schumacher; modified by Gruppe 175 (Moritz Henseleit, Ruben Meyer)
|
|
*/
|
|
public class Vector2D implements IVector2D {
|
|
|
|
/**
|
|
* The x part of the vector
|
|
*/
|
|
private double dx;
|
|
|
|
/**
|
|
* The y part of the vector
|
|
*/
|
|
private double dy;
|
|
|
|
/**
|
|
* This constructor creates a new vector with the given x and y parts.
|
|
*
|
|
* @param dx the delta x part for the new vector
|
|
* @param dy the delty y part for the new vector
|
|
*/
|
|
public Vector2D(double dx, double dy) {
|
|
this.dx = dx;
|
|
this.dy = dy;
|
|
}
|
|
|
|
/**
|
|
* This constructor creates a new vector based on the given positions start and end.
|
|
*
|
|
* @param start first position
|
|
* @param end second position
|
|
*/
|
|
public Vector2D(Position start, Position end) {
|
|
// vector(AB) = (B.x - A.x, B.y - A.y)
|
|
this.dx = end.getX() - start.getX();
|
|
this.dy = end.getY() - start.getY();
|
|
}
|
|
|
|
/**
|
|
* Getter for the dx-part
|
|
*
|
|
* @return dx The dx part of this vector
|
|
*/
|
|
public double getDx() {
|
|
return dx;
|
|
}
|
|
|
|
/**
|
|
* Setter for the dx-part
|
|
*
|
|
* @param dx The new dx part of this vector
|
|
*/
|
|
public void setDx(double dx) {
|
|
this.dx = dx;
|
|
}
|
|
|
|
/**
|
|
* Getter for the dy-part
|
|
*
|
|
* @return dy The dy part of this vector
|
|
*/
|
|
public double getDy() {
|
|
return dy;
|
|
}
|
|
|
|
/**
|
|
* Setter for the dy-part
|
|
*
|
|
* @param dy The new dy part of this vector
|
|
*/
|
|
public void setDy(double dy) {
|
|
this.dy = dy;
|
|
}
|
|
|
|
/**
|
|
* Rescale part
|
|
*/
|
|
public void rescale() {
|
|
// calc unit vector and set it
|
|
double vectorLength = Math.sqrt(Math.pow(getDx(), 2) + Math.pow(getDy(), 2)); //using the square root of x and y
|
|
|
|
// rescaling only needed, if value is not zero
|
|
if(getDx() != 0) setDx((1 / vectorLength) * getDx() * Constants.BALL_SPEED);
|
|
if(getDy() != 0) setDy((1 / vectorLength) * getDy() * Constants.BALL_SPEED);
|
|
}
|
|
}
|