通过扩展 Rectangle 类以使用其 contains 和 intersects 方法来实现碰撞检测
我想知道这是否是一个好主意,如下实现矩形的简单 2D 碰撞检测:
- 创建我自己的扩展 Rectangle 类的碰撞检测类。
- 然后,在实例化该对象时,执行诸如 Collision col = new Rectangle(); 之类的操作时 <- 我应该这样做还是应该避免这样做?我知道我“可以”,但我应该吗?
- 我想扩展 Rectangle 类,因为
contains()
和intersects()
方法;我应该这样做还是应该为 Java 中的 2D 碰撞检测做其他事情?
I would like to know if this is a good idea that to implement simple 2D collision detection of rectangles as follows:
- Make my own Collision detection class that extends Rectangle class.
- Then when instantiating that object doing something such as
Collision col = new Rectangle();
<- Should I do that or is that something that should be avoided? I am aware that I 'can' but should I? - I want to extend Rectangle class because of the
contains()
andintersects()
methods; should I be doing that or should I be doing something else for 2D collision detection in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
碰撞和矩形之间不存在
is-a
关系,碰撞不是矩形。冲突域可能有矩形,建议您使用组合。There is not a
is-a
relation between a Collision and a Rectangle, a collision is not a rectangle. A collision domain may have rectangles which suggests that you use composition.您可以轻松地扩展 Rectangle,
然后您可以像这样使用它:
正如评论中所述,我没有使用 Collision 类。通常您需要它,因为您还对深度或碰撞平面等碰撞参数感兴趣,因此您会得到类似的内容:
并且该方法将返回碰撞列表:
you can easily extend
Rectangle
then you would use it like:
As noted in comments I didn't use a
Collision
class. Usually you need it because you are also interested in collision parameters like depth or colliding plane so you would have something like:and the method would return a list of collisions:
首先让它与矩形无关怎么样?创建一个新的独立碰撞模块并处理您想要的任何不同类型。
(我不懂Java,对这里的任何问题感到抱歉)
恕我直言,从矩形中进行碰撞并没有真正意义,因为碰撞是矩形的正交概念,并且将它们挤在一起不必要地限制了您未来的选择。要认识到的关键是,您实际上并不需要碰撞对象,这些函数本质上是过程性的(Java 是否有类似 C++ 的“静态”修饰符用于类作用域成员函数?)。碰撞本身不是碰撞参与者的财产。
What about making it unrelated to Rectangle in the first place? Make a new, standalone Collision module and handle any different types you want.
(I don't know Java, sorry for any issues here)
IMHO it doesn't really make sense to make a collision out of a rectangle because a collision is an orthogonal concept to the rectangle, and jamming them together needlessly constrains your future options. The key thing to realize is that you don't really need a collision object, per se, these functions are procedural by nature (does Java have something like C++'s 'static' modifier for class-scope member functions?). The collision itself isn't a property of either of the participants of a collision.