如何确定圆与矩形的碰撞? [英] How to determine the collision of a circle with a rectangle?

查看:295
本文介绍了如何确定圆与矩形的碰撞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过每个领导者如何让这个工作(很好),到目前为止我所提出的每个解决方案都是丑陋或没有工作。我有一个圆形的精灵,敌人。

I have looked EVERYWHERE for leads on how to get this to work (nicely) and so far every solution I have come up with has been ugly or didn't work. What I have is a circular sprite, the enemy. Then I have a sprite that is in the shape of an arrow.

当检查敌人的箭头碰撞时,我使用 CGRectIntersect rect1,rect2)
但是...圆圈不是矩形!碰撞是令人厌恶的。

When checking for collision for the arrow in the enemy, I use CGRectIntersect(rect1, rect2) but... circles are not rectangles! The collision is disgusting.

所以我的问题是,我如何去检查圆形对象内的碰撞?

So my question is, how do I go about checking for collision inside a circular object? Should I make many rects, or is there something out there that is made for this purpose?

推荐答案

检测圆的碰撞一个矩形就是简单的东西。以下是执行此类测试的 C ++示例类有大量其他交集测试示例的源代码)。还有一个只显示伪代码的 SO的答案。 N +开发人员还解释他们的圆形与矩形碰撞检测方法。如果这对你似乎太多了,你特此建议寻找一个更简单的方法。 ;)

Detecting the collision of a circle and a rectangle is anything but simple. Here's a C++ example class that does this kind of test (source with plenty of other intersection test examples). There's also an answer on SO that only shows pseudo-code. The N+ developers also explain their approach to circle vs rectangle collision detection. If any of this seems too much for you, you're hereby advised to look for a simpler approach. ;)

例如,由于你提到箭头,这意味着一个尖的,薄的对象往往在一个方向相对直线飞行,箭头总是指向飞行方向。如果不是这样,我可能一直住在不同的星球上,否则我会用这个假设。

For example, since you mentioned "arrow" that implies a pointed, thin object that tends to fly relatively straight in one direction, with the arrowhead always being pointed in the direction of flight. If that is not the case, I may be have been living on a different planet, otherwise I'll use this assumption.

这意味着你可以很容易地改变碰撞箭头的类型从矩形到圆。圆圈只需要大到使它包围箭头。根据图形和游戏设计,在箭头的尖端处有一个碰撞圆可能就足够了。然后你可以实现Joshua的圆与圆碰撞测试的建议。

It means you can very easily change the collision type of the arrow from rectangle to circle. The circle only needs to be as big so that it encloses the arrowhead. Depending on graphics and your game design it may be even sufficient to have a collision circle at the very tip of the arrowhead. Then you can implement Joshua's suggestion of circle vs. circle collision tests.

一个替代非常细的箭头将是假设箭头是一条线,然后你可以工作具有相当简单的线 - 圈交叉点测试

An alternative for very thin arrows would be to assume the arrow to be a line, then you can work with a reasonably simple line-circle intersection test.

这篇关于如何确定圆与矩形的碰撞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆