vendredi 25 octobre 2024

2D pinball physic's engine

 




//flipper

class flipper {

float R,r,D,phi0;

float phi,omega;

float deltaphi=PI/2.6;

float choc=1.2;

float Vpinball = 25;

vec2 v,vt;

vec2 o,m;

vec2 a1,b1,a2,b2;

wall w1,w2 ;

circle c1,c2;

 //bar() { };

 

 flipper(vec2 io,float iR,float ir,float iD,float iphi0) {

   o = new vec2(io.x,io.y); 

   R =iR; r = ir; D = iD; phi0 = iphi0;

   m = new vec2(D,0);

 // rotate the bar  

   m = add(o,m);

   m = rot(m,o,phi0);

 // compute the two walls

  float L = sqrt(D*D-pow(R-r,2));

  a1 = new vec2();

  b1 = new vec2();

  a1.x = R*(R-r)/D+o.x; a1.y = R*L/D+o.y;

  b1.x = r*(R-r)/D+D+o.x; b1.y = r*L/D+o.y;

  a1 = rot(a1,o,phi0); b1 = rot(b1,o,phi0);

  w1 = new wall(a1.x,a1.y,b1.x,b1.y);

  a2 = new vec2();

  b2 = new vec2();

  a2.x = R*(R-r)/D+o.x; a2.y = -R*L/D+o.y;

  b2.x = r*(R-r)/D+D+o.x; b2.y = -r*L/D+o.y;

  a2 = rot(a2,o,phi0); b2 = rot(b2,o,phi0);

  w2 = new wall(a2.x,a2.y,b2.x,b2.y); 

  c1 = new circle(o.x,o.y,R);c1.choc =0.5;

  c2 = new circle(m.x,m.y,r);c2.choc =0.5;

   v =new vec2(0,0);

   vt =new vec2(0,0);

   omega = 5;

 }


void draw() {

  strokeWeight(1);

  stroke(0,255,0);

 fill(255,0,0,100);

 beginShape();

vertex(w1.a.x,w1.a.y);

vertex(w1.b.x,w1.b.y);

vertex(w2.b.x,w2.b.y);

vertex(w2.a.x,w2.a.y);

endShape(CLOSE);

  noFill();

  w1.wg=2; w2.wg = 2;

  w1.setcolor(255,0,0);

  w2.setcolor(255,0,0);

 w1.draw();w2.draw();

  //stroke(255,0,0);

  strokeWeight(1);

  fill(255,0,0);

  ellipse(o.x,o.y,2*R,2*R);

  ellipse(m.x,m.y,2*r,2*r);

 fill(255);  stroke(255);  strokeWeight(2);

  ellipse(o.x,o.y,R,R);

  ellipse(m.x,m.y,r,r);

}


void  update_motion(float t) {

    a1 = rot(a1,o,omega*t);

    b1 = rot(b1,o,omega*t);

    w1.a.x =a1.x;w1.a.y =a1.y;w1.b.x =b1.x;w1.b.y=b1.y;

    w1.omega = omega;

    a2 = rot(a2,o,omega*t);

    b2 = rot(b2,o,omega*t);

    w2.a.x =a2.x;w2.a.y =a2.y;w2.b.x =b2.x;w2.b.y=b2.y;

    w2.omega = omega;

    m = rot(m,o,omega*t);

    c2.p.x = m.x;c2.p.y=m.y;

    phi+=omega*t;

  }

  boolean touch_left() {

    for ( int i=0;i< touches.length;i++) 

    if (touches[i].x>width/2) return true;

    return false;

  }

   boolean touch_right() {

    for ( int i=0;i< touches.length;i++) 

    if (touches[i].x<width/2) return true;

    return false;

  }

   void control_right( char c) {

   if (/*touchIsStarted*/touch_left() ) {

   // if (!sound_pinball.isPlaying()) sound_pinball.play();


    if (phi>=phi0+deltaphi) omega=0; else omega=Vpinball;

  } else {

    if (phi<=phi0) omega=0; else omega=-Vpinball;

   }}

  

   void control_left( char c) {

      if (/*touchIsStarted*/ touch_right()) {

     //   sound_pinball.stop();

     // if (!sound_pinball.isPlaying()2) sound_pinball.play();


      

    if (phi<=(phi0-deltaphi)) omega=0; else omega=-Vpinball;

  } else {

    if (phi>=phi0) omega=0; else omega=Vpinball;

  }

   }

boolean collision(ball b) {

    

    

    return w1.collision(b) || w2.collision(b) || c1.collision(b) || c2.collision(b);

}


}