示例#1
0
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            // Piirtää kaikki taivaankappaleet elliptiseen muotoon
            foreach (CelBody body in sim.Bodies)
            {
                if (!body.Exists)
                {
                    continue;
                }

                if (body is BlackHole)
                {
                    BlackHole bh  = (BlackHole)body;
                    Pen       pen = new Pen(bh.Color);
                    e.Graphics.DrawEllipse(pen, (int)bh.XPos - (int)bh.SchwarzschildR, (int)bh.YPos - (int)bh.SchwarzschildR, (int)bh.SchwarzschildR * 2, (int)bh.SchwarzschildR * 2);
                    pen.Dispose();
                    continue;
                }

                Brush brush = new SolidBrush(body.Color);
                e.Graphics.FillEllipse(brush, (int)body.XPos - body.D / 2, (int)body.YPos - body.D / 2, body.D, body.D);
                brush.Dispose();
            }
        }
示例#2
0
        public void Gravity()
        {
            foreach (CelBody body in Bodies)
            {
                if (!body.Exists)
                {
                    continue;
                }

                foreach (CelBody body2 in Bodies)
                {
                    if (body2 == body)
                    {
                        continue;
                    }

                    if (!body2.Exists)
                    {
                        continue;
                    }

                    // Kappaleiden etäisyys
                    double distance;
                    // F = voima
                    double F;
                    // X-akseliin kohdistuva voima
                    double Fx = 0;
                    // Y-akseliin kohdistuva voima
                    double Fy = 0;

                    // Kappaleiden sijaintien ero x- ja y-akselilla
                    double xDiff = 0;
                    double yDiff = 0;

                    double velocity;

                    // Lasketaan kappaleiden etäisyys
                    distance = Math.Abs(Math.Pow((body.XPos - body2.XPos), 2)) + Math.Abs(Math.Pow((body.YPos - body2.YPos), 2));
                    distance = Math.Sqrt(distance);
                    distance = distance * Modifiers.metersForPixel;

                    if (body2 is BlackHole)
                    {
                        BlackHole bh = (BlackHole)body2;

                        if (distance <= bh.SchwarzschildR * Modifiers.metersForPixel)
                        {
                            Collision(body, bh);
                            bh.NewSchwarzschildR();
                            break;
                        }
                    }

                    // Lasketaan kappaleiden välillä vaikuttava voima
                    F = Modifiers.G * ((body.Mass * body2.Mass) / Math.Pow(distance, 2));
                    F = F / Modifiers.timerCorrection * Modifiers.timeMultiplier;

                    // Lasketaan kappaleiden x- ja y-akselin erotus
                    xDiff = Math.Abs(body.XPos - body2.XPos);
                    yDiff = Math.Abs(body.YPos - body2.YPos);

                    //Fx = sqrt(F ^ 2 / ([y / x] ^ 2 + 1))
                    // Lasketaan x- ja y-akseleihin vaikuttava voima
                    if (xDiff != 0 && yDiff != 0)
                    {
                        Fx = Math.Sqrt(Math.Pow(F, 2) / (Math.Pow((yDiff / xDiff), 2) + 1));
                        Fy = (yDiff / xDiff) * Fx;
                        //Fy = Math.Sqrt(Math.Pow(F, 2) / (Math.Pow((xDiff / yDiff), 2) + 1));
                    }
                    else if (xDiff == 0)
                    {
                        Fy = F;
                        Fx = 0;
                    }
                    else if (yDiff == 0)
                    {
                        Fy = 0;
                        Fx = F;
                    }

                    if (body.XPos > body2.XPos)
                    {
                        Fx = -Fx;
                    }

                    if (body.YPos > body2.YPos)
                    {
                        Fy = -Fy;
                    }

                    // Lasketaan voiman vaikutus kappaleen liikenopeuteen
                    body.XVel += Fx / body.Mass;
                    body.YVel += Fy / body.Mass;

                    velocity = Math.Sqrt(Math.Abs(Math.Pow(body.XVel, 2)) + Math.Abs(Math.Pow(body.YVel, 2)));

                    if (velocity >= 2.998E+8)
                    {
                        // Ei anneta kappaleen saavuttaa tai ylittää valon nopeutta
                        if (body.XVel != 0 && body.YVel != 0)
                        {
                            body.XVel = Math.Sqrt(Math.Pow(2.998E+8 - 1, 2) / (Math.Pow((yDiff / xDiff), 2) + 1));
                            body.YVel = (yDiff / xDiff) * body.XVel;
                        }


                        //body.XVel -= Fx / body.Mass;
                        //body.YVel -= Fy / body.Mass;
                    }
                }

                // Päivitetään kappaleen sijainti
                body.XPos += body.XVel / Modifiers.metersForPixel / Modifiers.timerCorrection * Modifiers.timeMultiplier;
                body.YPos += body.YVel / Modifiers.metersForPixel / Modifiers.timerCorrection * Modifiers.timeMultiplier;
            }
        }