swing - Java Circle to Circle collision detection -
i making circle circle collision detection program. can balls move around when collision detected, balls quite far overlapped. suggestions? in advance!
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.arraylist; import java.lang.math; public class shapepanel extends jpanel{ private jbutton button, startbutton, stopbutton; private jtextfield textfield; private jlabel label; private timer timer; private final int delay = 10; arraylist<shape> obj = new arraylist<shape>(); public static void main(string[] args){ jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(new shapepanel()); frame.pack(); frame.setvisible(true); } public shapepanel(){ jpanel controlpanel = new jpanel(); drawingpanel dpanel = new drawingpanel(); controlpanel.setpreferredsize(new dimension(100,400)); button = new jbutton("add shape"); startbutton = new jbutton("start"); stopbutton = new jbutton("stop"); textfield = new jtextfield(2); label = new jlabel("count:"); controlpanel.add(button); controlpanel.add(label); controlpanel.add(textfield); controlpanel.add(startbutton); controlpanel.add(stopbutton); add(controlpanel); add(dpanel); buttonlistener blisten = new buttonlistener(); button.addactionlistener(blisten); startbutton.addactionlistener(blisten); stopbutton.addactionlistener(blisten); timer = new timer(delay, blisten); } private class buttonlistener implements actionlistener{ public void actionperformed(actionevent e){ if (e.getsource() == button){ obj.add(new shape()); if (obj.get(obj.size()-1).y > 200){ obj.get(obj.size()-1).movey = -obj.get(obj.size()-1).movey; } }else if (e.getsource() == timer){ (int = 0; < obj.size(); i++){ obj.get(i).move(); } (int = 0; < obj.size(); i++){ (int j = + 1; j < obj.size(); j++){ if (math.sqrt(math.pow((double)obj.get(i).centercoordx - (double)obj.get(j).centercoordx,2)) + math.pow((double)obj.get(i).centercoordy - (double)obj.get(j).centercoordy,2) <= obj.get(i).radius + obj.get(j).radius){ timer.stop(); } } } }else if (e.getsource() == startbutton){ timer.start(); }else if (e.getsource() == stopbutton){ timer.stop(); } repaint(); } } private class drawingpanel extends jpanel{ drawingpanel(){ setpreferredsize(new dimension(400,400)); setbackground(color.pink); } public void paintcomponent(graphics g){ super.paintcomponent(g); for(int = 0; < obj.size(); i++){ obj.get(i).display(g); } } } } import java.awt.*; import java.util.*; public class shape{ public int x, y, width, height, movex = 1, movey = 1, centercoordx, centercoordy, radius; private color colour; public boolean reverse = false, samedirection = true; random generator = new random(); public int randomrange(int lo, int hi){ return generator.nextint(hi-lo)+lo; } shape(){ width = randomrange(30, 50); if (width % 2 != 0){ width = randomrange(30, 50); } height = width; radius = width/2; x = randomrange(0, 400-width); y = randomrange(0, 400-height); colour = new color(generator.nextint(256),generator.nextint(256),generator.nextint(256)); } public void display(graphics g){ g.setcolor(colour); g.filloval(x, y, width, height); } void move(){ x += movex; y += movey; centercoordx = x + width/2; centercoordy = y + height/2; if(x >= 400-width){ movex = -movex; }if(x <= 0){ movex = -movex; }if(y >= 400-height){ movey = -movey; }if (y <= 0){ movey = -movey; } } }
so uncommented code!
the balls colliding if centres within sum of radii. let r1 , r2 ball radii, , x1, y1 position of centre of ball1; x2, y2 ball2.
measure square of distance between centres (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)
. (pythagoras).
they have collided if less or equal (r1 + r2) * (r1 + r2)
.
the key thing here there no need compute square roots expensive computational task.
Comments
Post a Comment