コード例 #1
0
ファイル: PhysicsView.cs プロジェクト: softwaremeisterei/fx
    private void StartEngine(MassObject obj)
    {
        if (obj != null)
        {
            lock (_world.Objects)
            {
                if (obj.Engines.Count > 0)
                {
                    var engine   = obj.Engines[0];
                    var strength = engine.Force.Magnitude + engine.MaxStrength;
                    engine.Force = obj.Shape.Orientation.UnitVector * strength;

                    var particleCenter = obj.Position - obj.Shape.Orientation.UnitVector * 3 / 2;

                    for (int i = 0; i < 6; i++)
                    {
                        var particle = new MassObject("Fuel",
                                                      particleCenter + new Vector(0, 0.1 * (_randomizer.Next() % 10)).Rotate(((double)_randomizer.Next() % 180) / NaturalConstants.PI),
                                                      obj.Speed * 0.7,
                                                      obj.Direction,
                                                      0.01);
                        particle.Shape = new CircleShape(_fuelPens[_randomizer.Next(_fuelPens.Length)], 0.1);

                        particle.LiveUntil = DateTime.Now + TimeSpan.FromMilliseconds(maxSecondsEngineFireParticlesLivetime * 1000 * 2 / 5
                                                                                      + _randomizer.Next() % maxSecondsEngineFireParticlesLivetime * 1000 * 3 / 5);
                        lock (_world.Objects)
                        {
                            _world.Objects.Add(particle);
                        }
                    }
                }
            }
        }
    }
コード例 #2
0
ファイル: PhysicsView.cs プロジェクト: softwaremeisterei/fx
    private void PhysicsView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            _isFrozen = true;

            try
            {
                var form = new MassObjectDetails
                {
                    MassObject = new MassObject {
                        Position = new Vector(_viewPoint.X + e.X / _spatialScale, _viewPoint.Y + e.Y / _spatialScale)
                    }
                };

                if (form.ShowDialog() == DialogResult.OK)
                {
                    lock (_world.Objects)
                    {
                        _world.Objects.Add(form.MassObject);
                    }
                }
            }
            finally
            {
                _isFrozen = false;
            }
        }
        else
        {
            _focusedObject = null;
            _viewPoint     = _viewPoint + new Vector(e.X, e.Y) / _spatialScale - new Vector(Size.Width / 2, Size.Height / 2) / _spatialScale;
        }
    }
コード例 #3
0
ファイル: World.cs プロジェクト: softwaremeisterei/fx
    /// <summary>
    /// Adds the solar system's sun and planets to this world
    /// </summary>
    public void InsertSolarSystem()
    {
        // Sun

        MassObject m = new MassObject();

        m.Name     = "Sun";
        m.Position = new Vector(0, 0);
        m.Mass     = NaturalConstants.SolarSystem.Mass_Sun;
        m.Shape    = new CircleShape(NaturalConstants.SolarSystem.Diameter_Sun);
        Objects.Add(m);

        // Earth

        m           = new MassObject();
        m.Name      = "Earth";
        m.Position  = new Vector(NaturalConstants.AU, 0);
        m.Direction = new Vector(0, 1);
        m.Speed     = NaturalConstants.SolarSystem.Velocity_Orbital_Mean_Earth;
        m.Mass      = NaturalConstants.SolarSystem.Mass_Earth;
        m.Shape     = new CircleShape(NaturalConstants.SolarSystem.Diameter_Earth);
        Objects.Add(m);

        //...
    }
コード例 #4
0
 public void SetParent(MassObject ObjectParent, StickingObject stickingObjectParent)
 {
     sinRotation.Initialize();
     this.stickingObjectParent = stickingObjectParent;
     this.ObjectParent         = ObjectParent;
     size           = transform.localScale;
     rb.isKinematic = true;
 }
コード例 #5
0
    public void RemovePlanetFromGame(MassObject _planet)
    {
        if (_planet == MassObject.CharacterInstance)
        {
            Debug.LogError("Something tried to delete the player!");
            return;
        }

        Destroy(_planet.gameObject);
        PlanetsInPlay.Remove(_planet);
    }
コード例 #6
0
    public void DetatchFromParent()
    {
        transform.SetParent(null, true);
        ObjectParent         = null;
        stickingObjectParent = null;

        DetatchChilds();

        //testing
        rb.velocity        = (Random.insideUnitSphere * 2);
        rb.angularVelocity = (Random.insideUnitSphere * 5);
    }
コード例 #7
0
ファイル: Physics.cs プロジェクト: softwaremeisterei/fx
    /// <summary>
    /// Moves the given object by calculating the distance covered
    /// in the time period given in milliseconds
    /// </summary>
    private void Translate(MassObject o, double dtms)
    {
        var force = o.GravForce;

        foreach (var engine in o.Engines)
        {
            force += engine.Force;
        }

        o.Velocity += force / o.Mass * dtms / 1000;
        o.Position += o.Velocity * dtms / 1000;
    }
コード例 #8
0
    void Awake()
    {
        if (CharacterInstance == null && GetComponent <CharacterMover>() != null)
        {
            CharacterInstance = this;
        }

        MyRigidBody = GetComponent <Rigidbody2D>();
        MyRenderer  = GetComponent <Renderer>();
        MyMouth     = GetComponent <Mouth>();
        audio       = GetComponent <AudioSource>();
    }
コード例 #9
0
    public void FinishedEating(MassObject _mo)
    {
        ThingsBeingEaten.Remove(_mo);
        MyMassObject.SetMass(MyMassObject.Mass + _mo.Mass);

        if (ThingsBeingEaten.Count == 0)
        {
            MouthRenderer.transform.localScale = new Vector3(MouthMinSize, MouthMinSize, 1);
            MyBodyController.OpenMouth(false);
            MyMassObject.CurrentState = MassObject.StateEnum.Default;
        }
    }
コード例 #10
0
    IEnumerator _SpawnLoop()
    {
        float _spawnChance = 0;

        while (true)
        {
            if (PlanetsInPlay.Count >= MaxAmount)
            {
                yield return(null);

                continue;
            }

            if (PlanetsInPlay.Count == 0)
            {
                _spawnChance = 1;
            }
            else
            {
                _spawnChance = Mathf.Max(SpawnChanceMin, SpawnChanceMod / PlanetsInPlay.Count);
            }

            if (Random.value < _spawnChance)
            {
                Vector3 _spawnPos     = new Vector3(Mathf.Round(Random.value), Random.value, 0);
                float   _sideOfCamera = Mathf.Sign(_spawnPos.x - 1);

                GameObject _obj = Instantiate(PlanetPrefabs[Random.Range(1, PlanetPrefabs.Count)].gameObject, Camera.main.ViewportToWorldPoint(_spawnPos), Quaternion.identity) as GameObject;
                _obj.transform.position = new Vector3(_obj.transform.position.x, _obj.transform.position.y, 0);

                MassObject _mo = _obj.GetComponent <MassObject>();
                PlanetsInPlay.Add(_mo);
                _mo.MyRigidBody.velocity += new Vector2(-_sideOfCamera, 0);

                float _mass = 1;
                if (Random.value > 0.5f) // 50% of smaller
                {
                    _mass = Random.Range(PLANET_SIZE_MIN, MassObject.CharacterInstance.Mass);
                }
                else
                {
                    _mass = Random.Range(MassObject.CharacterInstance.Mass, Mathf.Min(PLANET_SIZE_MAX, MassObject.CharacterInstance.Mass * 1.25f));
                }
                _mo.SetMass(_mass);
                _obj.transform.position += new Vector3((_mo.Mass * 0.5f) * _sideOfCamera, 0, 0);
            }

            yield return(new WaitForSeconds(1));
        }
    }
コード例 #11
0
ファイル: Physics.cs プロジェクト: softwaremeisterei/fx
    /// <summary>
    /// Calculates the forces exerted by the given objects on each other
    /// </summary>
    private void CalcForces(MassObject o1, MassObject o2, double dtms)
    {
        double distance = (o2.Position - o1.Position).Magnitude;

        if (distance < 1E-5)
        {
            return;
        }

        var force = (o2.Position - o1.Position)
                    * (NaturalConstants.G * o1.Mass * o2.Mass)
                    / (distance * distance * distance);

        o1.GravForce += force;
        o2.GravForce -= force;
    }
コード例 #12
0
ファイル: PhysicsView.cs プロジェクト: softwaremeisterei/fx
 private void StopEngine(MassObject obj)
 {
     if (obj != null)
     {
         lock (_world.Objects)
         {
             if (obj.Engines.Count > 0)
             {
                 foreach (var engine in obj.Engines)
                 {
                     engine.Force = Vector.NullVector;
                 }
             }
         }
     }
 }
コード例 #13
0
ファイル: PhysicsView.cs プロジェクト: softwaremeisterei/fx
 private void FireWeapon(MassObject origin)
 {
     if (origin != null)
     {
         lock (_world.Objects)
         {
             var bullet = new MassObject("Weapon",
                                         origin.Position + origin.Shape.Orientation.UnitVector * 2,
                                         origin.Speed + 50,
                                         origin.Shape.Orientation,
                                         WeaponMass);
             bullet.Shape     = new CircleShape(Pens.Chocolate, 0.001);
             bullet.LiveUntil = DateTime.Now + TimeSpan.FromSeconds(4);
             _world.Objects.Add(bullet);
         }
     }
 }
コード例 #14
0
ファイル: PhysicsView.cs プロジェクト: softwaremeisterei/fx
    private void PhysicsView_Load(object sender, System.EventArgs e)
    {
        _spatialScale       = 6;
        _scala.SpatialScale = _spatialScale;

        _engineThread.Run();
#if false
        _world.InsertSolarSystem();
        _focusedObject = _world.FindPhysicObject("Sun");
        _spatialScale  = Size.Width / (CosmicConstants.AU * 2) * 2 / 3;
#else
        var nina = new MassObject("Nina", Vector.NullVector, 0, new Vector(1, 0), 1E+13);
        nina.Shape = new CircleShape(2);
        _world.Objects.Add(nina);

        var ninasMoon = new MassObject("Ninas Moon", new Vector(-10, 0), 6, new Vector(0, -1), 1E+10);
        ninasMoon.Shape = new CircleShape(1);
        _world.Objects.Add(ninasMoon);

        var starship = new MassObject("Starship Ed", Vector.NullVector + Vector.UnitVectorX * -20, 0, new Vector(1, 0), 1E+4);
        starship.Shape             = new StarshipShape();
        starship.Shape.Orientation = new Vector(1, -0.618);
        starship.Direction         = starship.Shape.Orientation;
        starship.Speed             = 4;
        _world.Objects.Add(starship);

        _focusedObject = starship;

        var engine = new Engine(Vector.NullVector, 0 /*-0.9*/);
        engine.MaxStrength = 10E+4;
        starship.Engines.Add(engine);
        _engineThread.Add(engine);
#endif

        physicsTimer.Interval = (int)_physicsTimerInterval;
        physicsTimer.Enabled  = true;
        physicsTimer.Elapsed += new System.Timers.ElapsedEventHandler(PhysicsTimerTick);
        physicsTimer.Start();

        collisionsTimer.Interval = (int)_collisionsTimerInterval;
        collisionsTimer.Enabled  = true;
        collisionsTimer.Elapsed += new System.Timers.ElapsedEventHandler(CollisionsTimerTick);
        collisionsTimer.Start();

        SetStyle(ControlStyles.ResizeRedraw | ControlStyles.Opaque, true);
    }
コード例 #15
0
ファイル: PhysicsView.cs プロジェクト: softwaremeisterei/fx
    private void Explode(MassObject massObject)
    {
        massObject.LiveUntil = DateTime.Now;

        for (var i = 0; i < 20 + _randomizer.Next(20); i++)
        {
            var direction = massObject.Shape.Orientation.Rotate(_randomizer.NextDouble() % (2 * NaturalConstants.PI)).UnitVector;

            var debris = new MassObject("Debris",
                                        massObject.Position + direction * (2 + _randomizer.Next(4)),
                                        5 + massObject.Speed * (1 + _randomizer.NextDouble() * 2),
                                        direction,
                                        DebrisMass);
            debris.Shape     = new CircleShape(_debrisPens[_randomizer.Next(_debrisPens.Length)], 0.003);
            debris.LiveUntil = DateTime.Now + TimeSpan.FromSeconds(1 + _randomizer.Next(5));
            _world.Objects.Add(debris);
        }
    }
コード例 #16
0
 public void SetFirstStickingchild(MassObject parent)
 {
     SetParent(parent, null);
 }
コード例 #17
0
 void Awake()
 {
     MyMassObject     = GetComponent <MassObject>();
     MyBodyController = GetComponent <BodyController>();
 }
コード例 #18
0
ファイル: PhysicsView.cs プロジェクト: softwaremeisterei/fx
 private string Describe(MassObject m)
 {
     return(string.Format("{0}\n{1:F} kg\n{2:F} m/s\nX: {3:F}nY: {4:F}", m.Name, m.Mass, m.Speed, m.Position.X, m.Position.Y));
 }
コード例 #19
0
ファイル: PhysicsView.cs プロジェクト: softwaremeisterei/fx
    private void HandleKeyboard()
    {
        if (Control.ModifierKeys == Keys.Shift)
        {
            if (Keyboard.IsKeyDown(Key.J))
            {
                _viewPoint -= new Vector(10, 0) / _spatialScale;
            }
            if (Keyboard.IsKeyDown(Key.L))
            {
                _viewPoint += new Vector(10, 0) / _spatialScale;
            }
            if (Keyboard.IsKeyDown(Key.I))
            {
                _viewPoint -= new Vector(0, 10) / _spatialScale;
            }
            if (Keyboard.IsKeyDown(Key.K))
            {
                _viewPoint += new Vector(0, 10) / _spatialScale;
            }

            return;
        }

        if (Keyboard.IsKeyDown(Key.S))
        {
            _spatialScale = Math.Min(1000, _spatialScale * 2); _scala.SpatialScale = _spatialScale;
        }
        if (Keyboard.IsKeyDown(Key.A))
        {
            _spatialScale = Math.Max(0.001, _spatialScale / 2); _scala.SpatialScale = _spatialScale;
        }

        if (Keyboard.IsKeyDown(Key.F))
        {
            _isFrozen = !_isFrozen;
        }
        if (Keyboard.IsKeyDown(Key.R))
        {
            lock (_world.Objects) { _world.Objects.Clear(); }
        }

        if (Keyboard.IsKeyDown(Key.N))
        {
            if (Control.ModifierKeys == Keys.Shift)
            {
                _focusedObject = null;
            }
            else
            {
                if (_world.Objects.Count > 0)
                {
                    if (_focusedObject == null)
                    {
                        _focusedObject = _world.Objects[0];
                    }
                    else
                    {
                        lock (_world.Objects)
                        {
                            var i = _world.Objects.IndexOf(_focusedObject) + 1;
                            i = i % _world.Objects.Count;
                            _focusedObject = _world.Objects[i];
                        }
                    }
                }
            }
        }

        if (_focusedObject != null)
        {
            if (Keyboard.IsKeyDown(Key.L))
            {
                lock (_world.Objects)
                {
                    _focusedObject.Shape.Orientation = _focusedObject.Shape.Orientation.Rotate(NaturalConstants.PI / 20);
                }
            }

            if (Keyboard.IsKeyDown(Key.J))
            {
                lock (_world.Objects)
                {
                    _focusedObject.Shape.Orientation =
                        _focusedObject.Shape.Orientation.Rotate(-NaturalConstants.PI / 20);
                }
            }

            if (Keyboard.IsKeyDown(Key.B))
            {
                lock (_world.Objects)
                {
                    _focusedObject.Speed /= 2;
                }
            }
        }

        if (Keyboard.IsKeyDown(Key.K))
        {
            StartEngine(_focusedObject);
        }
        else
        {
            StopEngine(_focusedObject);
        }

        if (Keyboard.IsKeyDown(Key.Space))
        {
            FireWeapon(_focusedObject);
        }
        if (Keyboard.IsKeyDown(Key.O))
        {
            _osdLevel = (_osdLevel + 1) % 2;
        }

        if (Keyboard.IsKeyDown(Key.M))
        {
            CreateBot();
        }
    }
コード例 #20
0
ファイル: PhysicsView.cs プロジェクト: softwaremeisterei/fx
    private void PhysicsTimerTick(object sender, System.Timers.ElapsedEventArgs e)
    {
        if (!Monitor.TryEnter(Mux))
        {
            return;
        }

        lock (Mux)
        {
            HandleKeyboard();

            if (_isFrozen == false)
            {
                var physics = new Physics();

                lock (_world.Objects)
                {
                    MassObject[] physObjs = null;

                    if (_world.Objects.Count > CountObjectsCalcThreshold)
                    {
                        // Take only the biggest objects into the calculation
                        // 1. Sort the objects descending by mass
                        _world.Objects.Sort(new Comparison <MassObject>((a, b) => - a.Mass.CompareTo(b.Mass)));

                        physObjs = new MassObject[CountObjectsCalcThreshold];

                        var count = 0;
                        foreach (MassObject o in _world.Objects)
                        {
                            physObjs[count++] = o;
                            if (count == CountObjectsCalcThreshold)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        physObjs = _world.Objects.ToArray();
                    }

                    physics.Calculate(physObjs, _physicsTimerInterval * _timeScale);
                }
            }

            var bots = _world.Objects.Where(o => o is Bot).ToList();
            foreach (Bot bot in bots)
            {
                bot.Calc(_physicsTimerInterval * _timeScale);
            }

            if (bots.Count < 10 && (DateTime.Now - _timeLastBotAdded).TotalSeconds > 5)
            {
                CreateBot();
                _timeLastBotAdded = DateTime.Now;
            }

            _world.CollectGarbage();
            Invalidate(false);
        }
    }