detect collision betw circles when 1 is rotated around exterior point?

edited December 2017 in Questions about Code

Hey Sages,

I'm trying to make a game in which an image (a humanoid figure) can be flown through space to grab a circular gem. The figure can be accelerated and rotated as it moves. The figure's hand includes a "hit circle" centered on the hand with a given diameter. When the hit circle collides with the gem, the gem should disappear.

The following code gives me a hit circle which positions correctly as the figure moves and rotates. (The hand is to the right and below the center of the image at setup.)

  imageMode(CENTER); 
  pushMatrix(); 
  translate(figX, figY); 
  rotate(radians(figAng)); 
  image (figViz, 0, 0); 
  ellipse(handX, handY, handDia, handDia); 
  popMatrix(); 

I was doing fine until I tried to use dist() to check for a collision:

    if (dist(figX + handX, figY + handY, gemX, gemY) < gemDia/2 + handDia/2 ) {
        gotGem = true;
        println("Huzzah!");
    }

This does not produce a collision whether I check within the push/popMatrix() code or outside of that code. Can anyone suggest what I am doing incorrectly or direct me to some resource that explains how to detect collisions between circles when one is being rotated around an exterior point?

Thanks much, L

Tagged:

Answers

  • void setup(){
      size(440,440);
    }
    
    void draw(){
      pushMatrix();
      translate(mouseX, mouseY);
      rotate(map(millis()%5000,0,5000,0,TWO_PI));
      float blueX = screenX(100,0);
      float blueY = screenY(100,0);
      popMatrix();
    
      boolean over = dist(220,220,blueX,blueY) < 60;
    
      background(0);
    
      stroke(0,255,0);
      line(0,0,blueX,blueY);
      line(width,0,blueX,blueY);
      line(0,height,blueX,blueY);
      line(width,height,blueX,blueY);
    
      noStroke();
      fill(255,over?255:0,0);
      ellipse(220,220,100,100);
      pushMatrix();
      translate(mouseX, mouseY);
      rotate(map(millis()%5000,0,5000,0,TWO_PI));
      stroke(255);
      line(0,0,100,0);
      noStroke();
      fill(0,0,255);
      ellipse(100,0,20,20);
      popMatrix();  
    }
    
  • Fantastic - thanks so much! I didn't know about screenX/Y() or the syntax you use for the boolean, but your code lays it out so clearly I was able to connect all the dots. Will report back on the implementation.

Sign In or Register to comment.