mercredi 31 mars 2021

pinball physic's engine developped with processing language



pinball
 قمت في اوقات الفراغ ببرمجته في المقهى على جهازي الهاتف النقال محرك فيزيائي ثم بعد ذلك تم نقل البرنامج على
منصة 
windows
 

During my free time I developed on my smartphone a pinball physic's engine  using processing apde application and  ported on windows platform to enhance the graphics

class flipper {

float R,r,D,phi0;
float phi,omega;
float deltaphi=PI/3;
float choc=1.2;
float Vpinball = 13;
vec2 v,vt;
vec2 o,m;
vec2 a1,b1,a2,b2;
wall w1,w2 ;
circle c1,c2;


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() {

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(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;
}

void control_right( char c) {
if ((mousePressed && mouseButton == LEFT) || (keyPressed && key==c)) {

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 ((mousePressed && mouseButton == LEFT) || (keyPressed && key==c)) {
sound_pinball.stop();
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;
}
}


boolean collision() {

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