Collision between Objects

edited May 2016 in How To...

I want to make my ellipse disappear when colliding with the walls of the maze (which are made up of arcs). Any tips?

Here's the code:

//Up to increase number of levels
//Down to decrease number of levels

int a=500;


void setup() {
  size(550,500);
}

int nlevels=5;
int ellipse=5;


void draw() {
  translate(0,20);
  background(255);
  noFill();



  if (nlevels<33) {
      strokeWeight(3);
    }
    else if  (nlevels<50) {
      strokeWeight(2);
    }
    else {
      strokeWeight(1);
    }
  print(nlevels);
  float dist=a/nlevels*0.8;
  //1
  line(a/2,(a/2)-dist/2,a/2,(a/2)+dist/2);
  line((a/2)-dist/2,a/2,(a/2)+dist/2,a/2);
  //2
  arc(a/2+dist/4,a/2-dist/2,dist/2,dist/2,PI,TWO_PI);
  for (int i=0;i<nlevels;i++) {
    arc(a/2-dist/2,a/2-dist/2,(dist*i),(dist*i),PI/2,PI);
    arc(a/2+dist/4,a/2-dist/2,(1.5*dist)+(dist*i),(1.5*dist)+(dist*i),PI,TWO_PI);
    arc(a/2+dist/2,a/2-dist/2,dist+(dist*i),dist+(dist*i),0,HALF_PI);
    line(a/2,(a+dist*(i-1))/2,a/2,(a+dist*i)/2);
    if (i%2==0) {
      if (i!=nlevels-1) {
        line(a/2-dist/2,a/2+(i*dist/2),a/2+dist/2,a/2+(i*dist/2));
      } else {
        line(a/2,(a+dist*i)/2,a/2+dist/2,(a+dist*i)/2);
      }
    }
  }

  fill(0);
  ellipse(mouseX,mouseY,20,20);

}

void keyPressed() {
  if (key == CODED) {
    switch (keyCode) {
      case UP:
      nlevels=min(83,nlevels+1);
      ellipse=min(83,ellipse+1);
      break;
      case DOWN:
      nlevels=max(5,nlevels-1);
      ellipse=max(5,ellipse-1);
      break;
    }
  }
}
Tagged:

Answers

  • Tip #1: Edit your post.

    Tip #2: Select your code.

    Tip #3: Press Ctrl + o.

  • I'm confused, what is it that I am supposed to follow on this guide?

  • you need to vectorize your dungeon and apply physics to the walls and the character. a good start is to look at this function that determines whether there is a collision between a segment and a circle, the second step should be the resolution of the collision but to start with and have to try;)

    This function only works with rectangles and circles, is the simplest way to detect collision in 2d between circle "character" with maze "walls".

    /********************************************************
    isCollidingCircleRectangle()
    
      params:
       circleX - center x coordinate for circle
       circleY - center y coordinate for circle
       radius  - radius of circle
       rectangleX - top left corner X coordinate
       rectangleY - top left corner Y coordinate
       rectangleWidth - width of rectangle
       rectangleHeight - and the height
    
     returns boolean - whether the two shapes are colliding
    
     code adapted from:
       http://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection
     adapted by: Jonathan Cecil
    ********************************************************/
    final boolean isCollidingCircleRectangle(
          float circleX,
          float circleY,
          float radius,
          float rectangleX,
          float rectangleY,
          float rectangleWidth,
          float rectangleHeight)
    {
        float circleDistanceX = abs(circleX - rectangleX - rectangleWidth/2);
        float circleDistanceY = abs(circleY - rectangleY - rectangleHeight/2);
    
        if (circleDistanceX > (rectangleWidth/2 + radius)) { return false; }
        if (circleDistanceY > (rectangleHeight/2 + radius)) { return false; }
    
        if (circleDistanceX <= (rectangleWidth/2)) { return true; }
        if (circleDistanceY <= (rectangleHeight/2)) { return true; }
    
        float cornerDistance_sq = pow(circleDistanceX - rectangleWidth/2, 2) +
                             pow(circleDistanceY - rectangleHeight/2, 2);
    
        return (cornerDistance_sq <= pow(radius,2));
    }
    
  • I guess you could use dist()?

Sign In or Register to comment.