void EffectBullet::bounceBullets(Bullet * bulA, Bullet * bulB) { float m21,dvx2,a,x21,y21,vx21,vy21,fy21; //m21=bulB->size/bulA->size; m21=bulB->size/bulA->size*bulB->size/bulA->size; x21=bulB->x - bulA->x; y21=bulB->y - bulA->y; vx21=bulB->dx * bulB->speed - bulA->dx * bulA->speed; vy21=bulB->dy * bulB->speed - bulA->dy * bulA->speed; if (x21*vx21 > 0 && y21*vy21 > 0) // they are moving away from eachother return; // *** I have inserted the following statements to avoid a zero divide; // (for single precision calculations, // 1.0E-12 should be replaced by a larger value). ************** fy21= .0000001f*fabs(y21); if ( fabs(x21)<fy21 ) x21=fy21 * (x21<0?-1:1); // *** update velocities *** a=y21/x21; dvx2= -2*(vx21 +a*vy21)/((1+a*a)*(1+m21)) ; bulB->dx = bulB->dx*bulB->speed + dvx2; bulB->dy = bulB->dy*bulB->speed + a*dvx2; bulB->speed = sqrt(bulB->dx*bulB->dx + bulB->dy*bulB->dy); bulB->dx /= bulB->speed; bulB->dy /= bulB->speed; bulB->speed = bulB->speed < 0.15f ? 0.15f : bulB->speed > 0.6f ? 0.6f : bulB->speed; bulA->dx = bulA->dx*bulA->speed - m21*dvx2; bulA->dy = bulA->dy*bulA->speed - a*m21*dvx2; bulA->speed = sqrt(bulA->dx*bulA->dx + bulA->dy*bulA->dy); bulA->dx /= bulA->speed; bulA->dy /= bulA->speed; bulA->speed = bulA->speed < 0.15f ? 0.15f : bulA->speed > 0.6f ? 0.6f : bulA->speed; bulA->deadTime = m_pSettings->frame + timeToHit(bulA); bulB->deadTime = m_pSettings->frame + timeToHit(bulB); return; }
void EffectBullet::incrementBullets() { int i; for (i = 0 ; i < NUM_BULLETS; i++) if (bullets[i].alive) { bullets[i].x += bullets[i].dx * bullets[i].speed; bullets[i].y += bullets[i].dy * bullets[i].speed; } for (i = 0; i < NUM_BULLETS; i++) { if (!bullets[i].alive) { if (frand() < bulletDensity) { bullets[i].speed = 0.2f + 0.3f*frand(); bullets[i].size = minsize + (maxsize - minsize)*frand(); float angle = frand()*2*3.141592f; bullets[i].alive = true; bullets[i].dx = sin(angle); bullets[i].dy = cos(angle); bullets[i].x = minx + scalex*frand(); bullets[i].y = miny + scaley*frand(); int time = timeToHit(&bullets[i]); bullets[i].x += bullets[i].dx * time; bullets[i].y += bullets[i].dy * time; bullets[i].dx *= -1; bullets[i].dy *= -1; bullets[i].deadTime = m_pSettings->frame + timeToHit(&bullets[i]); } } else { for (int j = 0; j < i; j++) if (bullets[j].alive && bulletsTooClose(&bullets[i],&bullets[j])) bounceBullets(&bullets[i],&bullets[j]); if (bullets[i].deadTime <= m_pSettings->frame) resetBullet(&bullets[i]); } } }
void predict(Ball** balls,Ball* currball){ if(currball== NULL) return; int i; for(i = 0;i<nballs;i++){ double dt = timeToHit(currball,balls[i]); if( time+dt<=tlimit && time+dt>0){ insertEvent(events,newEvent(time+dt,currball,balls[i])); } } // particle-wall collisions double dtX = timeToHitVerticalWall(currball); //printball(currball); double dtY = timeToHitHorizontalWall(currball); if (time + dtX <= tlimit && time + dtX >0) insertEvent(events,newEvent(time + dtX, currball, NULL)); if (time + dtY <= tlimit&& time + dtY >0) insertEvent(events,newEvent(time + dtY, NULL, currball)); }