`
aigo
  • 浏览: 2568927 次
  • 性别: Icon_minigender_1
  • 来自: 宜昌
社区版块
存档分类
最新评论

圆和矩形碰撞检测

 
阅读更多

圆和矩形碰撞检测:

http://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection

Here is how I would do it:

bool intersects(CircleType circle, RectType rect)
{
    circleDistance.x = abs(circle.x - rect.x);
    circleDistance.y = abs(circle.y - rect.y);

    if (circleDistance.x > (rect.width/2 + circle.r)) { return false; }
    if (circleDistance.y > (rect.height/2 + circle.r)) { return false; }

    if (circleDistance.x <= (rect.width/2)) { return true; } 
    if (circleDistance.y <= (rect.height/2)) { return true; }

    cornerDistance_sq = (circleDistance.x - rect.width/2)^2 +
                         (circleDistance.y - rect.height/2)^2;

    return (cornerDistance_sq <= (circle.r^2));
}

Here's how it works:

illusration

  1. The first pair of lines calculate the absolute values of the x and y difference between the center of the circle and the center of the rectangle. This collapses the four quadrants down into one, so that the calculations do not have to be done four times. The image shows the area in which the center of the circle must now lie. Note that only the single quadrant is shown. The rectangle is the grey area, and the red border outlines the critical area which is exactly one radius away from the edges of the rectangle. The center of the circle has to be within this red border for the intersection to occur.

  2. The second pair of lines eliminate the easy cases where the circle is far enough away from the rectangle (in either direction) that no intersection is possible. This corresponds to the green area in the image.

  3. The third pair of lines handle the easy cases where the circle is close enough to the rectangle (in either direction) that an intersection is guaranteed. This corresponds to the orange and grey sections in the image. Note that this step must be done after step 2 for the logic to make sense.

  4. The remaining lines calculate the difficult case where the circle may intersect the corner of the rectangle. To solve, compute the distance from the center of the circle and the corner, and then verify that the distance is not more than the radius of the circle. This calculation returns false for all circles whose center is within the red shaded area and returns true for all circles whose center is within the white shaded area.

分享到:
评论

相关推荐

    2D的矩形和圆的碰撞检测演示程序

    本程序演示了矩形和圆的碰撞检测,用鼠标可以控制圆与矩形碰撞,如果发生碰撞,矩形和圆都显示为红色,否则为白色。 具体的算法介绍和讨论在这个帖子里 ...

    圆形与矩形碰撞.rar

    在计算机图形学和游戏开发领域,圆形与矩形的碰撞检测是一个常见的问题。这个压缩包文件"圆形与矩形碰撞.rar"很可能包含了使用易语言编写的源代码,用于实现这种碰撞检测算法。易语言是一种简单易学的编程语言,特别...

    易语言源码圆形与矩形碰撞易语言源码.rar

    总结来说,"易语言源码圆形与矩形碰撞"的实现不仅提供了基本的几何碰撞检测方法,还展示了易语言的编程实践,包括数据结构定义、函数设计和异常处理等。通过学习和理解这份源码,开发者可以进一步提升在易语言环境下...

    易语言圆形与矩形碰撞

    1. **圆形与矩形碰撞检测**:对于一个圆形和一个矩形的碰撞检测,我们首先需要知道圆心的坐标 `(x_c, y_c)` 和半径 `r`,以及矩形的左下角坐标 `(x_l, y_l)`、右上角坐标 `(x_r, y_r)` 和宽高 `w` 和 `h`。...

    程序中几何图形间的距离计算(碰撞检测)

    以下是对给定标题和描述中提到的几种几何图形间距离计算和碰撞检测方法的详细解释: 1. **矩形碰撞**: - 矩形碰撞检测通常用于游戏对象或UI元素的交互。给定两个矩形的坐标(左下角的x,y坐标以及宽度w和高度h)...

    android中矩形和圆的碰撞测试

    在Android开发中,进行图形元素的交互操作时,碰撞检测是一项重要的技术。本文将深入探讨如何在Android...无论是基本的矩形检测,还是更复杂的矩形与圆形的碰撞,都需要开发者具备扎实的几何知识和良好的算法设计能力。

    圆形与矩形碰撞易语言源码

    - 对于圆形和矩形的碰撞检测,我们可以计算圆形中心到矩形各边的距离,如果这个距离小于等于圆形的半径,那么就发生了碰撞。 易语言源码中可能包含了这些算法的实现,通过阅读和分析源码,你可以了解到如何在...

    Android范围碰撞检测

    本篇文章将深入探讨"Android范围碰撞检测",包括矩形碰撞检测和圆形碰撞检测,并以在Android 2.2系统上实现为例进行说明。 首先,我们要理解什么是碰撞检测。碰撞检测是指在二维或三维空间中,检测两个或多个物体...

    Android游戏开发之碰撞检测(矩形碰撞、圆形碰撞、像素碰撞)

    圆形碰撞检测基于几何学原理,即两个圆发生碰撞的条件是它们的圆心之间的距离小于两圆半径之和。这个检测方法适用于圆形或近似圆形的游戏对象。在实际应用中,可以通过计算两个圆心点之间的欧几里得距离并与半径之...

    碰撞检测实例代码

    首先,碰撞检测可以分为两大类:精确碰撞检测和近似碰撞检测。精确检测力求找出所有可能的接触点,而近似检测则在效率和精度之间取得平衡,通常用于实时场景。在游戏开发中,近似检测更为常见,因为它能够快速响应并...

    cocos2d碰撞检测代码

    通过创建带有物理属性的节点,我们可以设置它们的质量、摩擦力、弹性等属性,并让引擎自动进行碰撞检测和响应。例如: ```objc CCPhysicsBody *body1 = [CCPhysicsBody bodyWithCircleOfRadius:radius]; body1....

    绘制旋转矩形以及旋转矩形的碰撞检测(基于BCGCBPro)

    本主题聚焦于“绘制旋转矩形以及旋转矩形的碰撞检测”,这通常用于游戏开发、用户界面设计或者可视化应用中。我们将深入探讨以下几个方面: 1. **绘制旋转矩形**:在BCGCBPro(可能指的是Borland C++ Builder或类似...

    基于QT的C++的碰撞检测

    总的来说,这个项目展示了如何结合QT框架和C++来实现一个交互式的碰撞检测系统,允许用户通过鼠标滚轮动态调整视角,从而观察和分析碰撞情况。这涉及到图形用户界面的设计、事件处理、几何形状的碰撞检测算法以及2D...

    xna中的碰撞检测

    在2D环境中,通常涉及到矩形与矩形、圆形与圆形以及更复杂的形状之间的碰撞检测。 在XNA中,我们可以利用内置的几何图形类来处理碰撞检测。例如,`Rectangle`类用于表示矩形,`Vector2`用于表示二维向量,这些都...

    OPENGL游戏中碰撞检测

    除了基本形状和地形的碰撞检测,游戏中的动态碰撞检测更为复杂。这涉及到物体的运动状态,如速度、加速度和旋转。在这种情况下,可以使用基于时间步进的预测算法,如Sweep and Prune(扫掠与修剪)或者Broadphase、...

    《MFC游戏开发》笔记十 碰撞检测 配套源代码

    碰撞检测主要分为两个层面:几何碰撞检测和物理碰撞检测。几何碰撞检测关注的是物体形状之间的碰撞,如矩形、圆形等;物理碰撞检测则涉及到速度、重力等物理因素,更贴近真实世界。 在MFC游戏开发中,我们通常会...

    碰撞检测教程 OpenGL版

    总结一下,"碰撞检测教程 OpenGL版"涵盖了2D环境下的基本碰撞检测策略,包括包围盒检测和多边形碰撞检测。通过学习这个教程,你可以掌握如何在OpenGL项目中有效地实现碰撞检测,提高应用程序的交互性和真实性。教程...

    cocos2d-x游戏碰撞检测(中文翻译)毕设也肯定用的上

    - **精灵(Sprite)碰撞检测**:通过比较两个精灵的矩形边界进行碰撞检测,但这种方法可能不准确,因为精灵的形状可能并非矩形。 - **精确形状碰撞检测**:对于更复杂的形状,如圆形、多边形,可以使用`b2World`...

    像素碰撞检测源码

    传统的碰撞检测方法通常基于矩形或圆形等简单的几何形状,但这种方法无法精确处理复杂形状的物体。像素碰撞检测则更加精确,它会逐像素比较两个图像,找出是否有颜色重叠,从而确定是否存在碰撞。 在Cocos2d-x中,...

Global site tag (gtag.js) - Google Analytics