Kollision zweier Objekte progammieren in Java Processing?
Hallo,
Habe diese Frage schon oft gestellt aber keiner antwortet.
Ich hab es folgend versucht:
If((schussx1 == balkenx)&&(schussy ==Balkens){
while(true);
}
2 Antworten
hier diese Klasse beinhaltet die Collision Detection für zwei Rectangle Objekte, vllt. hilft dir das schon
falls du mal Circle - Rectangle brauchst einfach melden
Rectangle a;
Rectangle b;
void setup() {
size(600, 600);
a = new Rectangle(200, 100, 70, 120);
b = new Rectangle(100, 100, 50, 50);
}
void draw() {
background(100);
b.x = mouseX;
b.y = mouseY;
if (a.checkCollision(b)) {
a.col = color(255,100,100);
}else{
a.col = color(100,100,100);
}
a.drawRectangle();
b.drawRectangle();
}
private class Rectangle {
private float x, y, w, h;
private color col;
private Rectangle(float x, float y, float w, float h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
private void drawRectangle() {
rectMode(CORNER);
fill(col);
rect(x, y, w, h);
}
private boolean checkCollision(Rectangle b) {
return (this.x+this.w)>b.x && (this.x<(b.x+b.w)) && (this.y+this.h)>b.y && (this.y<(b.y+b.h));
}
}
Was soll das tun?
Es startet einfach nur eine endlose While-Schleife, wenn irgendwelche Variablen (Koordinatenpunkte?) gleich sind.
Warum speicherst du die Location nicht mit nem Point?
https://docs.oracle.com/javase/7/docs/api/java/awt/Point.html
oder wenns präziser sein soll:
https://docs.oracle.com/javase/7/docs/api/java/awt/geom/Point2D.Double.html
Davon mal ganz ab, habe ich keinen Schimmer, ob "Balkens" richtig ist. Irgendwie hört sich "BalkenY" sinnvoller an, aber okay.
Oder verwaltest du die Koordinaten direkt in den Objekten?
Zu deinem eigentlichen Problem: Bau halt Hitboxen. Guck dir an, wie sie funktionieren und wende es auf dein Problem an ^^.