Beispiel #1
0
        public Thing(FieldState field, Location2d location, Velocity2d velocity, double size)
        {
            this.id       = idNext++;
            this.myField  = field;
            this.size     = size;
            this.location = location;
            this.velocity = velocity;

            // random color
            color = Color.FromArgb(r.Next(256), r.Next(256), r.Next(256));
        }
        public void Test_Thing_ColorForSerialization_Get()
        {
            Field      f    = new Field();
            Location2d loc  = new Location2d();
            Velocity2d vel  = new Velocity2d();
            double     size = 14.0;

            Thing thing = new Thing(f.CurrentState, loc, vel, size);

            Assert.AreEqual(thing.color.R, thing.ColorForSerialization.red);
            Assert.AreEqual(thing.color.G, thing.ColorForSerialization.green);
            Assert.AreEqual(thing.color.B, thing.ColorForSerialization.blue);
        }
        public void Test_Thing_OptimalDeltaT2()
        {
            Field      f    = new Field();
            Location2d loc  = new Location2d();
            Velocity2d vel  = new Velocity2d(0, 28);
            double     size = 14.0;

            Thing thing = new Thing(f.CurrentState, loc, vel, size);

            decimal expectedDeltaT = 0.5m;

            Assert.AreEqual(expectedDeltaT, thing.CalculateOptimalDeltaT());
        }
        public void Test_Thing_Mass()
        {
            Field      f    = new Field();
            Location2d loc  = new Location2d();
            Velocity2d vel  = new Velocity2d();
            double     size = 14.0;

            Thing thing = new Thing(f.CurrentState, loc, vel, size);

            double expectedMass = size * size * Math.PI;

            Assert.AreEqual(expectedMass, thing.Mass);
        }
Beispiel #5
0
        public Thing2(Field2 field, Location2d location, Velocity2d velocity, double size)
        {
            this.id       = idNext++;
            this.myField  = field;
            this.size     = size;
            this.location = location;
            this.velocity = velocity;

            this.rezinochki = new List <Rezinochka>();

            // random color
            color = Color.FromArgb(r.Next(256), r.Next(256), r.Next(256));
        }
        public void Test_Thing_CtorMain()
        {
            Field      f    = new Field();
            Location2d loc  = new Location2d();
            Velocity2d vel  = new Velocity2d();
            double     size = 14.0;

            Thing thing = new Thing(f.CurrentState, loc, vel, size);

            Assert.IsTrue(thing.id > -1);
            Assert.AreSame(thing.location, loc);
            Assert.AreSame(thing.velocity, vel);
            Assert.AreEqual(thing.size, size);
        }
Beispiel #7
0
        public static Thing Combine(List <Thing> list)
        {
            if (list.Count < 2)
            {
                throw new PhysicsException("Combine called with <2 items. This does not make sense.");
            }

            AveragerUtil massA = new AveragerUtil();

            AveragerUtil colorR = new AveragerUtil();
            AveragerUtil colorG = new AveragerUtil();
            AveragerUtil colorB = new AveragerUtil();

            AveragerUtil locXM = new AveragerUtil();
            AveragerUtil locYM = new AveragerUtil();

            AveragerUtil velIM = new AveragerUtil();
            AveragerUtil velJM = new AveragerUtil();


            foreach (Thing t in list)
            {
                massA.AddMeasurement(t.Mass);

                colorR.AddMeasurement(t.color.R);
                colorG.AddMeasurement(t.color.G);
                colorB.AddMeasurement(t.color.B);

                locXM.AddMeasurement(t.location.x * t.Mass);
                locYM.AddMeasurement(t.location.y * t.Mass);

                velIM.AddMeasurement(t.velocity.i * t.Mass);
                velJM.AddMeasurement(t.velocity.j * t.Mass);
            }

            // mass
            double size = Math.Sqrt(massA.Sum / Math.PI);

            // color
            Color color = Color.FromArgb((int)colorR.Average, (int)colorG.Average, (int)colorB.Average);

            // location
            Location2d loc = new Location2d(locXM.Sum / massA.Sum, locYM.Sum / massA.Sum);

            // velocity
            Velocity2d vel = new Velocity2d(velIM.Sum / massA.Sum, velJM.Sum / massA.Sum);

            return(new Thing(list[0].myField, loc, vel, size));
        }
        public void Test_Thing_ColorForSerialization_Set()
        {
            Field      f    = new Field();
            Location2d loc  = new Location2d();
            Velocity2d vel  = new Velocity2d();
            double     size = 14.0;

            Thing thing = new Thing(f.CurrentState, loc, vel, size);

            ColorSrz colorSrz = new ColorSrz();

            colorSrz.red   = 68;
            colorSrz.green = 71;
            colorSrz.blue  = 14;

            thing.ColorForSerialization = colorSrz;

            Assert.AreEqual(thing.color.R, thing.color.R);
            Assert.AreEqual(thing.color.G, thing.color.G);
            Assert.AreEqual(thing.color.B, thing.color.B);
        }
Beispiel #9
0
        //goto new time
        public void GotoNewTime(decimal deltaT)
        {
            // calculate net gravity - used in calculations
            CalculateNetGravity();

            // adjust velocity
            Vector2d accel = gravityNet * (1 / Mass);

            velocity += accel * deltaT;

            // displace by
            location.DisplaceBy(velocity);

            //// check if location is outside of bounds
            //CheckBounds();

            // check if we are overlapping with any other Things
            CheckOverlap();

            // calculate net gravity - for display
            CalculateNetGravity();
        }        //GotoNewTime
        public void Test_Thing_MakeCopy()
        {
            Field      f    = new Field();
            Location2d loc  = new Location2d();
            Velocity2d vel  = new Velocity2d();
            double     size = 14.0;

            Thing thing = new Thing(f.CurrentState, loc, vel, size);

            Thing copy = thing.MakeCopy();

            Assert.AreEqual(thing.id, copy.id);
            //TODO: also need to test Id which includes FieldState's time

            Assert.AreEqual(thing.color, copy.color);
            Assert.AreEqual(thing.size, copy.size);
            Assert.AreEqual(thing.Mass, copy.Mass);                 // calculated
            Assert.AreEqual(thing.location, copy.location);
            Assert.AreEqual(thing.velocity, copy.velocity);

            Assert.IsNull(copy.gravityNet);
            Assert.IsNull(copy.gravityComponents);
        }
        public void InitItems(int nrThings)
        {
            Random r = new Random();
            int    nrThingsDiscarded = 0;

            // the sun
            Location2d sunLoc = new Location2d(GlobalConfig.viewport.Left + GlobalConfig.viewport.Width / 2, GlobalConfig.viewport.Top + GlobalConfig.viewport.Height / 2);
            Velocity2d sunVel = new Velocity2d(0, 0);
            Thing      sun    = new Thing(this, sunLoc, sunVel, 14);

            things.Add(sun);


            // make things
            for (int i = 0; i < nrThings - 1;)
            {
                // if nrDiscarded is too high, we need to exit
                if (nrThingsDiscarded / nrThings > 71)
                {
                    throw new PhysicsException("Please lower Nr Things, they are not fitting");
                }

                double rr        = r.NextDouble();
                double thingSize = rr * (GlobalConfig.sizeMax - GlobalConfig.sizeMin) + GlobalConfig.sizeMin;

                // location
                double left   = myField.Viewport.Left + thingSize;
                double right  = myField.Viewport.Right - thingSize;
                double top    = myField.Viewport.Top + thingSize;
                double bottom = myField.Viewport.Bottom - thingSize;

                double     x        = r.NextDouble() * (right - left) + left;
                double     y        = r.NextDouble() * (bottom - top) + top;
                Location2d location = new Location2d(x, y);

                // check for overlap with previous things
                bool foundOverlap = false;
                foreach (Thing t in things)
                {
                    double dist = t.CalculateSpaceBetween(location, thingSize);
                    if (dist < 0)
                    {
                        foundOverlap = true;
                        break;
                    }
                }

                if (foundOverlap)
                {
                    nrThingsDiscarded++;
                    continue;
                }

                // velocity: get vector in direction of the sun, and then rotate it left or right
                Vector2d vel = new Vector2d(location, sun.location);
                vel.Normalize();
                vel.Rotate(r.Next() == 0 ? -Math.PI / 2 : Math.PI / 2);
                double coeff = r.NextDouble() * GlobalConfig.velocityMax;
                vel = vel * coeff;
                Velocity2d velocity = new Velocity2d(vel);

                // make the thing
                Thing thing = new Thing(this, location, velocity, thingSize);

                things.Add(thing);

                i++;
            }

            Util.Logger.AddMessage("Nr things discarded: " + nrThingsDiscarded);
        }        //InitItems()
Beispiel #12
0
        public void InitItems(int nrThings)
        {
            Random r = new Random();
            int    nrThingsDiscarded = 0;


            // make things
            for (int i = 0; i < nrThings;)
            {
                double rr        = r.NextDouble();
                double thingSize = rr * (GlobalConfig.sizeMax - GlobalConfig.sizeMin) + GlobalConfig.sizeMin;

                // location
                double left   = this.Viewport.Left + thingSize;
                double right  = this.Viewport.Right - thingSize;
                double top    = this.Viewport.Top + thingSize;
                double bottom = this.Viewport.Bottom - thingSize;

                double     x        = r.NextDouble() * (right - left) + left;
                double     y        = r.NextDouble() * (bottom - top) + top;
                Location2d location = new Location2d(x, y);

                // check for overlap with previous things
                bool foundOverlap = false;
                foreach (Thing2 t in things)
                {
                    double dist = t.CalculateSpaceBetween(location, thingSize);
                    if (dist < 0)
                    {
                        foundOverlap = true;
                        break;
                    }
                }

                if (foundOverlap)
                {
                    nrThingsDiscarded++;
                    continue;
                }

                Velocity2d velocity = new Velocity2d(0.0, 0.0);

                // make the thing
                Thing2 thing = new Thing2(this, location, velocity, thingSize);

                things.Add(thing);

                i++;
            }

            Util.Logger.AddMessage("Nr things discarded: " + nrThingsDiscarded);


            // add rezinochki
            for (int idThing = 0; idThing < things.Count - 1; idThing += 2)
            {
                Thing2 ta = this.things[idThing];
                Thing2 tb = this.things[idThing + 1];

                Rezinochka rez = new Rezinochka(Color.LightYellow, 1.0);

                rez.Attach(ta, Rezinochka.Orientation.A);
                rez.Attach(tb, Rezinochka.Orientation.B);

                this.rezinochki.Add(rez);
            }
        }        //InitItems()