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

@@ -4,11 +4,12 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<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$/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/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" />
</list>
<option name="SHOW_DIALOG" value="false" />
@@ -25,7 +26,6 @@
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
<option name="UPDATE_TYPE" value="REBASE" />
</component>
<component name="ProjectColorInfo">{
&quot;associatedIndex&quot;: 8
@@ -39,7 +39,6 @@
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"Application.GTCar.executor": "Run",
"Application.GameLogic.executor": "Run",
"Application.GraphicsProgram.executor": "Run",
"Application.Main.executor": "Run",
"Application.Tracker.executor": "Run",
@@ -52,7 +51,7 @@
"com.codeium.enabled": "true",
"git-widget-placeholder": "master",
"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.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
@@ -73,8 +72,8 @@
<option name="Make" enabled="true" />
</method>
</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" />
<method v="2">
<option name="Make" enabled="true" />
@@ -83,14 +82,20 @@
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Main" />
<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">
<option name="Make" enabled="true" />
</method>
</configuration>
<list>
<item itemvalue="Application.GTCar" />
<item itemvalue="Application.GraphicsProgram" />
<item itemvalue="Application.Main" />
</list>
<recent_temporary>
<list>
<item itemvalue="Application.Main" />
<item itemvalue="Application.GameLogic" />
<item itemvalue="Application.GraphicsProgram" />
</list>
</recent_temporary>
</component>
@@ -105,6 +110,7 @@
<workItem from="1732108906083" duration="758000" />
<workItem from="1732113837570" duration="20534000" />
<workItem from="1733327375447" duration="951000" />
<workItem from="1734091457440" duration="10746000" />
</task>
<task id="LOCAL-00001" summary="First Commit Test">
<option name="closed" value="true" />
@@ -171,14 +177,4 @@
<MESSAGE value="Version 27.11 (&quot;fertiger&quot; Tracker)" />
<option name="LAST_COMMIT_MESSAGE" value="Version 27.11 (&quot;fertiger&quot; Tracker)" />
</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>

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

@@ -1,5 +1,4 @@
import processing.core.PApplet;

import java.awt.*;
import java.util.LinkedList;
import java.util.Queue;
@@ -10,30 +9,45 @@ public class GameLogic extends PApplet {
PApplet.main("GameLogic");
}

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

public void settings() {
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);
}

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

@@ -18,30 +18,47 @@ public class GameLoop {
public GameLoop(){
player1 = new Player(0, "192.168.1.1", 9000);
player2 = new Player(1, "192.168.1.2", 9000);

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"};
PApplet.runSketch(args, graphics);


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

}

run();
}

private void run(){
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");
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");
break;
}
@@ -54,9 +71,10 @@ public class GameLoop {
//offen für Auto Kommunikation
}

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

player1.setKoords(p1.x, p1.y);
player2.setKoords(p2.x, p2.y);
@@ -65,10 +83,9 @@ public class GameLoop {
output.add(0, p1);
output.add(1, p2);
return output;
}
}*/

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

}
}

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

@@ -9,27 +9,52 @@ public class GraphicsProgram extends PApplet {
PImage imgBlue;
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) {
PApplet.main("GraficsProgram");
PApplet.main("GraphicsProgram");
}

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;

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){
image(imgBlue, x, y, particleSize, particleSize);

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

@@ -6,6 +6,8 @@ public class Player {
private final int ID;
private int x;
private int y;

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

ArduinoCommunication arduinoCommunicator;
@@ -37,11 +39,11 @@ public class Player {
}

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){
trail.poll();
}

}

public Queue<int[]> getTrail(){

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

@@ -5,32 +5,48 @@ import org.opencv.imgproc.Imgproc;
import org.opencv.imgproc.Moments;
import org.opencv.videoio.Videoio;


import java.util.ArrayList;
import java.util.LinkedList;
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 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) {
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();

if (!capture.isOpened()) {
@@ -42,35 +58,39 @@ public class Tracker {
Mat grayFrame = new Mat();
Mat thresholdFrame = new Mat();

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


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

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

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

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

private List<Point> trackPositions(Mat thresholdFrame) {
@@ -83,7 +103,6 @@ public class Tracker {
// Sortiere Konturen nach Fläche (absteigend)
contours.sort((c1, c2) -> Double.compare(Imgproc.contourArea(c2), Imgproc.contourArea(c1)));


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

@@ -95,37 +114,27 @@ public class Tracker {
positions.add(new Point(cx, cy));
}
}
//Check
return positions;
}

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

public Point getP2(){
public synchronized Point getP2() {
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() {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
@@ -140,4 +149,8 @@ public class Tracker {

return capture;
}

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

Loading…
Cancel
Save