We are about to switch to a new forum software. Until then we have removed the registration on this forum.

- All Categories 25.7K
- Announcements & Guidelines 13
- Common Questions 30
- Using Processing 22.1K
- Programming Questions 12.2K
- Questions about Code 6.4K
- How To... 4.2K
- Hello Processing 72
- GLSL / Shaders 292
- Library Questions 4K
- Hardware, Integration & Other Languages 2.7K
- Kinect 668
- Arduino 1K
- Raspberry PI 188
- Questions about Modes 2K
- Android Mode 1.3K
- JavaScript Mode 413
- Python Mode 205
- Questions about Tools 100
- Espanol 5
- Developing Processing 548
- Create & Announce Libraries 211
- Create & Announce Modes 19
- Create & Announce Tools 29
- Summer of Code 2018 93
- Rails Girls Summer of Code 2017 3
- Summer of Code 2017 49
- Summer of Code 2016 4
- Summer of Code 2015 40
- Summer of Code 2014 22
- p5.js 1.6K
- p5.js Programming Questions 947
- p5.js Library Questions 315
- p5.js Development Questions 31
- General 1.4K
- Events & Opportunities 289
- General Discussion 365

I would like to check if there is an overlap between two random shapes.

First I played around with Fisica / Box2d. But it seems that collision detection does not work for two shapes created in the same place, since two things can't exist in the same place in the real world. And simulating reality is what these libraries were made for. The detection only works if there is a real collision by two objects that were moving towards each other.

One way is to draw the shapes into two PGraphics and compares their pixel arrays afterwards. But this seems to be a bit naive. Is there any better of doing this?

Thanks!

Tagged:

## Answers

You need to be more specific regarding the shapes. For simple regular geometric shapes such as rectangle, circles, lines there are mathematical formula for testing overlap. For irregular shapes e.g. polygons the maths becomes much harder.

Images (without transparency) can be treated as rectangles when calculating overlap so again it is straightforward. For images where some of the pixels are transparent (e.g. sprites used in games) there is no simple solution but to test pixel against pixel.

This

linkprovides a number of methods for calculating intersections between regular shapes.For sprite data you could use the

Spriteslibrary which has several methods for detecting collisions.Thanks for pointing out your solutions!

But yes, I would actually like to check the overlap of irregular polygons.

To determine whether two irregular polygons overlap is a fairly straight forward algorithm but for efficiency you need to know the position of the bounding rectangle for each irregular polygon.

The bounding rectangle would be defined as [minX, minY, maxX, maxY] where

minX, maxX are the smallest and largest x values of the polygon's vertices, and

minY, maxY are the smallest and largest y values of the polygon's vertices.

So now the algorithm to determine if two polygons P0 and P1 overlap

This

Geometry 2D Cookbookprovides methods for testing rectangle-rectangle and line-line intersections.Thanks so much, this looks really great!

Meanwhile I already implemented the pixel array comparison. If this brings up any issues, I'll happily come back to your more grown up solution!

I also stumbled across the intersect() function of the Geomerative Processing library, which should probably do exactly what you described.