shooting works

This commit is contained in:
nora 2020-12-11 20:29:03 +01:00
parent 0d5397dbe3
commit e2483b30ff
8 changed files with 45 additions and 23 deletions

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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

View file

@ -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;
}

View file

@ -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));
}

View file

@ -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);
}
}

Binary file not shown.