views:

482

answers:

3

I would like to know if this is a good idea that conforms to best practices that does not lead to obscenely confusing code or major performance hit(s):

  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?
+1  A: 

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) { ... }
Jack
About "obscenely confusing code," notice that Jack called his class CollidableRectangle instead of Collision; it's fairly clear that a CollidableRectangle is a kind of Rectangle, but it's not clear that a Collision is a kind of Rectangle.
Lord Torgamus
good example, although I don't like the getCollisions method (name), as you are not returning collisions
Fortega
Yes, in a ready example you would have your own __Collision__ class, also if it implies some overhead (maybe many object allocations) but usually you need to store some collision data (example: the depth of the collision or the colliding plane/point)
Jack
+5  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.

msw
A: 

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.

dash-tom-bang