Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Maybe this is an old question, but I think to have the solution for anyone else that will read this post:</p> <pre><code>/** Rectangle To Point. */ boolean testRectangleToPoint(double rectWidth, double rectHeight, double rectRotation, double rectCenterX, double rectCenterY, double pointX, double pointY) { if(rectRotation == 0) // Higher Efficiency for Rectangles with 0 rotation. return Math.abs(rectCenterX-pointX) &lt; rectWidth/2 &amp;&amp; Math.abs(rectCenterY-pointY) &lt; rectHeight/2; double tx = Math.cos(rectRotation)*pointX - Math.sin(rectRotation)*pointY; double ty = Math.cos(rectRotation)*pointY + Math.sin(rectRotation)*pointX; double cx = Math.cos(rectRotation)*rectCenterX - Math.sin(rectRotation)*rectCenterY; double cy = Math.cos(rectRotation)*rectCenterY + Math.sin(rectRotation)*rectCenterX; return Math.abs(cx-tx) &lt; rectWidth/2 &amp;&amp; Math.abs(cy-ty) &lt; rectHeight/2; } /** Circle To Segment. */ boolean testCircleToSegment(double circleCenterX, double circleCenterY, double circleRadius, double lineAX, double lineAY, double lineBX, double lineBY) { double lineSize = Math.sqrt(Math.pow(lineAX-lineBX, 2) + Math.pow(lineAY-lineBY, 2)); double distance; if (lineSize == 0) { distance = Math.sqrt(Math.pow(circleCenterX-lineAX, 2) + Math.pow(circleCenterY-lineAY, 2)); return distance &lt; circleRadius; } double u = ((circleCenterX - lineAX) * (lineBX - lineAX) + (circleCenterY - lineAY) * (lineBY - lineAY)) / (lineSize * lineSize); if (u &lt; 0) { distance = Math.sqrt(Math.pow(circleCenterX-lineAX, 2) + Math.pow(circleCenterY-lineAY, 2)); } else if (u &gt; 1) { distance = Math.sqrt(Math.pow(circleCenterX-lineBX, 2) + Math.pow(circleCenterY-lineBY, 2)); } else { double ix = lineAX + u * (lineBX - lineAX); double iy = lineAY + u * (lineBY - lineAY); distance = Math.sqrt(Math.pow(circleCenterX-ix, 2) + Math.pow(circleCenterY-iy, 2)); } return distance &lt; circleRadius; } /** Rectangle To Circle. */ boolean testRectangleToCircle(double rectWidth, double rectHeight, double rectRotation, double rectCenterX, double rectCenterY, double circleCenterX, double circleCenterY, double circleRadius) { double tx, ty, cx, cy; if(rectRotation == 0) { // Higher Efficiency for Rectangles with 0 rotation. tx = circleCenterX; ty = circleCenterY; cx = rectCenterX; cy = rectCenterY; } else { tx = Math.cos(rectRotation)*circleCenterX - Math.sin(rectRotation)*circleCenterY; ty = Math.cos(rectRotation)*circleCenterY + Math.sin(rectRotation)*circleCenterX; cx = Math.cos(rectRotation)*rectCenterX - Math.sin(rectRotation)*rectCenterY; cy = Math.cos(rectRotation)*rectCenterY + Math.sin(rectRotation)*rectCenterX; } return testRectangleToPoint(rectWidth, rectHeight, rectRotation, rectCenterX, rectCenterY, circleCenterX, circleCenterY) || testCircleToSegment(tx, ty, circleRadius, cx-rectWidth/2, cy+rectHeight/2, cx+rectWidth/2, cy+rectHeight/2) || testCircleToSegment(tx, ty, circleRadius, cx+rectWidth/2, cy+rectHeight/2, cx+rectWidth/2, cy-rectHeight/2) || testCircleToSegment(tx, ty, circleRadius, cx+rectWidth/2, cy-rectHeight/2, cx-rectWidth/2, cy-rectHeight/2) || testCircleToSegment(tx, ty, circleRadius, cx-rectWidth/2, cy-rectHeight/2, cx-rectWidth/2, cy+rectHeight/2); } </code></pre> <p>This is the code to test Collisions between a circle (The Ball) and a rectangle (That can rotate).</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. 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