diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9efa837..ee1f9a5 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -4,11 +4,15 @@ + \ No newline at end of file diff --git a/Background.png b/Background.png new file mode 100644 index 0000000..381fc06 Binary files /dev/null and b/Background.png differ diff --git a/src/main/java/CamPuffer.java b/src/main/java/CamPuffer.java index 2a6782f..8f7fc49 100644 --- a/src/main/java/CamPuffer.java +++ b/src/main/java/CamPuffer.java @@ -11,7 +11,7 @@ public class CamPuffer implements Runnable { static { try { - System.load("C:\\Users\\paul3\\OneDrive\\Dokumente\\Studium BME\\Semester 3\\Interaktion\\GTCar V3\\opencv\\build\\java\\x64\\opencv_java4100.dll"); + System.load("C:\\Users\\david\\Documents\\Uni\\GTCar\\opencv\\build\\java\\x64\\opencv_java4100.dll"); System.out.println("OpenCV manuell geladen!"); } catch (UnsatisfiedLinkError e) { System.err.println("Fehler beim Laden der OpenCV-Bibliothek: " + e.getMessage()); @@ -98,7 +98,9 @@ public class CamPuffer implements Runnable { public Mat getCurrentMat() { synchronized (frameBuffer) { if (!frameBuffer.isEmpty()) { - return frameBuffer.pollLast().clone(); // Rückgabe des neuesten Frames + Mat resizedFrame = new Mat(); + Imgproc.resize(frameBuffer.pollLast().clone(), resizedFrame, new Size(1792, 1024)); + return resizedFrame; // Rückgabe des neuesten Frames } } return new Mat(); // Rückgabe eines leeren Frames, falls der Puffer leer ist @@ -124,4 +126,4 @@ public class CamPuffer implements Runnable { System.out.println("Kamera geöffnet und eingerichtet"); return capture; } -} +} \ No newline at end of file diff --git a/src/main/java/GameLogic.java b/src/main/java/GameLogic.java index 37562e1..04e8ac8 100644 --- a/src/main/java/GameLogic.java +++ b/src/main/java/GameLogic.java @@ -9,6 +9,7 @@ public class GameLogic extends PApplet { PApplet.main("GameLogic"); } + private static final int MAX_TRAIL_SIZE = 100; // Maximale Größe des Trails private Queue trail = new LinkedList<>(); private Point testPoint = new Point(200, 200); @@ -17,29 +18,35 @@ public class GameLogic extends PApplet { } public void setup() { - noLoop(); // Zeichnen nur bei Bedarf fillTrail(); // Beispielpfad erstellen - //boolean collision = doCheck(trail, testPoint); - System.out.println("Collision detected: "); + noLoop(); // Zeichnen nur bei Bedarf } private void fillTrail() { - // Beispielhafte Füllung der Spur - for (int i = 0; i < 100; i++) { + // Beispielhafte Füllung der Spur (maximale Größe beachten) + for (int i = 0; i < MAX_TRAIL_SIZE; i++) { trail.add(new int[]{50 + i * 5, 300}); // Punkte entlang der x-Achse } } - public boolean doCheckExtern(Queue trail, Point point){ + // Methode zum Hinzufügen eines neuen Punkts, begrenzt auf MAX_TRAIL_SIZE + public void addToTrail(int x, int y) { + if (trail.size() >= MAX_TRAIL_SIZE) { + trail.poll(); // Entferne das älteste Element, wenn die maximale Größe erreicht ist + } + trail.add(new int[]{x, y}); + } + + public boolean doCheckExtern(Queue trail, Point point) { return doCheck(trail, point); } public boolean doCheck(Queue trail, Point point) { int particleSize = 10; - // Zeichne den Pfad auf das Canvas + // Zeichne den Pfad auf das Canvas nur, wenn nötig background(255); // Weißer Hintergrund - for (int[] coordinates : new LinkedList<>(trail)) { + for (int[] coordinates : trail) { int x = coordinates[0]; int y = coordinates[1]; fill(0); // Schwarz diff --git a/src/main/java/GameLoop.java b/src/main/java/GameLoop.java index 3164884..a3577f3 100644 --- a/src/main/java/GameLoop.java +++ b/src/main/java/GameLoop.java @@ -68,6 +68,12 @@ public class GameLoop { } */ //draw(); + + try{ + wait(50); + } + catch (Exception e){ + } } } @@ -90,6 +96,5 @@ public class GameLoop { }*/ private void draw(){ - } -} +} \ No newline at end of file diff --git a/src/main/java/GraphicsProgram.java b/src/main/java/GraphicsProgram.java index 9813197..6aca883 100644 --- a/src/main/java/GraphicsProgram.java +++ b/src/main/java/GraphicsProgram.java @@ -6,18 +6,11 @@ import java.util.Queue; public class GraphicsProgram extends PApplet { - PImage imgBlue; - PImage imgRed; + private Queue playerTrails = new LinkedList<>(); - Player player1; - Player player2; - - private final int TRAIL_LENGTH = 30; - - public GraphicsProgram(Player player1, Player player2) { - this.player1 = player1; // Beispiel: Initialgröße setzen - this.player2 = player2; - } + PImage img1; + PImage img2; + PImage backgroundImage; public static void main(String[] args) { PApplet.main("GraphicsProgram"); @@ -27,49 +20,44 @@ public class GraphicsProgram extends PApplet { fullScreen(); } - public void setup(){ - imgBlue = loadImage("TrailBlue.png"); - imgRed = loadImage("TrailRed.png"); - background(0); + public void setup() { + backgroundImage = loadImage("Background.png"); + backgroundImage.resize(1792, 1024); + + img1 = loadImage("TrailBlue.png"); + img2 = loadImage("TrailRed.png"); } - public void draw(){ - drawPlayer(0); - drawPlayer(1); - } - public void drawPlayer(int playerID){ + public void draw() { + image(backgroundImage, 0, 0); // Hintergrund zeichnen - int particleSize = 5; - Queue trail; + int particleSizeP1 = 30; + int particleSizeP2 = 30; - if(playerID == 0){ - trail = player1.getTrail(); - } else{ - trail = player2.getTrail(); - } - while(!trail.isEmpty()) { - int[] koordinaten = trail.poll(); - int x = koordinaten[0]; - int y = koordinaten[1]; - System.out.println(); + // Kopie von trailPlayer1 erstellen und durch die Kopie iterieren + for (int[] koordinates : new LinkedList<>(playerTrails)) { + int x1 = koordinates[0]; + int y1 = koordinates[1]; + int x2 = koordinates[2]; + int y2 = koordinates[3]; + + fill(0, 0, 255); + rect(x1, y1, particleSizeP1, particleSizeP1); + //image(img1, x1, y1, particleSizeP1, particleSizeP1); + + fill(255, 0, 0); + rect(x2, y2, particleSizeP2, particleSizeP2); + //image(img2, x2, y2, particleSizeP2, particleSizeP2); + - if(playerID == 0){ - image(imgBlue, x, y, particleSize, particleSize); - } else if (playerID == 1) { - image(imgRed, x, y, particleSize, particleSize); - }else{ - fill(0, 255, 0); - rect(x, y, particleSize, particleSize); - } - particleSize ++; } } - public void drawEvent(){ - + public void updateTrail(Queue trails){ + this.playerTrails = trails; } } diff --git a/src/main/java/ImgAnalyzer.java b/src/main/java/ImgAnalyzer.java index d9358b1..7f84393 100644 --- a/src/main/java/ImgAnalyzer.java +++ b/src/main/java/ImgAnalyzer.java @@ -4,10 +4,13 @@ import org.opencv.highgui.HighGui; import org.opencv.imgproc.Imgproc; import org.opencv.imgproc.Moments; import org.opencv.videoio.Videoio; +import processing.core.PApplet; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; import java.awt.Point; +import java.util.Queue; public class ImgAnalyzer { @@ -15,13 +18,20 @@ public class ImgAnalyzer { private Point currentPositionP1 = new Point(0, 0); private Point currentPositionP2 = new Point(0, 0); + Queue trails = new LinkedList<>(); + private final int TRAIL_LENGTH = 200; private final CamPuffer camPuffer; + private GraphicsProgram playerGraphics; public ImgAnalyzer(CamPuffer cP, int threshold) { THV = threshold; camPuffer = cP; System.loadLibrary(Core.NATIVE_LIBRARY_NAME); + + playerGraphics = new GraphicsProgram(); + String[] args ={"GraphicsProgram"}; + PApplet.runSketch(args, playerGraphics); } public static void main(String[] args) { @@ -44,37 +54,45 @@ public class ImgAnalyzer { Mat thresholdFrame = new Mat(); while (true) { - try { - currentImg = camPuffer.getCurrentMat(); + try { + currentImg = camPuffer.getCurrentMat(); - if(currentImg.elemSize() != 0) { + if(currentImg.elemSize() != 0) { - // In Graustufen konvertieren - Imgproc.cvtColor(currentImg, grayFrame, Imgproc.COLOR_BGR2GRAY); + // In Graustufen konvertieren + Imgproc.cvtColor(currentImg, grayFrame, Imgproc.COLOR_BGR2GRAY); - // Threshold anwenden - Imgproc.threshold(grayFrame, thresholdFrame, THV, 255, Imgproc.THRESH_BINARY); - - //Ausgabe des Bildes zum Prüfen - HighGui.imshow("ThunfischHighGui", thresholdFrame); - HighGui.waitKey(1); - //Ende Bildausgabe */ - } - - } catch (Exception e) { + // Threshold anwenden + Imgproc.threshold(grayFrame, thresholdFrame, THV, 255, Imgproc.THRESH_BINARY); + //Ausgabe des Bildes zum Prüfen + HighGui.imshow("ThunfischHighGui", thresholdFrame); + HighGui.waitKey(1); + //Ende Bildausgabe */ } - if (currentImg != null) { - List positions = trackPositions(thresholdFrame); + } catch (Exception e) { - if (positions.size() >= 2) { - currentPositionP1 = positions.get(0); - currentPositionP2 = positions.get(1); - System.out.println("currentPositionP1: " + currentPositionP1 + " currentPositionP2: " + currentPositionP2); - } + } + if (currentImg != null) { + List positions = trackPositions(thresholdFrame); + + if (positions.size() >= 2) { + currentPositionP1 = positions.get(0); + currentPositionP2 = positions.get(1); + //System.out.println("currentPositionP1: " + currentPositionP1 + " currentPositionP2: " + currentPositionP2); } + + trails.add(new int[]{currentPositionP1.x, currentPositionP1.y, currentPositionP2.x, currentPositionP2.y}); + if(trails.size() > TRAIL_LENGTH){ + trails.poll(); + } + System.out.println(trails.size()); + playerGraphics.updateTrail(trails); + playerGraphics.draw(); + + } } } @@ -111,4 +129,4 @@ public class ImgAnalyzer { return currentPositionP2; } -} +} \ No newline at end of file diff --git a/src/main/java/Player.java b/src/main/java/Player.java index 3edef2d..f74f8b1 100644 --- a/src/main/java/Player.java +++ b/src/main/java/Player.java @@ -1,3 +1,4 @@ +import java.util.Arrays; import java.util.LinkedList; import java.util.Queue; @@ -20,6 +21,7 @@ public class Player { public Player(int id, String finalIpAddress, int finalPortNr) { this.ID = id; arduinoCommunicator = new ArduinoCommunication(finalIpAddress, finalPortNr); + trail.add(new int[]{0,0}); } public int getX(){ @@ -38,14 +40,14 @@ public class Player { this.y = y; } - private void addToTrail(int x, int y) { + private void addToTrail(int x, int y) { trail.add(new int[]{x, y}); - if(trail.size() > TRAIL_LENGTH){ - trail.poll(); + if (trail.size() > TRAIL_LENGTH) { + trail.poll(); // Entfernt das älteste Element } - } + public Queue getTrail(){ return trail; }