Example #1
0
        public override void Update(GameTime gameTime)
        {
            if (InputService.IsPressed(MouseButtons.Left, true) || InputService.IsPressed(Buttons.RightTrigger, true, LogicalPlayerIndex.One))
            {
                var     cameraPose      = GraphicsScreen.CameraNode.PoseWorld;
                Vector3 cameraPosition  = cameraPose.Position;
                Vector3 cameraDirection = cameraPose.ToWorldDirection(Vector3.Forward);

                // Create a ray for picking.
                RayShape ray = new RayShape(cameraPosition, cameraDirection, 1000);

                // The ray should stop at the first hit. We only want the first object.
                ray.StopsAtFirstHit = true;

                // The collision detection requires a CollisionObject.
                CollisionObject rayCollisionObject = new CollisionObject(new GeometricObject(ray, Pose.Identity));

                // Get the first object that has contact with the ray.
                ContactSet contactSet = Simulation.CollisionDomain.GetContacts(rayCollisionObject).FirstOrDefault();
                if (contactSet != null && contactSet.Count > 0)
                {
                    // The ray has hit something.

                    // The contact set contains all detected contacts between the ray and the rigid body.
                    // Get the first contact in the contact set. (A ray hit usually contains exactly 1 contact.)
                    Contact contact     = contactSet[0];
                    var     hitPosition = contact.Position;
                    var     normal      = contact.Normal;
                    if (contactSet.ObjectA == rayCollisionObject)
                    {
                        normal = -normal;
                    }

                    // The particle parameter arrays are circular buffers. Get the particle array index
                    // where the next particle is created:
                    int particleIndex = (_decals.ParticleStartIndex + _decals.NumberOfActiveParticles) % _decals.MaxNumberOfParticles;

                    // Add 1 particle.
                    int numberOfCreatedParticles = _decals.AddParticles(1, null);
                    if (numberOfCreatedParticles > 0)
                    {
                        // We initialize the particle parameters Position, Normal and Axis manually using
                        // the results of the collision detection:
                        var positionParameter = _decals.Parameters.Get <Vector3>(ParticleParameterNames.Position);
                        positionParameter.Values[particleIndex] = hitPosition + normal * 0.01f; // We add a slight 1 cm offset to avoid z-fighting.

                        var normalParameter = _decals.Parameters.Get <Vector3>("Normal");
                        normalParameter.Values[particleIndex] = normal;

                        var axisParameter = _decals.Parameters.Get <Vector3>("Axis");
                        axisParameter.Values[particleIndex] = (normal == Vector3.Up) ? Vector3.Backward : Vector3.Up;
                    }
                }
            }

            // Synchronize particles <-> graphics.
            _particleSystemNode.Synchronize(GraphicsService);

            Profiler.AddValue("ParticleCount", ParticleHelper.CountNumberOfParticles(ParticleSystemService.ParticleSystems));
        }
Example #2
0
        public override void Update(GameTime gameTime)
        {
            // Synchronize particles <-> graphics.
            _particleSystemNode.Synchronize(GraphicsService);

            Profiler.AddValue("ParticleCount", ParticleHelper.CountNumberOfParticles(ParticleSystemService.ParticleSystems));
        }
Example #3
0
        public override void Update(GameTime gameTime)
        {
            // Move the particle system with the camera.
            _particleSystem.Pose = new Pose(GraphicsScreen.CameraNode.PoseWorld.Position);

            // Synchronize particles <-> graphics.
            _particleSystemNode.Synchronize(GraphicsService);

            Profiler.AddValue("ParticleCount", ParticleHelper.CountNumberOfParticles(ParticleSystemService.ParticleSystems));
        }
Example #4
0
        public override void Update(GameTime gameTime)
        {
            // The bee swarm effect needs the CameraPose to determine the bee texture orientation.
            _beeSwarm.Parameters.Get <Pose>("CameraPose").DefaultValue = GraphicsScreen.CameraNode.PoseWorld;

            // Synchronize particles <-> graphics.
            _particleSystemNode.Synchronize(GraphicsService);

            Profiler.AddValue("ParticleCount", ParticleHelper.CountNumberOfParticles(ParticleSystemService.ParticleSystems));
        }
Example #5
0
        public override void Update(GameTime gameTime)
        {
            // Move the model and the particle system with the rigid body.
            _modelNode.PoseWorld = _rigidBody.Pose;
            _particleSystem.Pose = _rigidBody.Pose;

            // Synchronize particles <-> graphics.
            _particleSystemNode.Synchronize(GraphicsService);

            Profiler.AddValue("ParticleCount", ParticleHelper.CountNumberOfParticles(ParticleSystemService.ParticleSystems));
        }
Example #6
0
        public override void Update(GameTime gameTime)
        {
            if (InputService.IsDown(MouseButtons.Left) || InputService.IsDown(Buttons.RightTrigger, LogicalPlayerIndex.One))
            {
                _flameJet.AddParticles(6);
            }

            // Synchronize particles <-> graphics.
            _particleSystemNode.Synchronize(GraphicsService);

            Profiler.AddValue("ParticleCount", ParticleHelper.CountNumberOfParticles(ParticleSystemService.ParticleSystems));
        }
Example #7
0
        public override void Update(GameTime gameTime)
        {
            // If enough time has passed, trigger the explosion sound and the explosion effect.
            _timeUntilExplosion -= gameTime.ElapsedGameTime;
            if (_timeUntilExplosion <= TimeSpan.Zero)
            {
                _explosion.Explode();
                _explosionSound.Play(0.2f, 0, 0);
                _timeUntilExplosion = ExplosionInterval;
            }

            // Synchronize particles <-> graphics.
            _particleSystemNode.Synchronize(GraphicsService);

            Profiler.AddValue("ParticleCount", ParticleHelper.CountNumberOfParticles(ParticleSystemService.ParticleSystems));
        }
Example #8
0
        public override void Update(GameTime gameTime)
        {
            // Add 2 new particles in each frame.
            _particleSystem.AddParticles(2);

            // Particles are added and simulated when the particle system service is updated.
            // In this example the service is updated in Game1.Update(). The service is updated
            // after all GameComponents, before Game1.Draw().

            // The ParticleSystemNode needs to be synchronized with the ParticleSystem.
            // The Synchronize() method takes a snapshot of the current particles which
            // is then rendered by the graphics service.
            // (This explicit synchronization is necessary because the particle system
            // service and the graphics service may run in parallel on multiple threads.)
            _particleSystemNode.Synchronize(GraphicsService);

            Profiler.AddValue("ParticleCount", ParticleHelper.CountNumberOfParticles(ParticleSystemService.ParticleSystems));
        }
Example #9
0
        public override void Update(GameTime gameTime)
        {
            bool wasDepthSorted = _brownOut.IsDepthSorted;

            _brownOut.IsDepthSorted = !(InputService.IsDown(MouseButtons.Left) || InputService.IsDown(Buttons.RightTrigger, LogicalPlayerIndex.One));
            if (wasDepthSorted != _brownOut.IsDepthSorted)
            {
                // DigitalRune Graphics caches states like IsDepthSorted. To delete the cached data,
                // we can delete the current ParticleSystem.RenderData.
                _brownOut.RenderData = null;
                foreach (var child in _brownOut.Children)
                {
                    child.RenderData = null;
                }
            }

            // Synchronize particles <-> graphics.
            _particleSystemNode.Synchronize(GraphicsService);

            Profiler.AddValue("ParticleCount", ParticleHelper.CountNumberOfParticles(ParticleSystemService.ParticleSystems));
        }
Example #10
0
        public override void Update(GameTime gameTime)
        {
            _waitTime -= gameTime.ElapsedGameTime;

            if (_waitTime < TimeSpan.Zero)
            {
                // Time to start the next effect at a random position.
                var position = _boxDistribution.Next(RandomHelper.Random);

                // Create teleport effect (the effect comes from a resource pool).
                var teleport = _pool.Obtain();
                teleport.Initialize(ContentManager);
                teleport.Pose = new Pose(position);

                // Add the teleport effect to the particle system service and the scene.
                ParticleSystemService.ParticleSystems.Add(teleport.ParticleSystem);
                GraphicsScreen.Scene.Children.Add(teleport.ParticleSystemNode);
                _teleportEffects.Add(teleport);

                _waitTime = TimeSpan.FromSeconds(1);
            }

            // Update teleport effects and recycle them if they are dead.
            for (int i = _teleportEffects.Count - 1; i >= 0; i--)
            {
                var  teleport = _teleportEffects[i];
                bool isAlive  = teleport.Update(GraphicsService);
                if (!isAlive)
                {
                    ParticleSystemService.ParticleSystems.Remove(teleport.ParticleSystem);
                    GraphicsScreen.Scene.Children.Remove(teleport.ParticleSystemNode);
                    _teleportEffects.RemoveAt(i);

                    _pool.Recycle(teleport);
                }
            }

            Profiler.AddValue("ParticleCount", ParticleHelper.CountNumberOfParticles(ParticleSystemService.ParticleSystems));
        }
        public override void Update(GameTime gameTime)
        {
            // Update SceneNode.LastPoseWorld (required for optional effects, like motion blur).
            _meshNode0.SetLastPose(true);
            _meshNode1.SetLastPose(true);

            // Synchronize pose of rigid body and model.
            _meshNode0.PoseWorld = _rigidBody0.Pose;
            _meshNode1.PoseWorld = _rigidBody1.Pose;

            // The particle system nodes are attached to the mesh nodes. Their pose is
            // updated automatically.

            // _particleSystem0 is relative to world space, we need to update its
            // pose explicitly.
            _particleSystem0.Pose = _rigidBody0.Pose;

            // Synchronize particles <-> graphics.
            _particleSystemNode0.Synchronize(GraphicsService);
            _particleSystemNode1.Synchronize(GraphicsService);

            Profiler.AddValue("ParticleCount", ParticleHelper.CountNumberOfParticles(ParticleSystemService.ParticleSystems));
        }