From 35fb734030dbb97dcc25b4fff9e9fb943ea05678 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Mon, 16 Apr 2018 15:42:53 +0200 Subject: [PATCH] fixed jump --- OGP1718-Worms/src/worms/model/Worm.java | 107 +++++++++++++++++------- 1 file changed, 77 insertions(+), 30 deletions(-) diff --git a/OGP1718-Worms/src/worms/model/Worm.java b/OGP1718-Worms/src/worms/model/Worm.java index 3f3dcb1..2e41356 100644 --- a/OGP1718-Worms/src/worms/model/Worm.java +++ b/OGP1718-Worms/src/worms/model/Worm.java @@ -526,24 +526,6 @@ public class Worm extends GameObject { break; } } -// while(getDistance(currentLocation, nextLoc) < maxDistance) { -// nextLoc = Coordinate.create(round((nextLoc.getX() + 0.1 * cos(direction)) * 100.0) / 100.0, -// round((nextLoc.getY() + 0.1 * sin(direction)) * 100.0) / 100.0); -// if (!world.isPassable(nextLoc.toArray(), radius)) { -// break; -// } -// } -// double max = radius * 0.1 + 0.001; -// while(!world.isAdjacent(nextLoc.toArray(), radius)) { -// max -= 0.01; -// if (max < 0) { -// nextLoc = currentLocation; -// break; -// } -// nextLoc = Coordinate.create(round((nextLoc.getX() - 0.01 * cos(direction)) * 100.0) / 100.0, -// round((nextLoc.getY() - 0.01 * sin(direction)) * 100.0) / 100.0); -// } - //if (getDistance(currentLocation, prevLoc) > getDistance(currentLocation, nextLoc)) return prevLoc; return nextLoc; } @@ -673,14 +655,20 @@ public class Worm extends GameObject { if (!canJump()) throw new IllegalStateException(); - double distance = jumpDistance(this.jumpVelocity()); - Coordinate newLocation = Coordinate.create( getLocation().getX() + distance, getLocation().getY()); - if (getWorld().isAdjacent(newLocation, getRadius()) && canHaveAsJumpDistance(distance)) { - if (!isValidLocation(newLocation)) terminate(); - setLocation(newLocation); - setActionPoints(0); + double v = jumpVelocity(); + double t = calcJumpTime(); + 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); + setActionPoints(0); } /** @@ -695,10 +683,7 @@ public class Worm extends GameObject { */ public double jumpTime() { - if (getOrientation() >= PI || getActionPoints() == 0) - throw new IllegalStateException(); - - return (jumpDistance(jumpVelocity()) / (jumpVelocity() * cos(getOrientation()))); + return calcJumpTime(); } /** @@ -719,10 +704,72 @@ public class Worm extends GameObject { if (Double.isNaN(deltaTime) || deltaTime > this.jumpTime() || deltaTime < 0) throw new IllegalArgumentException(); - return Coordinate.create(getLocation().getX() + this.jumpVelocity() * cos(getOrientation()) * deltaTime, - getLocation().getY() + this.jumpVelocity() * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2)); + double velocity = this.jumpVelocity(); + + return Coordinate.create(getLocation().getX() + velocity * cos(getOrientation()) * deltaTime, + getLocation().getY() + velocity * sin(getOrientation()) * deltaTime - 0.5 * G * pow(deltaTime, 2)); } + private double calcJumpTime(){ + double v = jumpVelocity(); + + double t = 0; + double a = getOrientation(); + Coordinate loc = getLocation(); + World world = getWorld(); + double radius = getRadius(); + Coordinate newLoc; + + while(true) { + + t += 0.05; + double x = loc.getX() + v * t * cos(a); + double y = loc.getY() + v * t * sin(a) - (G * t * t) / 2.0; + newLoc = Coordinate.create(x, y); + if (!world.isPassable(newLoc, radius) || x < 0 || y < 0 || x > world.getWidth() || y > world.getHeight()) { + + while (true) { + + t -= 0.01; + newLoc = Coordinate.create(loc.getX() + v * t * cos(a), loc.getY() + v * t * sin(a) - (G * t * t) / 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(t) == 10) { + throw new RuntimeException(); + } + } + return t; + } + +// private Coordinate getJumpLocation() { +// +// double v = jumpVelocity(); +// double t = calcJumpTime(); +// double a = getOrientation(); +// double radius = getRadius(); +// World world = getWorld(); +// Coordinate loc = getLocation(); +// Coordinate newLoc; +// +// while (true) { +// +// t -= 0.01; +// newLoc = Coordinate.create(loc.getX() + v * t * cos(a), loc.getY() + v * t * sin(a) - (G * t * t) / 2.0); +// +// if (newLoc.getX() < 0 || newLoc.getY() < 0 || +// newLoc.getX() > getWorld().getWidth() || newLoc.getY() > world.getHeight()) { +// return newLoc; +// } +// if (world.isAdjacent(newLoc, radius)) return newLoc; +// } +// } + /** * Return a boolean whether the worm can jump or not *