private void World_Updating(object sender, WorldUpdatingArgs e)
        {
            if (_body == null)
            {
                // When the body is null, the fluid object is static (if there is one at all).  If it's not null, this needs to be called from the
                // body's apply force/torque event
                UpdateFluidForces();
            }

            // These are the visuals that fly along the fluid direction
            UpdateFlowLines(e.ElapsedTime);
        }
 private void World_Updating(object sender, WorldUpdatingArgs e)
 {
     try
     {
         _updateManager.Update_MainThread(e.ElapsedTime);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
        private void World_Updating(object sender, WorldUpdatingArgs e)
        {
            try
            {
                // Chased ball
                _chasedBall.Update(e.ElapsedTime);

                _chasedBallTransform.OffsetX = _chasedBall.Position.X;
                _chasedBallTransform.OffsetY = _chasedBall.Position.Y;
                _chasedBallTransform.OffsetZ = _chasedBall.Position.Z;

                _chasedDirectionModel.SetPoints(_chasedBall.Position, _chasedBall.Position + _chasedBall.Direction.ToUnit() * 10);

                // Chasing Objects
                if (_object_LinearVelocity != null)
                {
                    _object_LinearVelocity.SetPosition(_chasedBall.Position);
                }

                if (_object_LinearForce != null)
                {
                    _object_LinearForce.SetPosition(_chasedBall.Position);
                }

                if (_object_OrientationVelocity != null)
                {
                    _object_OrientationVelocity.SetOrientation(_chasedBall.Direction);
                }

                if (_object_OrientationForce != null)
                {
                    _object_OrientationForce.SetOrientation(_chasedBall.Direction);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Ejemplo n.º 4
0
        private void World_Updating(object sender, WorldUpdatingArgs e)
        {
            DateTime thisUpdate = DateTime.UtcNow;
            double elapsedTime = (thisUpdate - _lastUpdate).TotalSeconds;

            #region Refill containers

            if (_containers != null)
            {
                _containers.Energy.QuantityCurrent = _containers.Energy.QuantityMax;
                _containers.Fuel.QuantityCurrent = _containers.Fuel.QuantityMax;
            }

            #endregion

            #region Update sensors

            if (_gravSensors != null)
            {
                foreach (GravSensorStuff sensor in _gravSensors)
                {
                    sensor.Sensor.Update_MainThread(elapsedTime);
                    sensor.Sensor.Update_AnyThread(elapsedTime);
                }

                lblSensorMagnitude.Text = Math.Round(_gravSensors[0].Sensor.CurrentMax, 3).ToString();		// just report the first sensor's value
            }
            else
            {
                lblSensorMagnitude.Text = "";
            }

            #endregion
            #region Update brains

            if (_brains != null)
            {
                foreach (BrainStuff brain in _brains)
                {
                    brain.Brain.Update_MainThread(elapsedTime);
                    brain.Brain.Update_AnyThread(elapsedTime);
                }
            }

            #endregion

            #region Color neurons

            foreach (var neuron in UtilityCore.Iterate(
                _gravSensors == null ? null : _gravSensors.SelectMany(o => o.Neurons),
                _brains == null ? null : _brains.SelectMany(o => o.Neurons),
                _thrusters == null ? null : _thrusters.SelectMany(o => o.Neurons)))
            {
                if (neuron.Item1.IsPositiveOnly)
                {
                    neuron.Item2.Color = UtilityWPF.AlphaBlend(_colors.Neuron_One_ZerPos, _colors.Neuron_Zero_ZerPos, neuron.Item1.Value);
                }
                else
                {
                    double weight = neuron.Item1.Value;		// need to grab the value locally, because it could be modified from a different thread

                    if (weight < 0)
                    {
                        neuron.Item2.Color = UtilityWPF.AlphaBlend(_colors.Neuron_NegOne_NegPos, _colors.Neuron_Zero_NegPos, Math.Abs(weight));
                    }
                    else
                    {
                        neuron.Item2.Color = UtilityWPF.AlphaBlend(_colors.Neuron_One_NegPos, _colors.Neuron_Zero_NegPos, weight);
                    }
                }
            }

            #endregion

            _lastUpdate = thisUpdate;
        }
        private void World_Updating(object sender, WorldUpdatingArgs e)
        {
            DateTime now = DateTime.UtcNow;

            _updateManager.Update_MainThread(e.ElapsedTime);

            #region Explosions

            int index = 0;
            while (index < _explosions.Count)
            {
                ExplodingBean explosion = _explosions[index];

                // Tell this explosion to advance its shockwave
                if (explosion.Update(e.ElapsedTime))
                {
                    // It has expired, remove it
                    DisposeExplosion(explosion);
                }
                else
                {
                    index++;
                }
            }

            #endregion

            if (_beans.Count + _beansCreatingCount < _options.NumBeansAtATime)
            {
                // Add new bean
                AddBeanAsync();     // fire and forget
            }

            if (_selectedBean != null)
            {
                // Chase bean
                _camera.Position = _selectedBean.Bean.PositionWorld + _selectedBean.CameraOffset;
            }

            if ((now - _lastWinnerScan).TotalSeconds > _options.TrackingScanFrequencySeconds)
            {
                FindWinners();
                RefreshStats();

                _lastWinnerScan = now;
            }

            if (_heightText != null && now > _heightText.Item2)
            {
                pnlVisuals2D.Children.Remove(_heightText.Item1);
                _heightText = null;
            }

            #region Autosave

            double elapsedFromStart = (DateTime.UtcNow - _windowStartTime).TotalMinutes;

            if (_lastAutosave == null)
            {
                if (elapsedFromStart > 3)
                {
                    _panelFile.Save(true, true, AUTOSAVESUFFIX);
                    _lastAutosave = DateTime.UtcNow;
                }
            }
            else
            {
                double autosaveElapse;

                // Figure out the delay
                if (elapsedFromStart <= 15d)
                {
                    autosaveElapse = 3d;		// 3 minutes between saves for the first 15 minutes
                }
                else if (elapsedFromStart <= 30d)
                {
                    autosaveElapse = 5d;		// 5 between for next 15 minutes
                }
                else if (elapsedFromStart <= 60d)
                {
                    autosaveElapse = 10d;		// 10 between for next 30 minutes
                }
                else if (elapsedFromStart <= 120d)
                {
                    autosaveElapse = 20d;		// 20 between for next hour
                }
                else
                {
                    autosaveElapse = 30d;		// every half hour after that
                }

                if ((DateTime.UtcNow - _lastAutosave.Value).TotalMinutes > autosaveElapse)
                {
                    _panelFile.Save(true, true, AUTOSAVESUFFIX);
                    _lastAutosave = DateTime.UtcNow;
                }
            }

            #endregion
        }
        private void World_Updating(object sender, WorldUpdatingArgs e)
        {
            try
            {
                _updateManager.Update_MainThread(e.ElapsedTime);

                _strokes.Tick();

                UpdateClusterVisuals();

                #region report thread ratios

                if (chkShowBotThreadUsage.IsChecked.Value)
                {
                    long totalMain = 0;
                    long totalAny = 0;

                    foreach (SwarmBot1a bot in _map.GetItems(typeof(SwarmBot1a), false))
                    {
                        totalMain += bot.MainThreadCounter;
                        totalAny += bot.AnyThreadCounter;
                    }

                    double ratio = totalMain == 0 ? 999d : Convert.ToDouble(totalAny) / Convert.ToDouble(totalMain);

                    lblCounter.Text = string.Format("main: {0}\r\nany: {1}\r\nratio: {2}", totalMain.ToString(), totalAny.ToString(), ratio.ToStringSignificantDigits(1));
                }
                else
                {
                    lblCounter.Text = "";
                }

                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Ejemplo n.º 7
0
        private void World_Updating(object sender, WorldUpdatingArgs e)
        {
            try
            {
                _updateManager.Update_MainThread(e.ElapsedTime);

                _brushStrokes.Tick();

                _backImageManager.Update();
                _cameraHelper.Update();
                _miniMap.Update();
                _progressBars.Update();

                Point3D position = _player.Ship.PositionWorld;

                if (_stations != null)
                {
                    #region Space Stations

                    SpaceStation2D currentlyOverStation = null;

                    foreach (SpaceStation2D station in _stations)
                    {
                        // See if the ship is over the station
                        Point3D stationPosition = station.PositionWorld;
                        stationPosition.Z = 0;

                        if ((stationPosition - position).LengthSquared < station.Radius * station.Radius)
                        {
                            currentlyOverStation = station;
                        }
                    }

                    // Store the station that the ship is over
                    if (currentlyOverStation == null)
                    {
                        statusMessage.Content = "";
                    }
                    else
                    {
                        statusMessage.Content = "Press space to enter station";

                        //TODO:  Play a sound if this is the first time they entered the station's range
                    }

                    _currentlyOverStation = currentlyOverStation;

                    #endregion
                }

                #region Autosave

                if (DateTime.UtcNow > _nextAutoSave)
                {
                    SaveSession(true, true);
                    _nextAutoSave = GetNextAutoSaveTime();
                }

                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
 private void World_Updating(object sender, WorldUpdatingArgs e)
 {
 }
Ejemplo n.º 9
0
        private void World_Updating(object sender, WorldUpdatingArgs e)
        {
            try
            {
                if (_bodyAttraction != null)
                {
                    #region Body/Body Attraction

                    _bodyAttractionForces.Clear();

                    // Init the forces list
                    for (int cntr = 0; cntr < _bodySets.Count; cntr++)
                    {
                        foreach (Body body in _bodySets[cntr])
                        {
                            _bodyAttractionForces.Add(body, new Vector3D());
                        }
                    }

                    // Apply Gravity (bodies within the same set aren't compared to each other)
                    for (int outerSetCntr = 0; outerSetCntr < _bodySets.Count - 1; outerSetCntr++)
                    {
                        for (int outerCntr = 0; outerCntr < _bodySets[outerSetCntr].Length; outerCntr++)
                        {
                            Body bodyOuter = _bodySets[outerSetCntr][outerCntr];
                            Point3D position1 = bodyOuter.Position;

                            for (int innerSetCntr = outerSetCntr + 1; innerSetCntr < _bodySets.Count; innerSetCntr++)
                            {
                                for (int innerCntr = 0; innerCntr < _bodySets[innerSetCntr].Length; innerCntr++)
                                {
                                    Body bodyInner = _bodySets[innerSetCntr][innerCntr];
                                    Point3D position2 = bodyInner.Position;

                                    #region Apply Gravity

                                    Vector3D gravityLink = position1 - position2;

                                    // Calculate Force
                                    double force;
                                    switch (_bodyAttraction.AttractionType)
                                    {
                                        case BodyAttractionType.Constant:
                                            #region Constant

                                            force = _bodyAttraction.Strength;

                                            #endregion
                                            break;

                                        case BodyAttractionType.Spring:
                                            #region Spring

                                            force = _bodyAttraction.Strength * gravityLink.Length;

                                            #endregion
                                            break;

                                        case BodyAttractionType.SpringInverseDist:
                                            #region SpringInverseDist

                                            // Force should be max when distance is zero, and linearly drop off to nothing
                                            double inverseDistance = _bodyAttraction.Distance - gravityLink.Length;
                                            if (inverseDistance > 0d)
                                            {
                                                force = _bodyAttraction.Strength * inverseDistance;
                                            }
                                            else
                                            {
                                                force = 0d;
                                            }

                                            #endregion
                                            break;

                                        case BodyAttractionType.SpringDesiredDistance:
                                            #region SpringDesiredDistance

                                            //TODO:  Come up with a better name.  This is attracted to a point along a ring around the body

                                            // If inside the ring, it is attracted to the closer part of the ring (directly behind it, actually away from the other body)
                                            force = _bodyAttraction.Strength * (gravityLink.Length - _bodyAttraction.Distance);

                                            #endregion
                                            break;

                                        case BodyAttractionType.Gravity:
                                            #region Gravity

                                            force = _bodyAttraction.Strength * (bodyOuter.Mass * bodyInner.Mass) / gravityLink.LengthSquared;

                                            #endregion
                                            break;

                                        case BodyAttractionType.Tangent:
                                            #region Tangent

                                            // At a distance of _bodyAttraction.Distance, the tangent should have gone through one cycle (pi)
                                            //force = Math.Tan((gravityLink.Length / _bodyAttraction.Distance) * Math.PI);
                                            force = Math.Tan(gravityLink.Length * Math.PI / _bodyAttraction.Distance);
                                            //force = Math.Sin(gravityLink.Length * Math.PI / _bodyAttraction.Distance);

                                            #endregion
                                            break;

                                        default:
                                            throw new ApplicationException("Unknown BodyAttractionType: " + _bodyAttraction.AttractionType.ToString());
                                    }

                                    if (!_bodyAttraction.IsToward)
                                    {
                                        force *= -1;
                                    }

                                    if (!double.IsNaN(force) && !double.IsInfinity(force))
                                    {
                                        gravityLink.Normalize();
                                        gravityLink *= force;

                                        _bodyAttractionForces[bodyInner] += gravityLink;
                                        _bodyAttractionForces[bodyOuter] -= gravityLink;
                                    }

                                    #endregion
                                }
                            }

                        }
                    }

                    #endregion
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void World_Updating(object sender, WorldUpdatingArgs e)
        {
            _updateManager.Update_MainThread(e.ElapsedTime);

            if ((SHOWGRAVITYLINES || SHOWGRAVITYMASSES) && (DateTime.UtcNow - _debugGravityVisualsLastBuild).TotalMilliseconds > 150d)
            {
                #region Show gravity lines

                // Clear existing visuals
                if (_debugGravityVisuals == null)
                {
                    _debugGravityVisuals = new List<Visual3D>();
                }

                foreach (Visual3D model in _debugGravityVisuals)
                {
                    _viewport.Children.Remove(model);
                }
                _debugGravityVisuals.Clear();

                if (SHOWGRAVITYLINES)
                {
                    #region Lines

                    ScreenSpaceLines3D line = new ScreenSpaceLines3D(true);
                    line.Color = UtilityWPF.ColorFromHex("80B8DE2F");
                    line.Thickness = 1d;

                    double multiplier = .01d;

                    foreach (var field in _gravityField.GetLeaves())
                    {
                        line.AddLine(field.Position, field.Position + (field.Force * multiplier));
                    }

                    _debugGravityVisuals.Add(line);
                    _viewport.Children.Add(line);

                    #endregion
                }

                if (SHOWGRAVITYMASSES)
                {
                    #region Boxes

                    Model3DGroup geometries = new Model3DGroup();

                    double multiplier = .5d;
                    double oneThird = 1d / 3d;

                    foreach (var field in _gravityField.GetLeaves())
                    {
                        MaterialGroup material = new MaterialGroup();
                        material.Children.Add(new DiffuseMaterial(new SolidColorBrush(UtilityWPF.ColorFromHex("40CF835B"))));
                        material.Children.Add(new SpecularMaterial(new SolidColorBrush(UtilityWPF.ColorFromHex("A8FFA170")), 50d));

                        GeometryModel3D geometry = new GeometryModel3D();
                        geometry.Material = material;
                        geometry.BackMaterial = material;

                        double length = Math.Pow(field.Mass, oneThird) * multiplier;
                        length *= .5d;

                        geometry.Geometry = UtilityWPF.GetCube_IndependentFaces(new Point3D(field.Position.X - length, field.Position.Y - length, field.Position.Z - length), new Point3D(field.Position.X + length, field.Position.Y + length, field.Position.Z + length));

                        geometries.Children.Add(geometry);
                    }

                    ModelVisual3D model = new ModelVisual3D();
                    model.Content = geometries;

                    _debugGravityVisuals.Add(model);
                    _viewport.Children.Add(model);

                    #endregion
                }

                _debugGravityVisualsLastBuild = DateTime.UtcNow;

                #endregion
            }
        }
        private void World_Updating(object sender, WorldUpdatingArgs e)
        {
            try
            {
                #region Remove temp bodies

                int index = 0;
                while (index < _tempBodies.Count)
                {
                    if (_tempBodies[index].ShouldDie())
                    {
                        foreach (Visual3D visual in _tempBodies[index].PhysicsBody.Visuals)
                        {
                            _viewport.Children.Remove(visual);
                        }

                        _tempBodies[index].PhysicsBody.Dispose();

                        _tempBodies.RemoveAt(index);
                    }
                    else
                    {
                        index++;
                    }
                }

                #endregion

                if (radFallingSand.IsChecked.Value || (radFallingSandPlates.IsChecked.Value && (DateTime.UtcNow - _lastSandAdd).TotalMilliseconds > 5000d))
                {
                    #region Falling Sand

                    bool isPlate = radFallingSandPlates.IsChecked.Value;
                    Color color = UtilityWPF.ColorFromHex("FFF5EE95");
                    Vector3D size = new Vector3D(.05, .05, .05);
                    double maxDist = 1.1 * size.LengthSquared;
                    double mass = trkMass.Value;
                    double radius = 1d;
                    double startHeight = 1d;
                    TimeSpan lifespan = TimeSpan.FromSeconds(15d);
                    Vector3D velocity = new Vector3D(0, 0, -trkSpeed.Value);
                    int numCubes = isPlate ? 150 : 1;

                    // Calculate positions (they must all be calculated up front, or some sand will be built later)
                    List<Vector3D> positions = new List<Vector3D>();
                    for (int cntr = 0; cntr < numCubes; cntr++)
                    {
                        Vector3D startPos;
                        while (true)
                        {
                            startPos = Math3D.GetRandomVector_Circular(radius);

                            if (!positions.Any(o => (o - startPos).LengthSquared < maxDist))
                            {
                                break;
                            }
                        }

                        positions.Add(startPos);
                    }

                    // Place the sand
                    for (int cntr = 0; cntr < numCubes; cntr++)
                    {
                        Vector3D startPos = new Vector3D(positions[cntr].X, positions[cntr].Y, startHeight);
                        Quaternion orientation = isPlate ? Quaternion.Identity : Math3D.GetRandomRotation();

                        Body body = GetNewBody(CollisionShapeType.Box, color, size, mass, startPos.ToPoint(), orientation, _material_Sand);
                        body.Velocity = velocity;
                        body.LinearDamping = 0d;
                        body.AngularDamping = new Vector3D(0d, 0d, 0d);

                        _tempBodies.Add(new TempBody(body, lifespan));
                    }

                    // Remember when this was done
                    _lastSandAdd = DateTime.UtcNow;

                    #endregion
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void World_Updating(object sender, WorldUpdatingArgs e)
        {
            try
            {
                _updateManager.Update_MainThread(e.ElapsedTime);

                if (_bot == null)
                {
                    return;
                }

                RefillContainers(_bot);

                // If close to edge, move to origin




            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void World_Updating(object sender, WorldUpdatingArgs e)
        {
            #region Explosions

            int index = 0;
            while (index < _explosions.Count)
            {
                ExplosionWithVisual explosion = _explosions[index];

                // Tell this explosion to advance its shockwave
                if (explosion.Update(e.ElapsedTime))
                {
                    #region Remove Explosion

                    // It has expired, remove it
                    explosion.Dispose();
                    explosion.Body.Dispose();
                    _explosions.RemoveAt(index);

                    #endregion
                }
                else
                {
                    index++;
                }
            }

            #endregion
            #region Lit Fuses

            index = 0;
            while (index < _projectilesLitFuse.Keys.Count)
            {
                bool isExplode = !radProjectilesImplode.IsChecked.Value;		// not checking if explode is selected, because they may have switched to standard while the fuse was lit, just defaulting to explode

                Body projectile = _projectilesLitFuse.Keys[index];

                _projectilesLitFuse[projectile] -= e.ElapsedTime;
                if (_projectilesLitFuse[projectile] <= 0)
                {
                    #region Convert to explosion

                    ExplodeBody(projectile, isExplode, 1d);

                    // Remove from projectile lists
                    _projectiles.Remove(projectile);
                    _projectilesLitFuse.Remove(projectile);

                    #endregion
                }
                else
                {
                    index++;
                }
            }

            #endregion
        }