java - Collision detection between circles -
i got working class creates darkhole object in screen. filled circle random color, position , radius. when screen touched, supposed draw ball avoiding other ones have been drawn.
i used method: colision radius
public class darkhole extends view { public static final int maxdiameter = 250; public static final int mindiameter = 240; /** * class log tag. */ private static final string log_tag = darkhole.class.getsimplename(); private static list<shapedrawable> mholes; private int mwindowwidth; private int mwindowheight; private random random; /** * constructor; * * @param context application context. */ public darkhole(context context) { super(context); mholes = new arraylist<shapedrawable>(); random = new random(); //get screen size point point = new point(); windowmanager windowmanager = (windowmanager) context.getsystemservice(context.window_service); display display = windowmanager.getdefaultdisplay(); display.getsize(point); // screen max x , y. mwindowwidth = point.x; mwindowheight = point.y; } /** * draw random hole. */ private void generaterandomhole() { while(true) { shapedrawable hole = new shapedrawable(new ovalshape()); // generate random color. int r = random.nextint(255); int g = random.nextint(255); int b = random.nextint(255); int randomcolor = color.rgb(r, g, b); // generate random position. int diameter = random.nextint(maxdiameter - mindiameter + 1) + mindiameter; int x = random.nextint((mwindowwidth - diameter) + 1); int y = random.nextint((mwindowheight - diameter) + 1); hole.getpaint().setcolor(randomcolor); hole.setbounds(x, y, x + diameter, y + diameter); if (checkdrawcontains(hole)) { log.d(log_tag, "[generaterandomhole] contains!"); hole = null; random = null; } else { log.d(log_tag, "[generaterandomhole] not contains!"); mholes.add(hole); break; } } } /** * draw informative text. * * @param canvas canvas object. */ private void generatetext(canvas canvas) { paint color = new paint(); color.setcolor(color.black); color.settextsize(70); color.setantialias(true); color.setdither(true); canvas.drawtext("bolas: " + mholes.size(), 10, 50, color); } private boolean checkdrawcontains(shapedrawable newhole) { long newcenterx = newhole.getbounds().left + (newhole.getbounds().width()/2); long newcentery = newhole.getbounds().top + (newhole.getbounds().height()/2); log.d(log_tag, "[checkdrawcontains] newcenterx: " + newcenterx); for(shapedrawable hole: mholes) { long centerx = hole.getbounds().left + (hole.getbounds().width()/2); long centery = hole.getbounds().top + (hole.getbounds().height()/2); long x = centerx - newcenterx; long y = centery - newcentery; long aux = (long) ((math.pow(math.abs(x),2)) + (math.pow(math.abs(y),2))); long distance = (long) math.sqrt(aux); log.d(log_tag, "[checkdrawcontains] x: " + x); log.d(log_tag, "[checkdrawcontains] y: " + y); log.d(log_tag, "[checkdrawcontains] distance: " + distance); log.d(log_tag, "[checkdrawcontains] sum radius: " + (newcenterx + centerx)); long srads = (newhole.getbounds().width()/2) + (hole.getbounds().width()/2); if(distance <= srads ) { return true; } } return false; } /** {@inheritdoc} */ protected void ondraw(canvas canvas) { super.ondraw(canvas); generatetext(canvas); (shapedrawable hole : mholes) { hole.draw(canvas); } invalidate(); } /** {@inheritdoc} */ @override public boolean ontouchevent(motionevent event) { switch (event.getaction()) { case motionevent.action_down: generaterandomhole(); return true; } return super.ontouchevent(event); } }
it should work, crashes when recursion starts recalculate random parameters. stack overflow? why happening? how should solved?
Comments
Post a Comment