JimBrown
YaBB Newbies
Offline
Posts: 4
Re: 2D Pool
Reply #1 - May 5th , 2008, 7:43am
PART 2
Code: // ############################################################# // MAIN LOOP void draw(){ background(60,140,80); UpdateBallPhysics(); RenderBalls(); // only if all balls are stationary if (ballsmoving==false){ mx=mouseX ; my=mouseY; DrawAimLine() ; DrawPowerBar(); // power up and shoot if (mousePressed && (mouseButton == LEFT)) { power=power+0.3+(power*0.04); if (power > 50.0){ power=50.0; } }else{ int ang=angle(ball[0].x,ball[0].y,mx,my); ball[0].dx= sin(radians(ang)) * power; ball[0].dy=-cos(radians(ang)) * power; power=0; } // reset if (mousePressed && (mouseButton == RIGHT)) { ballcount=0; SetupCueBall() ; SetupTriangle(); } } } void DrawAimLine(){ stroke(200,200,200); line(ball[0].x,ball[0].y,mx,my); noFill(); ellipse(mx-ball[0].radius, my-ball[0].radius, ball[0].radius*2, ball[0].radius*2); stroke(0); } void DrawPowerBar(){ fill(0); rect(sw*0.3-1,sh-16,50*4+2,10); fill(color(170,251,206)); rect(sw*0.3,sh-15,power*4,8); fill(color(200,200,200)); //DrawText "[LMB] Shoot [RMB] Reset [Esc] Exit",10,10 } // ############################################################# void SetupCueBall(){ ball[0] = new ballType(); ball[0].x = sw-BALL_RADIUS-24; ball[0].y = sh/2; ball[0].dx=0.0 ; ball[0].dy=0.0; ball[0].radius = BALL_RADIUS; ball[0].mass = BALL_MASS; ball[0].r=255 ; ball[0].g=255 ; ball[0].b=255; } void SetupTriangle(){ final int ballTriangleSize=5; float ballrad2=BALL_RADIUS*2; ballcount=0; for(int xloop=ballTriangleSize;xloop>0;xloop--){ for(int yloop=1;yloop<xloop+1;yloop++){ ballcount++; ball[ballcount] = new ballType(); ball[ballcount].radius = BALL_RADIUS; ball[ballcount].mass = BALL_MASS; ball[ballcount].x = (4-xloop)*ballrad2 + 100; ball[ballcount].y = (yloop*ballrad2) - (xloop*ballrad2)/2.0 + (sh/2); ball[ballcount].r= (int) random(255); ball[ballcount].g= (int) random(255); ball[ballcount].b= (int) random(255); } } }