implemented jump and small changes
This commit is contained in:
BIN
docs/28641484_1576783732442687_76036637_o (1).jpg
Normal file
BIN
docs/28641484_1576783732442687_76036637_o (1).jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 908 KiB |
@@ -3,7 +3,7 @@ import be.kuleuven.cs.som.annotate.*;
|
||||
|
||||
|
||||
/**
|
||||
* A tuple implementation
|
||||
* A tuple (pair) implementation
|
||||
*
|
||||
* @param <T1> first element type
|
||||
* @param <T2> second element type
|
||||
|
@@ -6,7 +6,8 @@ public class Worm {
|
||||
|
||||
|
||||
// Location
|
||||
private double[] location;
|
||||
private Tuple<Double, Double> location;
|
||||
private Tuple<Double, Double> oldLocation;
|
||||
|
||||
// Orientation
|
||||
private double orientation;
|
||||
@@ -22,7 +23,7 @@ public class Worm {
|
||||
private int maxPoints;
|
||||
|
||||
// name
|
||||
private final String name;
|
||||
private String name;
|
||||
|
||||
//===================================================================================
|
||||
// endregion
|
||||
@@ -38,7 +39,7 @@ public class Worm {
|
||||
* @param name ...
|
||||
* @param radius ...
|
||||
*/
|
||||
public Worm(double[] location, double orientation, String name, double radius) {
|
||||
public Worm(Tuple<Double, Double> location, double orientation, String name, double radius) {
|
||||
|
||||
this(location, orientation, name, radius, 0.25);
|
||||
}
|
||||
@@ -52,7 +53,7 @@ public class Worm {
|
||||
* @param minRadius ...
|
||||
* @throws IllegalArgumentException ...
|
||||
*/
|
||||
public Worm(double[] location, double orientation, String name, double radius, double minRadius)
|
||||
public Worm(Tuple<Double, Double> location, double orientation, String name, double radius, double minRadius)
|
||||
throws IllegalArgumentException {
|
||||
|
||||
setLocation(location);
|
||||
@@ -81,13 +82,13 @@ public class Worm {
|
||||
//===================================================================================
|
||||
|
||||
|
||||
public double[] getLocation() {
|
||||
public Tuple<Double, Double> getLocation() {
|
||||
return this.location;
|
||||
}
|
||||
|
||||
private void setLocation(double[] location) throws IllegalArgumentException {
|
||||
private void setLocation(Tuple<Double, Double> location) throws IllegalArgumentException {
|
||||
|
||||
if (location == null || location.length != 2)
|
||||
if (location.equals(null))
|
||||
throw new IllegalArgumentException("Illegal value for location"); // TODO add decent exception msg
|
||||
this.location = location;
|
||||
}
|
||||
@@ -194,6 +195,16 @@ public class Worm {
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
|
||||
int validName = isValidName(name);
|
||||
if (validName != -1)
|
||||
throw new IllegalNameException(validName, name);
|
||||
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name ...
|
||||
* @return ...
|
||||
@@ -247,10 +258,8 @@ public class Worm {
|
||||
|
||||
double distanceX = this.radius * Math.cos(this.orientation);
|
||||
double distanceY = this.radius * Math.sin(this.orientation);
|
||||
this.location = new double[] {
|
||||
this.location[0] + numberSteps * distanceX,
|
||||
this.location[1] + numberSteps * distanceY
|
||||
};
|
||||
this.location = Tuple.create(this.location.item1 + numberSteps * distanceX,
|
||||
this.location.item2 + numberSteps * distanceY);
|
||||
subtractPoints(cost);
|
||||
|
||||
}
|
||||
@@ -288,10 +297,46 @@ public class Worm {
|
||||
// region Jump
|
||||
//===================================================================================
|
||||
|
||||
private final double G = 5.0;
|
||||
private final double FORCE_TIME = 0.5;
|
||||
|
||||
public void jump() {
|
||||
|
||||
if (!canJump())
|
||||
throw new IllegalStateException();
|
||||
|
||||
this.oldLocation = this.location;
|
||||
this.location = Tuple.create(location.item1 + jumpDistance(jumpVelocity()), location.item2);
|
||||
|
||||
this.points = 0;
|
||||
}
|
||||
|
||||
private boolean canJump() {
|
||||
return this.points > 0 && this.orientation < Math.PI;
|
||||
}
|
||||
|
||||
public double jumpTime() {
|
||||
|
||||
double v = jumpVelocity();
|
||||
return jumpDistance(v) / (v * Math.cos(this.orientation));
|
||||
}
|
||||
public double jumpStep() {
|
||||
|
||||
return 0.0;
|
||||
}
|
||||
private double jumpDistance(double v) {
|
||||
return (Math.pow(v, 2) * Math.sin(2 * this.orientation)) / this.G;
|
||||
}
|
||||
|
||||
private double jumpVelocity() {
|
||||
|
||||
double force = 5 * this.points + this.mass * G;
|
||||
return (force / this.mass) * FORCE_TIME;
|
||||
}
|
||||
|
||||
|
||||
//===================================================================================
|
||||
// endregion
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user