Jump in Projectile

This commit is contained in:
Leen Dereu
2018-05-11 14:49:33 +02:00
parent 688bcd037a
commit 99450efe5f

View File

@@ -1,5 +1,7 @@
package worms.model; package worms.model;
import static java.lang.Math.*;
import worms.util.Coordinate; import worms.util.Coordinate;
public abstract class Projectile extends GameObject { public abstract class Projectile extends GameObject {
@@ -17,7 +19,7 @@ public abstract class Projectile extends GameObject {
public static double calcRadius(double mass) { public static double calcRadius(double mass) {
return Math.pow(((mass / 1000.0 / rho) * (3.0 / (4.0 * Math.PI))), 1.0 / 3.0); return pow(((mass / 1000.0 / rho) * (3.0 / (4.0 * PI))), 1.0 / 3.0);
} }
public double getForce() { public double getForce() {
@@ -63,30 +65,88 @@ public abstract class Projectile extends GameObject {
!getWorld().isAdjacent(location, radius)); !getWorld().isAdjacent(location, radius));
} }
public static final double G = 5.0;
public static final double FORCE_TIME = 0.5;
public Coordinate getJumpStep(double elapsedTime) { public Coordinate getJumpStep(double elapsedTime) {
// TODO zie naar worm hoe dit moet, implementatie moet wel anders! // TODO zie naar worm hoe dit moet, implementatie moet wel anders!
if (Double.isNaN(elapsedTime) || elapsedTime > this.getJumpTime(jumpTimeStep) || elapsedTime < 0) // DONE?
if (Double.isNaN(elapsedTime) || elapsedTime > this.getJumpTime(elapsedTime) || elapsedTime < 0)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
double velocity = this.jumpVelocity(); double velocity = this.jumpVelocity();
return Coordinate.create(getLocation().getX() + velocity * cos(getOrientation()) * elapsedTime,
getLocation().getY() + velocity * sin(getOrientation()) * elapsedTime - 0.5 * G * pow(elapsedTime, 2) );
return null;
} }
public double jumpVelocity() { private double jumpVelocity() {
return -1; //wat met de action points?
double force = 5 * Worm.getActionPoints() + getMass() * G;
return force/getMass() * FORCE_TIME;
} }
public double getJumpTime(double jumpTimeStep) { private double getJumpTime(double jumpTimeStep) {
// TODO zie naar worm hoe dit moet, implementatie moet wel anders! // TODO zie naar worm hoe dit moet, implementatie moet wel anders!
return 0.0; //wat met parameter jumpTimeStep?
double v = jumpVelocity();
double time = 0;
double a = getOrientation();
Coordinate loc = getLocation();
World world = getWorld();
double radius = getRadius();
Coordinate newLoc;
while (true) {
time += 0.05;
double x = loc.getX() + v * time * cos(a);
double y = loc.getY() + v * time * sin(a) - (G * time * time) / 2.0;
newLoc = Coordinate.create(x, y);
if (! world.isPassable(newLoc, radius) || x < 0 || y < 0 || x > world.getWidth() || y > world.getHeight()) {
while (true) {
time -= 0.01;
newLoc = Coordinate.create(loc.getX() + v * time * cos(a), loc.getY() + v * time * sin(a) - (G * time * time) / 2.0);
if (newLoc.getX() < 0 || newLoc.getY() < 0 || newLoc.getX() > getWorld().getWidth() ||
newLoc.getY() > world.getHeight() || world.isAdjacent(newLoc, radius)) {
break;
}
}
break;
}
if ((int) round(time) == 10) {
throw new RuntimeException();
}
} }
public void jump() { return time;
}
private boolean canJump() {
// wat met action points.
return Worm.getActionPoints() > 0 && getOrientation() < PI;
}
public void jump() throws IllegalStateException {
// TODO zie naar worm hoe dit moet, implementatie moet wel anders! // TODO zie naar worm hoe dit moet, implementatie moet wel anders!
// wat meegeven met getJumpTime() ?
// wat met action points?
if (!canJump())
throw new IllegalStateException();
double v = jumpVelocity();
double t = getJumpTime();
double a = getOrientation();
Coordinate newLocation = Coordinate.create(getLocation().getX() + v * t * cos(a), getLocation().getY() + v * t * sin(a) - (G * t * t) / 2.0);
if (! isValidLocation(newLocation)) {
terminate();
return;
}
setLocation(newLocation);
Worm.setActionPoints(0);
} }
} }