collision with lines

Hello I am a processing noob, i cant get a line to collide with my ball and this is my final project for my class i really need some help please.

Tagged:

Answers

  • edited May 2017

    Read Jeffrey Thompson's tutorial on "line-circle" collision detection:

    ...and if you need more advice, please share your code here (format according to the forum formatting instructions) and explain what you tried and what still isn't working.

  • I am trying to make these lines collide with the ellipse that is the mouse. I know i shouldve used an array but im not really concerned with it right now just tying to hand this in ASAP. :)

    int radius = 10, directionX = 1, directionY = 0; float x=20, y=20, speed=0.5;

    int line1= 20; int line1x = 40; int line2 = 80; int line2x = 100; int line3 = 120; int line3x = 140; int line4 = 180; int line4x = 200; int line5 = 220; int line5x = 240; int line6 = 280; int line6x = 300; int line7 = 320; int line7x = 340; int line8 = 380; int line8x = 400; int line9 = 420; int line9x = 440; int line10 = 480; int line10x = 500; int line11 = 520; int line11x = 540; int line12 = 580; int line12x = 600;

    int eD = 20;

    int eX = mouseX; int eY = mouseY; void setup() { size(900,500); smooth(); noStroke(); ellipseMode(RADIUS); }

    void draw() { background(0); // changing Position x=x+speeddirectionX; y=y+speeddirectionY; // Boundary Checker if ((x>width-radius) || (x<radius)) {
    directionX=-directionX; } if ((y>height-radius) || (y<radius)) {
    directionY=-directionY; } fill (color(245,130,7)); ellipse (mouseX, mouseY, radius, radius); // Liger noStroke();

    // LINES stroke(255);

    line(line1,30,line1x,30); line1= line1+3; line1x= line1x+3;

    if(line1 > width){ line1 -= width; if (line1x > width) line1x -= width; }

    if (dist(line1,line1x,eX,eY) < eD/2) { background(255); }

    line(line2,70,line2x,70); line2= line2+5; line2x= line2x+5;

    if(line2 > width){ line2 -= width; if (line2x > width) line2x -= width; }

    line(line3,110,line3x,110); line3= line3+4; line3x= line3x+4;

    if(line3 > width){ line3 -= width; if (line3x > width) line3x -= width; }

    line(line4,150,line4x,150); line4= line4+8; line4x= line4x+8;

    if(line4 > width){ line4 -= width; if (line4x > width) line4x -= width; }

    line(line5,190,line5x,190); line5= line5+7; line5x= line5x+7;

    if(line5 > width){ line5 -= width; if (line5x > width) line5x -= width; }

    line(line6,230,line6x,230); line6= line6+5; line6x= line6x+5;

    if(line6 > width){ line6 -= width; if (line6x > width) line6x -= width; }

    line(line7,270,line7x,270); line7= line7+4; line7x= line7x+4;

    if(line7 > width){ line7 -= width; if (line7x > width) line7x -= width; }

    line(line8,310,line8x,310); line8= line8+9; line8x= line8x+9;

    if(line8 > width){ line8 -= width; if (line8x > width) line8x -= width; }

    line(line9,350,line9x,350); line9= line9+5; line9x= line9x+5;

    if(line9 > width){ line9 -= width; if (line9x > width) line9x -= width; }

    line(line10,390,line10x,390); line10= line10+6; line10x= line10x+6;

    if(line10 > width){ line10 -= width; if (line10x > width) line10x -= width; }

    line(line11,430,line11x,430); line11= line11+5; line11x= line11x+5;

    if(line11 > width){ line11 -= width; if (line11x > width) line11x -= width; }

    line(line12,470,line12x,470); line12= line12+7; line12x= line12x+7;

    if(line12 > width){ line12 -= width; if (line12x > width) line12x -= width; }

    }

  • i dont know how to format the code

  • Please edit your post (gear icon in the top right corner of your post), select your code and hit ctrl+o to format your code. Make sure there is an empty line above and below your code.

    Kf

  • make an array of your lines and use the array and a for loop to display the lines

    then use a for loop to loop over the lines and use dist() to check the distance to mouseX,mouseY

Sign In or Register to comment.