1 / 15

int screenDim =500; Dimension of the Screen in the middle which pattern move inside

int screenDim =500; Dimension of the Screen in the middle which pattern move inside int leftCornerx =390; Where screen starts ( X) int leftCornery =150; Where screen starts (Y) int w=1280; The Width of the Display window int h=800; The Height of the display window

ayita
Download Presentation

int screenDim =500; Dimension of the Screen in the middle which pattern move inside

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. intscreenDim=500; Dimension of the Screen in the middle which pattern move inside intleftCornerx=390; Where screen starts ( X) intleftCornery=150; Where screen starts (Y) int w=1280; The Width of the Display window int h=800; The Height of the display window intmaxradius=25; Maximum radius of the circles intmaxvelocity=5; The Maximum speed of the circles intnumberofBalls=500;

  2. PVectorvels[]=new PVector[numberofBalls]; We are adding a class(Pvector)which is Used for two or three dimensional vector. Here our vector is velocity. Then we assign an int which is “number of balls” to that class. Ball balls[]=new Ball[numberofBalls]; Another class for balls. ( Logic the same as above) import processing.serial.*; We need a serial library to read from Arduino intval=-1; An Int which read the arduino result. ( 0,1 or 2 ) Serial port; The port that is used for reading processing from arduino void setup(){ size(w,h); port = new Serial(this, Serial.list()[0], 2400); ( Reading the first serial port from arduino) smooth(); noStroke();

  3. for(inti=0; i<numberofBalls;i++){ balls[i]=new Ball(int(random(leftCornerx,leftCornerx+screenDim)),int(random(leftCornery,leftCornery+screenDim)),int(random(0,maxradius)), color(0,200,0,100)); } for(inti=0; i<numberofBalls;i++){ vels[i]=new PVector(int(random(1,maxvelocity)),int(random(1,maxvelocity))); } } For each of the 500 balls that we have do these functions : Apply randomness to the position of the balls inside of the screen which is in the middle and then assign randomness also to the radius of the balls. Also for the velocity does the randomness.

  4. void draw(){ serialEvent(port.read()); background(255); for(inti=0; i<numberofBalls;i++){ if(val==0&&balls[i].x>540&&balls[i].x<740&&balls[i].y>100&&balls[i].y<300) balls[i].c=color(200,0,0,100); if(val==1&&balls[i].x>340&&balls[i].x<540&&balls[i].y>300&&balls[i].y<500) balls[i].c=color(200,0,0,100); if(val==2&&balls[i].x>740&&balls[i].x<940&&balls[i].y>300&&balls[i].y<500) balls[i].c=color(200,0,0,100); } For data that we receive from arduino we have three options (0/1/2) if processing recognized each of them then Change the color within the screen that we assigned

  5. for(inti=0; i<numberofBalls;i++){ fill(balls[i].c); ellipse(balls[i].x,balls[i].y,balls[i].r,balls[i].r); balls[i].x=balls[i].x+vels[i].x; balls[i].y=balls[i].y+vels[i].y; checkBoundaryCollision(balls[i], vels[i],i); } Read each of the 500 balls first. Each of them are ellipses. The first two numbers are the positions and the two last ones are the radius of the ellipses. Then it is getting distance between balls components.

  6. for(inti=0; i<numberofBalls;i++){ for(int j=0; j<numberofBalls;j++){ if(i!=j){ Ball[] ballspairs={balls[i],balls[j]}; PVector[] velspairs={vels[i],vels[j]}; checkObjectCollision(ballspairs, velspairs, i,j); } } } for(inti=0; i<numberofBalls;i++){ balls[i].c=color(0,200,0,100); } } Choose color For balls between 1 to 500

  7. class Ball{ float x, y, r,m ; color c; // default constructor Ball() { } Ball(float x, float y, float r, color c) { this.x = x; this.y = y; this.r = r; this.c=c; m = r*.1; } } FloatX and Y are x and y coordinate of the location of the center for balls //r = radious of the ball //c= color of the ball

  8. void checkBoundaryCollision(Ball ball, PVectorvel, inti) { if ((ball.x <leftCornerx)|| (ball.x>leftCornerx+screenDim)) { vels[i].x *= -1; if (ball.x <leftCornerx)ball.x=leftCornerx; else ball.x=leftCornerx+screenDim; } if (ball.y < leftCornery || ball.y>leftCornery+screenDim) { vels[i].y *= -1; if (ball.y <leftCornery)ball.y=leftCornery; else ball.y=leftCornery+screenDim; } } We are giving a boundary for collision of balls So if the x and y coordinates are not situated in the boundary between 390<x<890 and 150<y<650 then put them in this boundary

  9. void checkObjectCollision(Ball[] b, PVector[] v, inti , int j){ components PVectorbVect = new PVector(); bVect.x = b[1].x - b[0].x; bVect.y = b[1].y - b[0].y; Here we are making a void for checking object collision, to get the distance between the balls float bVectMag = sqrt(bVect.x * bVect.x + bVect.y * bVect.y); if (bVectMag < b[0].r + b[1].r){ We are calculating magnitude of the vector separating the balls float theta = atan2(bVect.y, bVect.x); This is to get angle of bVect float sine = sin(theta); float cosine = cos(theta); These are triangular values

  10. Ball[] bTemp = { new Ball(), new Ball() }; bTemp is holding positions of rotated balls bTemp[1].x = cosine * bVect.x + sine * bVect.y; bTemp[1].y = cosine * bVect.y - sine * bVect.x; * b[1]'s position is relative to b[0]'s so you can use the vector between them (bVect) as the reference point in the rotation expressions. bTemp[0].x and bTemp[0].y will initialize automatically to 0.0, which is what you want since b[1] will rotate around b[0] */

  11. PVector[] vTemp = { new PVector(), new PVector() }; vTemp[0].x = cosine * v[0].x + sine * v[0].y; vTemp[0].y = cosine * v[0].y - sine * v[0].x; vTemp[1].x = cosine * v[1].x + sine * v[1].y; vTemp[1].y = cosine * v[1].y - sine * v[1].x; Rotate the velocities with triangular values PVector[] vFinal = { new PVector(), new PVector() Now that velocities are rotated, you can use 1D conservation of momentum equations to calculate the final velocity along the x-axis.

  12. vFinal[0].x = ((b[0].m - b[1].m) * vTemp[0].x + 2 * b[1].m * vTemp[1].x) / (b[0].m + b[1].m); vFinal[0].y = vTemp[0].y; vFinal[1].x = ((b[1].m - b[0].m) * vTemp[1].x + 2 * b[0].m * vTemp[0].x) / (b[0].m + b[1].m); vFinal[1].y = vTemp[1].y; final rotated velocity bTemp[0].x += vFinal[0].x; bTemp[1].x += vFinal[1].x;

  13. Ball[] bFinal = { new Ball(), new Ball() }; bFinal[0].x = cosine * bTemp[0].x - sine * bTemp[0].y; bFinal[0].y = cosine * bTemp[0].y + sine * bTemp[0].x; bFinal[1].x = cosine * bTemp[1].x - sine * bTemp[1].y; bFinal[1].y = cosine * bTemp[1].y + sine * bTemp[1].x; Rotate ball positions and velocities back Reverse signs in trig expressions to rotate in the opposite direction rotate balls

  14. balls[j].x = b[0].x + bFinal[1].x; balls[j].y = b[0].y + bFinal[1].y; balls[i].x = b[0].x + bFinal[0].x; balls[i].y = b[0].y + bFinal[0].y; update balls to screen position vels[i].x = cosine * vFinal[0].x - sine * vFinal[0].y; vels[i].y = cosine * vFinal[0].y + sine * vFinal[0].x; vels[j].x = cosine * vFinal[1].x - sine * vFinal[1].y; vels[j].y = cosine * vFinal[1].y + sine * vFinal[1].x; } } update velocities

  15. void serialEvent(int serial) { println(serial); if(serial>47){ counter=0; val=serial-48; println(val); } if(serial==-1){ counter=counter+1; } if(counter>10){ val=-1; } } This code is to get the information from arduino and transform it actual numbers 0,1,2,3

More Related