Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to utilize a boolean method, which returns more than 2 values
    primarykey
    data
    text
    <p>This method returns true, false, and another value.</p> <pre><code>public boolean Intersection (Circle circle, Rectangle rectangle) { ... // test something ... return true; ... // test another thing ... return false; ... ... return xCornerDistSq + yCornerDistSq &lt;= maxCornerDistSq; //Third return value } </code></pre> <p>This is a 2D game, where a ball is supposed to bounce off of a rectangle, including the edges of the rectangle. The third return value, which I linked above, is supposed to detect collision between the ball and the edges of the rectangle. The problem is, I don't know how to utilize it in my code once I call the method.</p> <p>What I currently have is this</p> <p>This is the full code of the method:</p> <pre><code> public boolean Intersection (Circle circle, Rectangle rectangle) { double cx = Math.abs(circle.getLayoutX() - rectangle.getLayoutX() - rectangle.getWidth() / 2); double xDist = rectangle.getWidth() / 2 + circle.getRadius(); if (cx &gt; xDist) { return false; } double cy = Math.abs(circle.getLayoutY() - rectangle.getLayoutY() - rectangle.getHeight() / 2) ; double yDist = rectangle.getHeight() / 2 + circle.getRadius(); if (cy &gt; yDist) { return false; } if (cx &lt;= rectangle.getWidth() / 2 || cy &lt;= rectangle.getHeight() / 2) { return true; } double xCornerDist = cx - rectangle.getWidth() / 2; double yCornerDist = cy - rectangle.getHeight() / 2; double xCornerDistSq = xCornerDist * xCornerDist; double yCornerDistSq = yCornerDist * yCornerDist; double maxCornerDistSq = circle.getRadius() * circle.getRadius(); return xCornerDistSq + yCornerDistSq &lt;= maxCornerDistSq; } </code></pre> <p>So, how do I go about implementing it when I call the function? I want my ball to bounce off of the edges too, but I don't know how to call it by using this method.</p> <p>What I currently have is this:</p> <pre><code> boolean intersection = Intersection(circle1, rect1); if (intersection == true) { double x = (rect1.getLayoutX() + rect1.getWidth() / 2) - (circle1.getLayoutX() + circle1.getRadius()); double y = (rect1.getLayoutY() + rect1.getHeight() / 2) - (circle1.getLayoutY() + circle1.getRadius()); if (Math.abs(x) &gt; Math.abs(y)) { c1SpeedX = -c1SpeedX; } else { c1SpeedY = -c1SpeedY; } } } } </code></pre>
    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.
 

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