Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK adapting some code I found <a href="https://stackoverflow.com/questions/18754722/detecting-if-rectangles-buttons-overlap-on-x-or-y-axis/18756547?noredirect=1#18756547">here</a>, I have come up with a solution which works perfectly:</p> <pre><code>Float newX = ev.getX() - (b.getMeasuredWidth()/2); Float newY = ev.getY() - (b.getMeasuredHeight()/2); for (TouchButton t:myButtons) { if(!(t.equals(b))) { Rectangle r1 = new Rectangle(t.getX(), t.getY(), (float)t.getMeasuredWidth(), (float)t.getMeasuredHeight()); Rectangle r2 = new Rectangle(newX, b.getY(), (float)b.getMeasuredWidth(), (float) b.getMeasuredHeight()); Rectangle r3 = new Rectangle(b.getX(), newY, (float)b.getMeasuredWidth(), (float) b.getMeasuredHeight()); if(r1.interects(r2)) { MoveX = false; } if(r1.interects(r3)) { MoveY = false; } } } </code></pre> <p>and here is Rectangle.java:</p> <pre><code>public class Rectangle { private Float startX; private Float startY; private Float width; private Float height; private Float endX; private Float endY; public Rectangle(Float _x, Float _y, Float _width, Float _height) { startX = _x; startY = _y; width = _width; height = _height; endX = _width + _x; endY = _height + _y; } public Float getX() { return startX; } public Float getY() { return startY; } public float getWidth() { return width; } public float getHeight() { return height; } public float getEndX() { return endX; } public float getEndY() { return endY; } public boolean interects (Rectangle _r2) { return rectOverlap(this, _r2); } private boolean valueInRange(float value, float min, float max) { return (value &gt;= min) &amp;&amp; (value &lt;= max); } private boolean rectOverlap(Rectangle A, Rectangle B) { boolean xOverlap = valueInRange(A.getX(), B.getX(), B.getEndX()) || valueInRange(B.getX(), A.getX(), A.getEndX()); boolean yOverlap = valueInRange(A.getY(), B.getY(), B.getEndY()) || valueInRange(B.getY(), A.getY(), A.getY() + B.getHeight()); return xOverlap &amp;&amp; yOverlap; } } </code></pre> <p>[old choppy answer here] </p> <p>[for historical purposes]</p> <p>I'm not sure how elegant this is, maybe somebody can come up with something better?</p> <p>Building on Khaled A Khunaifer's answer, I've realized I need to check where the buttons WOULD be placed if the move was to take place, then only perform each move if there would be no collisions after:</p> <pre><code>Boolean MoveX = true; Boolean MoveY = true; Float newX = ev.getX() - (b.getMeasuredWidth()/2); Float newY = ev.getY() - (b.getMeasuredHeight()/2); for (TouchButton t:myButtons) { if(!(t.equals(b))) { if (areOverlapping(t,b.getX(), newY)) { MoveY=false; } if(areOverlapping(t,newX,b.getY())) { MoveX = false; } } } if (MoveX) { b.setX(newX); } if (MoveY) { b.setY(newY); } boolean areOverlapping (TouchButton a, double x, double y) { return (y &gt;= a.getY() &amp;&amp; (y &lt;= (a.getY() + a.getMeasuredHeight())) &amp;&amp; x &gt;= a.getX() &amp;&amp; (x &lt;= (a.getX() + a.getMeasuredWidth()))); } </code></pre> <p>This kind of achieves what I'm looking for but it's a bit choppy and sometimes allows the buttons to overlap. I'll try experimenting with the areTouching but I need to decode the logic first as the way it's written is producing errors.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload