public int pwcollide( Particle p ) { if ((p.pos.x + p.r + 20f) > this.size.x) { // p.vel.x = -p.vel.x * 0.2f; p.pos.x = this.size.x - p.r - 20.1f; } if ((p.pos.y + p.r + 100f) > this.size.y) { // p.vel.y = -p.vel.y * 0.2f; p.pos.y = this.size.y - p.r - 100.1f; } if ((p.pos.x - p.r) < 0.0f) { // p.vel.x = -p.vel.x * 0.2f; p.pos.x = p.r; } if ((p.pos.y - p.r) < 0.0f) { // p.vel.y = -p.vel.y * 0.2f; p.pos.y = p.r; } return 0; }
public static int PtoPCollide( Particle p1, Particle p2 ) { // if ( p1 == null || p2 == null ) // return 0; Vector2 dx = p2.pos.Clone(); dx.Vsub(p1.pos); float r = p1.r + p2.r - dx.Len(); if ( r > 0.0f ) { Vector2 cn = dx.Norm(); float pr = r / ( p1.mass + p2.mass ) * 0.4f; p1.pos.x -= cn.x * ( p2.mass * pr ); p1.pos.y -= cn.y * ( p2.mass * pr ); p2.pos.x += cn.x * ( p1.mass * pr ); p2.pos.y += cn.y * ( p1.mass * pr ); return 1; } return 0; }
public Particle Badd(Particle b) { lock (bodyes) { this.bodyes.Add(b); this.cg.locate(b); } return b; }
public void pUpd(Particle p) { // lock (p) // { //p.force.y += 0.05f * p.mass; p.pos.y += 0.01f; p.Upd(this.dt*0.0000001f); pwcollide(p); // } }
public static int PtoPCollide( Particle p1, Particle p2 ) { if ( p1 == null || p2 == null ) return 0; //lock(p1){ // lock(p2){ Vector2 dx = new Vector2( p2.pos.x, p2.pos.y ); //dx.Vsub(p1.pos); dx.x -= p1.pos.x; dx.y -= p1.pos.y; float r1 = p1.r; float r2 = p2.r; float r = r1 + r2 - dx.Len(); if (r > 0.0f) { Vector2 cn = dx.Norm(); // Vector2 dv = p1.vel.Clone(); // dv.Vsub(p2.vel); // float sv = dv.Dot( cn ); //float tim = 1 / p1.mass + 1 / p2.mass; //float pr = r / tim*0.2f; float pr = r / ( p1.mass + p2.mass ) * 0.4f; //p1.pos.Vsub(cn.Clone().Smul(1 / p1.mass * pr)); p1.pos.x -= cn.x * ( p2.mass * pr ); p1.pos.y -= cn.y * ( p2.mass * pr); //p2.pos.Vadd(cn.Clone().Smul(1 / p2.mass * pr)); p2.pos.x += cn.x * ( p1.mass * pr ); p2.pos.y += cn.y * ( p1.mass * pr ); /* if( sv <= 0 ) return 1; float nsv = -sv * 0.5f; float dsv = nsv - sv; float p = dsv / tim; Vector2 pn = cn.Smul(p); p1.vel.Vadd( pn.Clone().Smul( 1/p1.mass) ); p2.vel.Vsub( pn.Smul( 1/p2.mass) );*/ return 1; } // } // } return 0; }
public int pcontain(Particle p) { if ( (p.pos.x - p.r < this.p1.x && p.pos.x + p.r > this.p0.x) && (p.pos.y - p.r < this.p1.y && p.pos.y + p.r > this.p0.y) ) { return 1; } return 0; }
public int locate( Particle p ) { if ( p == null ) return 0; if ( this.pcontain ( p ) == 1 ) { // lock(this.CL){ int r = 0; if ( this.CL[0] != null ) { /*Parallel.ForEach ( this.CL, cg => { if ( cg.pcontain ( p ) == 1 ) { r += cg.locate ( p ); } } );*/ foreach ( CollGrid cg in this.CL ) { if ( cg.pcontain ( p ) == 1 ) { r += cg.locate ( p ); //return; } } if ( r > 0 ) { tnump += 1; return r; } } // } // lock ( PL ) { this.PL.Add ( p ); //} tnump += 1; return 1; } return 0; }
public int Rand(int n) { Random r = new Random(); float m; Particle kp = new Particle(30f, 500f, 150, 150); this.Badd(kp); for (int i = 0; i < n; i++) { m = r.Next(15); Particle p = new Particle(3.0f+m, 1.0f+m, r.Next((int)this.size.x-50), r.Next((int)this.size.y-50)); this.Badd(p); } return 0; }
public int locate( Particle p ) { if ( this.pcontain ( p ) == 1 ) { int r = 0; if ( this.CL[0] != null ) { foreach ( CollGrid cg in this.CL ) { if ( cg.pcontain ( p ) == 1 ) { r += cg.locate ( p ); } } if ( r > 0 ) { tnump += 1; return r; } } this.PL.Add ( p ); tnump += 1; return 1; } return 0; }