//------------------------------------------------------------ public void ToMake(Graphics g) { this.stick.ToMake(g); this.eLine.ToMake(g); for (int i = 0; i < this._ballsOnTable.Count; i++) { Balls a = this._ballsOnTable[i]; a.ToMake(g); for (int j = i + 1; j < this._ballsOnTable.Count; j++) { Balls b = this._ballsOnTable[j]; this.Clash(a, b); } } }
//------------------------------------------------------------ private void Clash(Balls b1, Balls b2) // we copied pasted this part { float xDifferece = b1.X - b2.X; float yDifferece = b1.Y - b2.Y; float hypotenuse = Convert.ToSingle(Math.Pow(xDifferece, 2) + Math.Pow(yDifferece, 2)); if (Math.Sqrt(hypotenuse) <= (b1.Radius + b2.Radius + b2.Radius)) { if (b2 is Pockets || b1 is Pockets) { Debug.WriteLine("ball is in the hole."); b2.Vx = 0; b2.Vy = 0; b1.Vx = 0; b1.Vy = 0; if (b2 is Pockets) { if (b1 is WhiteBall) { b1.ResetPosition(); } else { _ballsOnTable.Remove(b1); } } else { if (b2 is WhiteBall) { b2.ResetPosition(); } else { _ballsOnTable.Remove(b2); } } } else { float Vx = b2.Vx - b1.Vx; float Vy = b2.Vy - b1.Vy; float dotProduct = xDifferece * Vx + yDifferece * Vy; if (dotProduct > 0) { float collisionScale = dotProduct / hypotenuse; float xCollision = (xDifferece * collisionScale); float yCollision = (yDifferece * collisionScale); float combinedMass = b1.Mass + b2.Mass; float collisionWeightA = 2 * b2.Mass / combinedMass; float collisionWeightB = 2 * b1.Mass / combinedMass; b1.Vx += collisionWeightA * xCollision; b1.Vy += collisionWeightA * yCollision; b2.Vx -= collisionWeightB * xCollision; b2.Vy -= collisionWeightB * yCollision; } } } }
//------------------------------------------------------------ public void AddBall(Balls b) { this._ballsOnTable.Add(b); }