通过扩展 Rectangle 类以使用其 contains 和 intersects 方法来实现碰撞检测

发布于 2024-08-26 01:02:40 字数 336 浏览 2 评论 0原文

我想知道这是否是一个好主意,如下实现矩形的简单 2D 碰撞检测:

  1. 创建我自己的扩展 Rectangle 类的碰撞检测类。
  2. 然后,在实例化该对象时,执行诸如 Collision col = new Rectangle(); 之类的操作时 <- 我应该这样做还是应该避免这样做?我知道我“可以”,但我应该吗?
  3. 我想扩展 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:

  1. Make my own Collision detection class that extends Rectangle class.
  2. 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?
  3. I want to extend Rectangle class because of the contains() and intersects() methods; should I be doing that or should I be doing something else for 2D collision detection in Java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独岁月 2024-09-02 01:02:40

碰撞和矩形之间不存在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.

川水往事 2024-09-02 01:02:40

您可以轻松地扩展 Rectangle,

class CollidableRectangle extends Rectangle
{
  public boolean isCollidingWith(Rectangle otherRect)
  {
    //check collision
  }

  // return all collisions
  public List<CollidableRectangle> getCollisions(List<Rectangle)
  {
    // code
  }
}

然后您可以像这样使用它:

CollidableRectangle r1 = new CollidableRectangle();
CollidableRectangle r2 = new CollidableRectangle();

r1.isCollidingWith(r2);

//and so on

正如评论中所述,我没有使用 Collision 类。通常您需要它,因为您还对深度碰撞平面等碰撞参数感兴趣,因此您会得到类似的内容:

class Collision
{
   CollidableRectangle first, second;
   float depth;
   Line2D collidingLine;
}

并且该方法将返回碰撞列表:

public List<Collision> getCollisions(List<Rectangle) { ... }

you can easily extend Rectangle

class CollidableRectangle extends Rectangle
{
  public boolean isCollidingWith(Rectangle otherRect)
  {
    //check collision
  }

  // return all collisions
  public List<CollidableRectangle> getCollisions(List<Rectangle)
  {
    // code
  }
}

then you would use it like:

CollidableRectangle r1 = new CollidableRectangle();
CollidableRectangle r2 = new CollidableRectangle();

r1.isCollidingWith(r2);

//and so on

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:

class Collision
{
   CollidableRectangle first, second;
   float depth;
   Line2D collidingLine;
}

and the method would return a list of collisions:

public List<Collision> getCollisions(List<Rectangle) { ... }
薄荷→糖丶微凉 2024-09-02 01:02:40

首先让它与矩形无关怎么样?创建一个新的独立碰撞模块并处理您想要的任何不同类型。

(我不懂Java,对这里的任何问题感到抱歉)

class Collision
{
   public boolean BetweenRectangles(Rectangle a, Rectangle b)
   {
   }

   public boolean BetweenCircles(Circle a, Circle b)
   {
   }

   public boolean RectangleToCircle(Rectangle r, Circle c)
   {
   }

   public boolean MyCrazyShapeToRectangle(MyCrazyShape mcs, Rectangle r)
   {
   }
}

恕我直言,从矩形中进行碰撞并没有真正意义,因为碰撞是矩形的正交概念,并且将它们挤在一起不必要地限制了您未来的选择。要认识到的关键是,您实际上并不需要碰撞对象,这些函数本质上是过程性的(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)

class Collision
{
   public boolean BetweenRectangles(Rectangle a, Rectangle b)
   {
   }

   public boolean BetweenCircles(Circle a, Circle b)
   {
   }

   public boolean RectangleToCircle(Rectangle r, Circle c)
   {
   }

   public boolean MyCrazyShapeToRectangle(MyCrazyShape mcs, Rectangle r)
   {
   }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文