How can you determine the strategy of colliding shapes with each other? Throw some thread idea, I was thinking about comparing the coordinates of the figures, the presence of common points. But the algorithm is not yet possible to come up, help out

Closed due to the fact that the issue is too common for the participants Kromster , αλεχολυτ , Grundy , Sasha Omelchenko , pavel Apr 7 '17 at 18:32 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Well, yes, checking the coordinates. You can first do a rough comparison of the comparison rectangles) for better performance. And then look for common points. Depending on the type of shapes you need to check differently. Looking pixel-by-pixel intersection is, of course, very inefficient, but in the worst case it will be so. - cy6erGn0m 8:25 pm
  • Where did pixelated come from? A figure is one thing, and displaying it in a raster is another. And so - look for border crossings. (If there are no intersections, then you can check for the belonging of a smaller point in the larger figure - the smaller one is already inside) - alexlz

2 answers 2

if these are squares then just type something (their total height and width are 10, hence 5)

if((x-x1)<5) { if((y-y1)<5) { ....// действия при столкновении } } 

if these are circles then (assume that the sum of the radii is 5)

 x0=x-x1 y0=y-y1 if(Math.sqrt((x0*x0)+(y0*y0))>5) { ...// действия при столкновении } 

I also found a method that checks if the figure does not contain a given point or a rectangle

 private Ellipse2D q; public boolean find(Point2D t)// также может быть Rectangle2D { if(q.contains(t)) { return true; } return false; } 

    The problem is not trivial. Start with this , for example. It is necessary to use some algorithms from computational geometry + physics. Maybe it’s just worth taking some physics engine where this thing is implemented.