Using polymorphism to detect collisions in an elegant way

I am trying to customize some simple 2D shapes that can be dragged around the window with the mouse. I want the shapes to record a collision when I drag one into the other. I have an interface.

interface ICollidable { bool CollidedWith(Shape other); } 

Then I have an abstract Shape class that implements the above interface.

 abstract class Shape : ICollidable { protected bool IsPicked { private set; get; } protected Form1 Form { private set; get; } protected int X { set; get; } // Usually top left X, Y corner point protected int Y { set; get; } // Used for drawing using the Graphics object protected int CenterX { set; get; } // The center X point of the shape protected int CenterY { set; get; } // The center X point of the shape public Shape(Form1 f, int x, int y) { Form = f; X = x; Y = y; Form.MouseDown += new MouseEventHandler(form_MouseDown); Form.MouseMove += new MouseEventHandler(Form_MouseMove); Form.MouseUp += new MouseEventHandler(Form_MouseUp); } void Form_MouseMove(object sender, MouseEventArgs e) { if(IsPicked) Update(e.Location); } void Form_MouseUp(object sender, MouseEventArgs e) { IsPicked = false; } void form_MouseDown(object sender, MouseEventArgs e) { if (MouseInside(e.Location)) IsPicked = true; } protected abstract bool MouseInside(Point point); protected abstract void Update(Point point); public abstract void Draw(Graphics g); public abstract bool CollidedWith(Shape other); } 

Then I have ten concrete classes of Circle, Square, Rectangle, etc. that extend the Shape class and implement abstract methods. What I would like to do is come up with a clean and elegant way to do collosion detection instead of having a large block of if statements in a CollidedWith method such as

 public bool CollidedWith(Shape other) { if(other is Square) { // Code to detect shape against a square } else if(other is Triangle) { // Code to detect shape against a triangle } else if(other is Circle) { // Code to detect shape against a circle } ... // Lots more if statements } 

Does anyone have any ideas. This is a problem that I thought about before, but now I’m only practicing.

+6
source share
3 answers

Is collision detection a β€œSpecific Format” that there is a different implementation for each permutation

 Circle vs. Other Circle Circle vs. Other Square Circle vs. Other Triangle Square vs. Other Circle ... 

It looks like you are trying to create a matrix of all the possibilities, but if you come up with 10 new forms, a total of 20, you will have 400 possibilities.

Instead, I would try to propose a general Shape.Overlaps(Shape other) method in your abstract class that would satisfy them all.

If it's just 2D geometry, if it's trivial to determine if the boundary paths of any shapes intersect.

+4
source

Instead of specific forms, all of them are ALL Paths or Regions .

A square is just a poly with 4 points that are at right angles. Then just write one Path.CollidesWith(Path) method and get in the way.

Take a look questions .

+1
source

Source: https://habr.com/ru/post/924774/


All Articles