Browse Source

Version 13.12 Fehler: getP1 und getP2 geben 0,0 zurück obwohl bis dahin noch richtige Koordinaten in currentPositionP1 -P2 gespeichert sind

master
David Moeckel 1 week ago
parent
commit
b05c2a8319

+ 13
- 17
.idea/workspace.xml View File

<option name="autoReloadType" value="SELECTIVE" /> <option name="autoReloadType" value="SELECTIVE" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="4d75e996-cee4-4ec6-b802-5b56d0c0eac4" name="Changes" comment="Version 27.11 (&quot;fertiger&quot; Tracker)">
<list default="true" id="4d75e996-cee4-4ec6-b802-5b56d0c0eac4" name="Changes" comment="Version 04.12 &quot;vollständiges Programm mit Fehlern&quot;">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/GameLogic.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/GameLogic.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/GameLogic.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/GameLogic.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/GameLoop.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/GameLoop.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/GameLoop.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/GameLoop.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/GraphicsProgram.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/GraphicsProgram.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/GraphicsProgram.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/GraphicsProgram.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/Player.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/Player.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/Tracker.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/Tracker.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/src/main/java/Tracker.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/Tracker.java" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
</component> </component>
<component name="Git.Settings"> <component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
<option name="UPDATE_TYPE" value="REBASE" />
</component> </component>
<component name="ProjectColorInfo">{ <component name="ProjectColorInfo">{
&quot;associatedIndex&quot;: 8 &quot;associatedIndex&quot;: 8
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent"><![CDATA[{
"keyToString": { "keyToString": {
"Application.GTCar.executor": "Run", "Application.GTCar.executor": "Run",
"Application.GameLogic.executor": "Run",
"Application.GraphicsProgram.executor": "Run", "Application.GraphicsProgram.executor": "Run",
"Application.Main.executor": "Run", "Application.Main.executor": "Run",
"Application.Tracker.executor": "Run", "Application.Tracker.executor": "Run",
"com.codeium.enabled": "true", "com.codeium.enabled": "true",
"git-widget-placeholder": "master", "git-widget-placeholder": "master",
"kotlin-language-version-configured": "true", "kotlin-language-version-configured": "true",
"last_opened_file_path": "C:/Users/david/Documents/Uni/GTCar/processing-4.3/core/library/core.jar",
"last_opened_file_path": "C:/Users/david/Documents/Uni/GTCar/opencv/build/java/opencv-4100.jar",
"node.js.detected.package.eslint": "true", "node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true", "node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)", "node.js.selected.package.eslint": "(autodetect)",
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration name="GameLogic" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="GameLogic" />
<configuration name="GraphicsProgram" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="GraphicsProgram" />
<module name="GTCar_V2_maven" /> <module name="GTCar_V2_maven" />
<method v="2"> <method v="2">
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true"> <configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Main" /> <option name="MAIN_CLASS_NAME" value="Main" />
<module name="GTCar_V2_maven" /> <module name="GTCar_V2_maven" />
<option name="VM_PARAMETERS" value="-Djava.library.path=C:\Users\david\Documents\Uni\GTCar\opencv\build\java\x64" />
<method v="2"> <method v="2">
<option name="Make" enabled="true" /> <option name="Make" enabled="true" />
</method> </method>
</configuration> </configuration>
<list>
<item itemvalue="Application.GTCar" />
<item itemvalue="Application.GraphicsProgram" />
<item itemvalue="Application.Main" />
</list>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="Application.Main" /> <item itemvalue="Application.Main" />
<item itemvalue="Application.GameLogic" />
<item itemvalue="Application.GraphicsProgram" />
</list> </list>
</recent_temporary> </recent_temporary>
</component> </component>
<workItem from="1732108906083" duration="758000" /> <workItem from="1732108906083" duration="758000" />
<workItem from="1732113837570" duration="20534000" /> <workItem from="1732113837570" duration="20534000" />
<workItem from="1733327375447" duration="951000" /> <workItem from="1733327375447" duration="951000" />
<workItem from="1734091457440" duration="10746000" />
</task> </task>
<task id="LOCAL-00001" summary="First Commit Test"> <task id="LOCAL-00001" summary="First Commit Test">
<option name="closed" value="true" /> <option name="closed" value="true" />
<MESSAGE value="Version 27.11 (&quot;fertiger&quot; Tracker)" /> <MESSAGE value="Version 27.11 (&quot;fertiger&quot; Tracker)" />
<option name="LAST_COMMIT_MESSAGE" value="Version 27.11 (&quot;fertiger&quot; Tracker)" /> <option name="LAST_COMMIT_MESSAGE" value="Version 27.11 (&quot;fertiger&quot; Tracker)" />
</component> </component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<breakpoint enabled="true" type="java-exception">
<properties class="java.lang.NullPointerException" package="java.lang" />
<option name="timeStamp" value="1" />
</breakpoint>
</breakpoints>
</breakpoint-manager>
</component>
</project> </project>

+ 30
- 16
src/main/java/GameLogic.java View File

import processing.core.PApplet; import processing.core.PApplet;

import java.awt.*; import java.awt.*;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Queue; import java.util.Queue;
PApplet.main("GameLogic"); PApplet.main("GameLogic");
} }


private Queue<int[]> trail = new LinkedList<>();
private Point testPoint = new Point(200, 200);

public void settings() { public void settings() {
size(800, 600); size(800, 600);
noLoop();
} }


public boolean doCheck(Queue<int[]> trail, Point point){
public void setup() {
noLoop(); // Zeichnen nur bei Bedarf
fillTrail(); // Beispielpfad erstellen
//boolean collision = doCheck(trail, testPoint);
System.out.println("Collision detected: ");
}


int particleSize = trail.size()+5;
private void fillTrail() {
// Beispielhafte Füllung der Spur
for (int i = 0; i < 100; i++) {
trail.add(new int[]{50 + i * 5, 300}); // Punkte entlang der x-Achse
}
}


background(255, 255, 255);
public boolean doCheckExtern(Queue<int[]> trail, Point point){
return doCheck(trail, point);
}

public boolean doCheck(Queue<int[]> trail, Point point) {
int particleSize = 10;


for (int[] koordinates : new LinkedList<>(trail)) {
int x = koordinates[0];
int y = koordinates[1];
fill(0,0,0);
// Zeichne den Pfad auf das Canvas
background(255); // Weißer Hintergrund
for (int[] coordinates : new LinkedList<>(trail)) {
int x = coordinates[0];
int y = coordinates[1];
fill(0); // Schwarz
rect(x, y, particleSize, particleSize); rect(x, y, particleSize, particleSize);
} }


int c = get(point.x, point.y);
if (c==0){
return true; //Kollision erkannt
} else {
return false;
}
// Überprüfe die Pixelwerte am Testpunkt
int c = get(point.x, point.y); // Farbwert abrufen
return (c == color(0)); // Schwarz bedeutet Kollision
} }

} }

+ 26
- 9
src/main/java/GameLoop.java View File

public GameLoop(){ public GameLoop(){
player1 = new Player(0, "192.168.1.1", 9000); player1 = new Player(0, "192.168.1.1", 9000);
player2 = new Player(1, "192.168.1.2", 9000); player2 = new Player(1, "192.168.1.2", 9000);

tracker = new Tracker(); tracker = new Tracker();
// Starte Tracker in einem separaten Thread
Thread trackerThread = new Thread(tracker::run);
trackerThread.start();


graphics = new GraphicsProgram();
graphics = new GraphicsProgram(player1, player2);
String[] args = {"GraphicsProgram"}; String[] args = {"GraphicsProgram"};
PApplet.runSketch(args, graphics); PApplet.runSketch(args, graphics);



gl = new GameLogic(); gl = new GameLogic();
String[] args_ = {"GameLogic"}; String[] args_ = {"GameLogic"};
PApplet.runSketch(args_, gl); PApplet.runSketch(args_, gl);
try {
Thread.sleep(5000);
}catch (Exception e){

}

run(); run();
} }


private void run(){ private void run(){
while(true){ while(true){
communicate();
//communicate();

Point p1 = tracker.getP1();
//System.out.println("Player 1 is at " + p1.x + " and " + p1.y);
Point p2 = tracker.getP2();
//System.out.println("Player 2 is at " + p2.x + " and " + p2.y);


List<Point> positions = track();
player1.setKoords(p1.x, p1.y);
player2.setKoords(p2.x, p2.y);


if(gl.doCheck(player2.getTrail(), positions.get(0))){ //Check Position von P1 mit Spur von P2
if(gl.doCheckExtern(player2.getTrail(), p1)){ //Check Position von P1 mit Spur von P2
System.out.println("Spieler 1 hat verloren"); System.out.println("Spieler 1 hat verloren");
break; break;
} }


if(gl.doCheck(player1.getTrail(), positions.get(1))){ //Check Position von P2 mit Spur von P1
if(gl.doCheckExtern(player1.getTrail(), p2)){ //Check Position von P2 mit Spur von P1
System.out.println("Spieler 2 hat verloren"); System.out.println("Spieler 2 hat verloren");
break; break;
} }
//offen für Auto Kommunikation //offen für Auto Kommunikation
} }


private List<Point> track(){
/*private List<Point> track(){
Point p1 = tracker.getP1(); Point p1 = tracker.getP1();
Point p2 = tracker.getP2(); Point p2 = tracker.getP2();
System.out.println(p1.x);


player1.setKoords(p1.x, p1.y); player1.setKoords(p1.x, p1.y);
player2.setKoords(p2.x, p2.y); player2.setKoords(p2.x, p2.y);
output.add(0, p1); output.add(0, p1);
output.add(1, p2); output.add(1, p2);
return output; return output;
}
}*/


private void draw(){ private void draw(){
graphics.drawTrail(player1.getTrail(), 0);
graphics.drawTrail(player2.getTrail(), 1);

} }
} }

+ 34
- 9
src/main/java/GraphicsProgram.java View File

PImage imgBlue; PImage imgBlue;
PImage imgRed; PImage imgRed;


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

public static void main(String[] args) { public static void main(String[] args) {
PApplet.main("GraficsProgram");
PApplet.main("GraphicsProgram");
} }


public void settings() { public void settings() {
size(800, 600);
imgBlue = loadImage("TrailBlue");
imgRed = loadImage("TrailRed");
fullScreen();
} }


public void setup(){
imgBlue = loadImage("TrailBlue.png");
imgRed = loadImage("TrailRed.png");
background(0);
}


public void drawTrail(Queue<int[]> trail, int playerID){
public void draw(){
drawPlayer(0);
drawPlayer(1);
}
public void drawPlayer(int playerID){


int particleSize = 5; int particleSize = 5;


Queue<int[]> trail;

if(playerID == 0){
trail = player1.getTrail();
} else{
trail = player2.getTrail();
}


background(255);
while(!trail.isEmpty()) {
int[] koordinaten = trail.poll();
int x = koordinaten[0];
int y = koordinaten[1];


for (int[] koordinates : new LinkedList<>(trail)) {
int x = koordinates[0];
int y = koordinates[1];
System.out.println();


if(playerID == 0){ if(playerID == 0){
image(imgBlue, x, y, particleSize, particleSize); image(imgBlue, x, y, particleSize, particleSize);

+ 4
- 2
src/main/java/Player.java View File

private final int ID; private final int ID;
private int x; private int x;
private int y; private int y;

private String name = "Thunfisch";
Queue<int[]> trail = new LinkedList<>(); Queue<int[]> trail = new LinkedList<>();


ArduinoCommunication arduinoCommunicator; ArduinoCommunication arduinoCommunicator;
} }


private void addToTrail(int x, int y) { private void addToTrail(int x, int y) {
this.trail.add(new int[]{x, y});

trail.add(new int[]{x, y});
if(trail.size() > TRAIL_LENGTH){ if(trail.size() > TRAIL_LENGTH){
trail.poll(); trail.poll();
} }

} }


public Queue<int[]> getTrail(){ public Queue<int[]> getTrail(){

+ 57
- 44
src/main/java/Tracker.java View File

import org.opencv.imgproc.Moments; import org.opencv.imgproc.Moments;
import org.opencv.videoio.Videoio; import org.opencv.videoio.Videoio;



import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Queue;
import java.util.Comparator;

import java.awt.Point;


public class Tracker {
public class Tracker implements Runnable {


private final int CAM = 1; // Kamera-ID (0 für Webcam, 1 für andere Cams) private final int CAM = 1; // Kamera-ID (0 für Webcam, 1 für andere Cams)
private final int EXP = 0; // Belichtungszeit (idealerweise: 0) private final int EXP = 0; // Belichtungszeit (idealerweise: 0)
private final int FPS = 60; // FPS
private final int THV = 250;
private final int FPS = 60; // FPS
private final int THV = 250; // Threshold

private volatile boolean running = true; // Steuerung des Tracker-Threads


private Point currentPositionP1 = new Point(0, 0);
private Point currentPositionP2 = new Point(0, 0);
private volatile Point currentPositionP1 = new Point(0, 0);
private volatile Point currentPositionP2 = new Point(0, 0);


public Tracker() {}
public Tracker() {
}


public static void main(String[] args) { public static void main(String[] args) {
Tracker tracker = new Tracker(); Tracker tracker = new Tracker();
tracker.run();
Thread trackerThread = new Thread(tracker);
trackerThread.start();

// Beispiel: Positionen regelmäßig abfragen
/*
while (true) {
Point p1 = tracker.getP1();
Point p2 = tracker.getP2();
//System.out.println("P2 - X: " + p2.x + " Y: " + p2.y);

try {
Thread.sleep(1000); // Warte 1 Sekunde
} catch (InterruptedException e) {
e.printStackTrace();
}
}
*/
} }


private void run() {
@Override
public void run() {
VideoCapture capture = setUp(); VideoCapture capture = setUp();


if (!capture.isOpened()) { if (!capture.isOpened()) {
Mat grayFrame = new Mat(); Mat grayFrame = new Mat();
Mat thresholdFrame = new Mat(); Mat thresholdFrame = new Mat();


while (true) {
while (running) {
if (capture.read(frame)) { if (capture.read(frame)) {



// Konvertiere in Graustufen und wende Threshold an // Konvertiere in Graustufen und wende Threshold an
Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY); Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY);
Imgproc.threshold(grayFrame, thresholdFrame, THV, 255, Imgproc.THRESH_BINARY); Imgproc.threshold(grayFrame, thresholdFrame, THV, 255, Imgproc.THRESH_BINARY);


// Tracke die Positionen der beiden größten Objekte // Tracke die Positionen der beiden größten Objekte
List<Point> positions = trackPositions(thresholdFrame); List<Point> positions = trackPositions(thresholdFrame);
//Check


if (positions.size() >= 2) { if (positions.size() >= 2) {
currentPositionP1 = positions.get(0);
currentPositionP2 = positions.get(1);

System.out.println("P1 - X: " + currentPositionP1.x + " Y: " + currentPositionP1.y + " P2 - X: " + currentPositionP2.x + " Y: " + currentPositionP2.y);

setCurrentPositionP1(positions.get(0));
//System.out.println(currentPositionP1);
//Check. currentPosition wird aktualisiert
setCurrentPositionP2(positions.get(1));
//Check

//System.out.println("P1 - X: " + currentPositionP1.x + " Y: " + currentPositionP1.y +
// " P2 - X: " + currentPositionP2.x + " Y: " + currentPositionP2.y);
} }
drawMarkers(frame);
HighGui.imshow("GTCar", frame); HighGui.imshow("GTCar", frame);


if (HighGui.waitKey(1) == 27) {
break;
if (HighGui.waitKey(1) == 27) { // ESC-Taste zum Beenden
stop();
} }
} else { } else {
System.out.println("Fehler: Das Kamerabild konnte nicht gelesen werden."); System.out.println("Fehler: Das Kamerabild konnte nicht gelesen werden.");
break; break;
} }
} }
capture.release();
} }


private List<Point> trackPositions(Mat thresholdFrame) { private List<Point> trackPositions(Mat thresholdFrame) {
// Sortiere Konturen nach Fläche (absteigend) // Sortiere Konturen nach Fläche (absteigend)
contours.sort((c1, c2) -> Double.compare(Imgproc.contourArea(c2), Imgproc.contourArea(c1))); contours.sort((c1, c2) -> Double.compare(Imgproc.contourArea(c2), Imgproc.contourArea(c1)));



// Speichere die Positionen der zwei größten Konturen // Speichere die Positionen der zwei größten Konturen
List<Point> positions = new ArrayList<>(); List<Point> positions = new ArrayList<>();


positions.add(new Point(cx, cy)); positions.add(new Point(cx, cy));
} }
} }
//Check
return positions; return positions;
} }


public Point getP1(){
public synchronized Point getP1() {
//ab hier ist Punkt[0,0]
return currentPositionP1; return currentPositionP1;
} }


public Point getP2(){
public synchronized Point getP2() {
return currentPositionP2; return currentPositionP2;
} }


private void drawMarkers(Mat frame) {
// Größe der Quadrate
int size = 10;

// Blaues Quadrat an currentPositionP1
if (currentPositionP1 != null) {
Point topLeftP1 = new Point(currentPositionP1.x - size, currentPositionP1.y - size);
Point bottomRightP1 = new Point(currentPositionP1.x + size, currentPositionP1.y + size);
Imgproc.rectangle(frame, topLeftP1, bottomRightP1, new Scalar(255, 0, 0), -1); // Blau, gefüllt
}

// Rotes Quadrat an currentPositionP2
if (currentPositionP2 != null) {
Point topLeftP2 = new Point(currentPositionP2.x - size, currentPositionP2.y - size);
Point bottomRightP2 = new Point(currentPositionP2.x + size, currentPositionP2.y + size);
Imgproc.rectangle(frame, topLeftP2, bottomRightP2, new Scalar(0, 0, 255), -1); // Rot, gefüllt
}
private synchronized void setCurrentPositionP1(Point p1) {
//p1 ist richtig
currentPositionP1 = p1;
} }


private synchronized void setCurrentPositionP2(Point p2) {
currentPositionP2 = p2;
}


private VideoCapture setUp() { private VideoCapture setUp() {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); System.loadLibrary(Core.NATIVE_LIBRARY_NAME);


return capture; return capture;
} }

public void stop() {
running = false;
}
} }

Loading…
Cancel
Save