diff --git a/src/main/java/core/Master.java b/src/main/java/core/Master.java index f739bd8..54a8d2c 100644 --- a/src/main/java/core/Master.java +++ b/src/main/java/core/Master.java @@ -3,6 +3,7 @@ package core; import objects.DebugPos; import objects.ships.BattleShip; import objects.GameObject; +import objects.ships.Turret; import objects.world.Grid; import javax.swing.*; @@ -38,9 +39,9 @@ public class Master extends JPanel { BattleShip battleShip = new BattleShip(Color.DARK_GRAY); BattleShip bs = new BattleShip(70, 10, 5, 80, Color.GREEN); - /*for (int i = 0; i < 10; i++) { + for (int i = 0; i < 10; i++) { bs.addTurret(new Turret(bs, 25, 10 * i + 1, 50, i % 5)); - }*/ + } objects.add(bs); objects.add(battleShip); } diff --git a/src/main/java/core/Vector2D.java b/src/main/java/core/Vector2D.java index 26167fc..2e1ecef 100644 --- a/src/main/java/core/Vector2D.java +++ b/src/main/java/core/Vector2D.java @@ -1,5 +1,6 @@ package core; + /** * A 2-dimensional Vector that can be used to store position or velocity */ @@ -45,7 +46,7 @@ public class Vector2D { } /** - * Mulitply this vector with a simple scalar, modifies this object + * Multiply this vector with a simple scalar, modifies this object * * @param a The scalar * @return this after the multiplication @@ -111,6 +112,9 @@ public class Vector2D { /** * Rotate a point around another point + * + * This method can now be trusted + * * @param center The center of the rotation * @param value The point to be rotated, absolut to the center * @param rotation The rotation angle in radians @@ -121,12 +125,18 @@ public class Vector2D { Vector2D dif = Vector2D.subtract(value, center); - double rotatedX = Math.cos(rotation * rotationDirection) * dif.x; - double rotatedY = Math.sin(rotation * rotationDirection) * dif.x; + rotation = rotationDirection * rotation; + + double rotatedX = dif.x * Math.cos(rotation) - dif.y * Math.sin(rotation); + double rotatedY = dif.x * Math.sin(rotation) + dif.y * Math.cos(rotation); return new Vector2D(rotatedX + center.x, rotatedY + center.y); } + /** + * Copy this object + * @return A copy of this object + */ public Vector2D copy(){ return new Vector2D(x, y); } diff --git a/src/main/java/objects/DebugPos.java b/src/main/java/objects/DebugPos.java index 0a8c248..d423114 100644 --- a/src/main/java/objects/DebugPos.java +++ b/src/main/java/objects/DebugPos.java @@ -17,7 +17,7 @@ public class DebugPos extends GameObject { @Override public void draw(Graphics2D g2d, int w, Master master) { g2d.setPaint(Color.green); - g2d.drawOval((int) (position.x + size.x / 2), (int) (position.y + size.y / 2), (int) size.x, (int) size.y); + g2d.drawOval((int) (position.x - size.x / 2), (int) (position.y - size.y / 2), (int) size.x, (int) size.y); } @Override diff --git a/src/main/java/objects/ships/BattleShip.java b/src/main/java/objects/ships/BattleShip.java index 16b0eef..cd946e3 100644 --- a/src/main/java/objects/ships/BattleShip.java +++ b/src/main/java/objects/ships/BattleShip.java @@ -17,9 +17,9 @@ public class BattleShip extends GameObject { public BattleShip(Color mainColor) { super(20, 20, 5, 40); turrets = new ArrayList<>(); - turrets.add(new Turret(this, 25, 25, 50, 1)); - //turrets.add(new Turret(this, 25, 10, 50, 2)); - //turrets.add(new Turret(this, 25, 70, 50, 2)); + turrets.add(new Turret(this, 25, 25, 50, 3)); + turrets.add(new Turret(this, 25, 10, 50, 2)); + turrets.add(new Turret(this, 25, 70, 50, 2)); this.mainColor = mainColor; } diff --git a/src/main/java/objects/ships/Turret.java b/src/main/java/objects/ships/Turret.java index 35b68c4..6d39aa8 100644 --- a/src/main/java/objects/ships/Turret.java +++ b/src/main/java/objects/ships/Turret.java @@ -92,9 +92,7 @@ public class Turret extends GameObject { lastShot = System.currentTimeMillis(); Vector2D shellVel = new Vector2D(xCenterAbs - msLoc.x, yCenterAbs - msLoc.y).normalized().negative().multiply(SHELL_SPEED); - Vector2D pos = Vector2D.rotateAround(new Vector2D(xCenterAbs, yCenterAbs), new Vector2D(barrelX, frontPosY), rotation, Vector2D.CLOCKWISE); - - master.debugPos(pos); + Vector2D pos = Vector2D.rotateAround(new Vector2D(xCenterAbs, yCenterAbs), new Vector2D(barrelX, frontPosY), rotation, Vector2D.COUNTERCLOCKWISE); master.create(new Shell(pos, new Vector2D(10, 10), shellVel)); } diff --git a/src/test/java/core/Vector2DTest.java b/src/test/java/core/Vector2DTest.java index e8a20a8..973444c 100644 --- a/src/test/java/core/Vector2DTest.java +++ b/src/test/java/core/Vector2DTest.java @@ -41,20 +41,33 @@ class Vector2DTest { } @Test - void rotateAroundTest(){ + void rotateAroundTestSimple(){ Vector2D cent = new Vector2D(0, 0); Vector2D a = new Vector2D(1, 0); - Vector2D exceptedA = new Vector2D(6.123233995736766E-17 ,-1); //floating point precision probably - - Vector2D centB = new Vector2D(1, 1); - Vector2D b = new Vector2D(2, 1); - Vector2D exceptedB = new Vector2D(1 ,2); - - + Vector2D excepted = new Vector2D(0 ,-1); //floating point precision probably Vector2D rotated = Vector2D.rotateAround(cent, a, Math.toRadians(90), Vector2D.CLOCKWISE); - Vector2D rotatedb = Vector2D.rotateAround(centB, b, Math.toRadians(90), Vector2D.COUNTERCLOCKWISE); + assertEquals(round(excepted), round(rotated)); + } - assertEquals(exceptedA, rotated); - assertEquals(exceptedB, rotatedb); + @Test + void rotateAroundTestMovedCenter(){ + Vector2D cent = new Vector2D(1, 1); + Vector2D a = new Vector2D(2, 1); + Vector2D excepted = new Vector2D(1 ,2); + Vector2D rotated = Vector2D.rotateAround(cent, a, Math.toRadians(90), Vector2D.COUNTERCLOCKWISE); + assertEquals(round(excepted), round(rotated)); + } + + @Test + void rotateAroundTestExact(){ + Vector2D cent = new Vector2D(); + Vector2D a = new Vector2D(5, 6); + Vector2D rotated = Vector2D.rotateAround(cent, a, Math.toRadians(120), Vector2D.COUNTERCLOCKWISE); + Vector2D expected = new Vector2D(-7.6962, 1.3301); + assertEquals(round(expected), round(rotated)); + } + + public static Vector2D round(Vector2D a){ + return new Vector2D(Math.round(a.x*10000d)/10000d, Math.round(a.y*10000d)/10000d); } } \ No newline at end of file diff --git a/target/classes/core/Master.class b/target/classes/core/Master.class index a5ff27c..cb7b329 100644 Binary files a/target/classes/core/Master.class and b/target/classes/core/Master.class differ diff --git a/target/classes/objects/ships/Turret.class b/target/classes/objects/ships/Turret.class index 2cc59bb..6b61a94 100644 Binary files a/target/classes/objects/ships/Turret.class and b/target/classes/objects/ships/Turret.class differ