How to check if a shape is touching another shape?

How do I check if a shape is touching another shape in p5? I tried something like this:

if (shape1.x && shape1.y === shape2.x && shape2.y) {
    //other code
}

but it doesn't work.

Tagged:

Answers

  • Answer ✓

    It really depends on what kinda shape your talking about, for circles you can use the dist and check the distance between the two shapes with something like this

    if ( dist(circleOneX, circleOneY, circleTwoX, circleTwoY) > circleOneRadius + circleTwoRadius) {
    //other code
    }
    

    But to really help I have to know shapes your using

  • It really depends on what kinda shape your talking about, for circles you can use the dist and check the distance between the two shapes with something like this

    I'm using two circles, one circle can move, the other one can't. I want to see if the first circle is touching the other so then I can do something with the second one.

  • edited October 2017

    From the guide that @koogs linked to in the FAQ, there is an entry "circle/circle":

    http://www.jeffreythompson.org/collision-detection/circle-circle.php

  • edited November 2017

    When I did some simple collision detection in C++ it looked like this. You can apply the same logic BUT, I am not familiar enough with P5js yet to offer a more "built-in" alternative such as distance metrics using dist()
    My Pseudo:

    if ((obj1.leftV <= obj2.rightV) && 
    (obj1.bottomV >= obj2.topV) &&  
    (obj1.topV <= obj2.bottomV) && 
    (obj1.bottomV >= obj2.topV)) 
    

    Essentially you derive the points or vectors you would like to compare (x, y)
    leftV / rightV - implies x
    topV / bottomV - implies y

  • @Mostly_Yoshi -- @Nautilus requested circle-circle collision detection, but you gave the example of rectangle-rectangle collision detection. That won't work here.

  • @jeremydouglass true, its certainly not optimal for elliptical shapes. The idea is still apparent I suppose.

Sign In or Register to comment.