Fehlerhafte Kollissionserkennung

Status
Nicht offen für weitere Antworten.

Lucky Luc

Mitglied
Ich hab ein kleines Java-Spiel gemacht, habe aber etwas Probleme mit der Kollissionserkennung. Ganz davon abgesehen, dass der Ball wie ein Rechteck behandelt wird (ist mir aber relativ egal; das merkt eh keiner), kann es bei steigender Geschwindigkeit sein, dass der Ball praktisch durch den ganzen Schläger "durchflutscht". Kann ich dagegen etwas machen?

Hier der Code:
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class pingpong extends Applet implements Runnable {
	int hits = 0;
	int rekord = 0;
	double ballx = 150;
	double bally = 150;
	double dirx = 0;
	double diry = 0;
	double sch = 0;
	int hitter = 0;
	int lost = 1;
	int bb;
	int i;
	Thread thr;
	Graphics g2,g1;
	Image offscreen;
	double t,tt;
	public void init() {
        g1 = getGraphics();
        offscreen = createImage(600,300);
        g2 = offscreen.getGraphics();
        addMouseMotionListener(new MMHandler());
        addMouseListener(new MHandler());
	}
	public void start() {
		thr = new Thread(this);
		t = 0;
		thr.start();
	}
	public void stop() {
		thr.stop();
		thr = null;
	}
	class MHandler extends MouseAdapter {
		public void mousePressed(MouseEvent e) {
			if(lost == 1) {
				ballx = 20;
				bally = 10;
				dirx = 1;
				diry = 1;
				lost = 0;
				hits = 0;
				hitter = 0;
			}
		}
	}
	class MMHandler extends MouseMotionAdapter {
		public void mouseMoved(MouseEvent e) {
			sch = e.getY()-25;
			if(sch > 250) {
				sch = 250;
			}
			if(sch < 0) {
				sch = 0;
			}
		}
	}
    public void run() {
		long t0,t1;
		t0 = System.currentTimeMillis();
		tt = 0;
		while(true) {
			paint(g2);
			g1.drawImage(offscreen,0,0,this);
			t1 = System.currentTimeMillis();
			tt = t;
			t += (t1 - t0) / 10.0;
			t0 = t1;
			if(bally >= 290) {
				if(diry >= 0) {
					diry -= diry*2;
				}
			}
			if(bally <= 0) {
				if(diry <= 0) {
					diry -= diry*2;
				}	
			}
			if(ballx >= 310) {
				if(dirx >= 0) {
					dirx -= dirx*2;
				}
				hitter = 1;
			}
			if(ballx <= 20.0 && ballx >= 0.0 && bally > sch-10 && bally < sch+50) {
				diry = (bally+5.0-(sch+25))/34.0;
				dirx = 2-diry;
				if(dirx > 2) {
					dirx += diry*2;
				}
				if(hitter == 1) {
					hits += 1;
					if(hits > rekord) {
						rekord = hits;
					}
					hitter = 0;
            		play(getCodeBase(), "sounds/ip.au");
				}
			}
			if(ballx < -10) {
				if(lost == 0) {
       	    		play(getCodeBase(), "sounds/that.hurts.au");
           			dirx = 0;
           			diry = 0;
           			lost = 1;
           		}
            }
		}
	}
	public void paint(Graphics g) {
		if(lost == 0) {
			g.setColor(Color.green);
			g.fillRect(0,0,600,300);
			g.setColor(Color.white);
			g.fillRect(600/2-5,0,10,300);
			g.drawRect(0,0,599,299);
			g.fillRect(0,150,285,2);
			g.setColor(Color.black);
			g.drawString("Treffer: "+hits,15,12);
			g.drawString("Rekord: "+rekord,85,12);
			g.fillRect(305,1,10,297);
			g.setColor(Color.red);
			g.fillRect(2,(int)sch,10,50);
			g.setColor(Color.orange);
			ballx += (t-tt)*dirx*(hits/20.0+1.0);
			bally += (t-tt)*diry*(hits/20.0+1.0);
			g.fillOval((int)ballx,(int)bally,10,10);
		} else {
			g.setColor(Color.black);
			g.fillRect(0,0,330,300);
			g.setColor(Color.white);
			g.drawString("Zum Starten ins Bild klicken",80,160);
		}
	}
}
Na toll; jetzt ist meine Frage schon auf der 2. Seite, und immer noch niemand hat geantwortet. Ich will mich ja nicht beschweren; aber sonst geht es hier ja immer so schnell. Und jetzt zum Grundthema Kollisionserkennung: Ich weiß jetzt, dass es da sowas mit Rectangle gibt; aber was ist das eigentlich so genau und wie kann ich es anwenden?
 

Lucky Luc

Mitglied
Ich hab mein Spiel jetzt erweitert und habe noch ein Problem:
Der Gewonnen-/Verlorenbildschirm funktioniert nicht.
Hier der neue Code:
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class pong42 extends Applet implements Runnable {
	int player = 0;
	int comp = 20;
	double ballx = 150;
	double bally = 150;
	double dirx = 0;
	double diry = 0;
	double sch = 0;
	double kisch = 0;
	int hitter = 0;
	int kihitter = 0;
	int lost = 1;
	int bb;
	int i;
	Thread thr;
	Graphics g2,g1;
	Image offscreen;
	double t,tt;
	public void init() {
        g1 = getGraphics();
        offscreen = createImage(600,300);
        g2 = offscreen.getGraphics();
        addMouseMotionListener(new MMHandler());
        addMouseListener(new MHandler());
	}
	public void start() {
		thr = new Thread(this);
		t = 0;
		thr.start();
	}
	public void stop() {
		thr.stop();
		thr = null;
	}
	class MHandler extends MouseAdapter {
		public void mousePressed(MouseEvent e) {
			if(lost == 1) {
				ballx = 20;
				bally = 10;
				dirx = 1.5;
				diry = 0.5;
				lost = 0;
				hitter = 0;
				kihitter = 0;
			}
		}
	}
	class MMHandler extends MouseMotionAdapter {
		public void mouseMoved(MouseEvent e) {
			sch = e.getY()-25;
			if(sch > 250) {
				sch = 250;
			}
			if(sch < 0) {
				sch = 0;
			}
		}
	}
    public void run() {
		long t0,t1;
		t0 = System.currentTimeMillis();
		tt = 0;
		while(true) {
			paint(g2);
			g1.drawImage(offscreen,0,0,this);
			t1 = System.currentTimeMillis();
			tt = t;
			t += (t1 - t0) / 10.0;
			t0 = t1;
			if(bally >= 290) {
				if(diry >= 0) {
					diry -= diry*2;
				}
			}
			if(bally <= 0) {
				if(diry <= 0) {
					diry -= diry*2;
				}	
			}
			if(ballx >= 580 && ballx <= 600 && bally > kisch-10 && bally < kisch+50) {
				diry = (bally+5.0-(kisch+25))/34.0;
				dirx = -2+diry;
				hitter = 1;
				if(dirx >= 0) {
					dirx -= dirx*2;
				}
				if(dirx < -2) {
					dirx -= diry*2;
				}
				if(kihitter == 1) {
					hitter = 0;
            		play(getCodeBase(), "sounds/ip.au");
				}
			}
			if(ballx <= 20.0 && ballx >= 0.0 && bally > sch-10 && bally < sch+50) {
				diry = (bally+5.0-(sch+25))/34.0;
				dirx = 2-diry;
				kihitter = 1;
				if(dirx > 2) {
					dirx += diry*2;
				}
				if(hitter == 1) {
					kihitter = 0;
            		play(getCodeBase(), "sounds/ip.au");
				}
			}
			if(ballx < -10) {
				if(lost == 0) {
       	    		play(getCodeBase(), "sounds/that.hurts.au");
           			dirx = 0;
           			diry = 0;
           			lost = 1;
           			comp += 1;
           		}
            }
			if(ballx > 610) {
				if(lost == 0) {
       	    		play(getCodeBase(), "sounds/that.hurts.au");
           			dirx = 0;
           			diry = 0;
           			lost = 1;
           			player += 1;
           		}
            }
		}
	}
	public void paint(Graphics g) {
		if(lost == 0) {
			g.setColor(Color.green);
			g.fillRect(0,0,600,300);
			g.setColor(Color.white);
			g.drawRect(0,0,599,299);
			g.fillRect(0,150,600,2);
			g.fillRect(295,0,10,300);
			g.setColor(Color.black);
			g.drawString(player+":"+comp,290,12);
			g.setColor(Color.red);
			if(bally+10 < kisch+25) {
				kisch -= t-tt;
			}
			if(bally+10 > kisch+25)  {
				kisch += t-tt;
			}
			if(kisch < 0) {
				kisch = 0;
			}
			if(kisch > 250) {
				kisch = 250;
			}
			g.fillRect(2,(int)sch,10,50);
			g.fillRect(587,(int)kisch,10,50);
			g.setColor(Color.orange);
			ballx += (t-tt)*dirx*1.5;
			bally += (t-tt)*diry*1.5;
			g.fillOval((int)ballx,(int)bally,10,10);
		} else {
			g.setColor(Color.black);
			g.fillRect(0,0,600,300);
			g.setColor(Color.white);
			if(player < 21 && comp < 21) {
				g.drawString("Zum Starten ins Bild klicken",230,160);
			}
			if(player == 21) {
			g.drawString("Gewonnen!!!",270,160);
				player = 0;
				comp = 0;
			}
			if(comp == 21) {
			g.drawString("Leider Verloren...",260,160);
				player = 0;
				comp = 0;
			}
		}
	}
}
Ich hoffe, jetzt antwortet mir mal einer.
 

Lucky Luc

Mitglied
:gaen:
:gaen: :gaen:
:gaen: :gaen: :gaen:

Ich warte...

Muss jetz meine Frage wieder ein bisschen nach vorne bringen; will nicht, dass sie in den Untiefen der anfängerfragen versinkt...
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben