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)); }
/// <inheritdoc/> protected override void OnBeginUpdate(TimeSpan deltaTime) { float dt = (float)deltaTime.TotalSeconds; float emissionRate = (_emissionRateParameter != null) ? _emissionRateParameter.DefaultValue : DefaultEmissionRate; float numberOfParticles = emissionRate * dt + _leftoverParticles; if (EmissionLimit >= 0) { float totalCount = _emittedParticles + (int)numberOfParticles; if (totalCount > EmissionLimit) { numberOfParticles = EmissionLimit - _emittedParticles; } } if (numberOfParticles >= 1) { ParticleSystem.AddParticles((int)numberOfParticles, this); _emittedParticles += (int)numberOfParticles; } // The decimal fraction of numberOfParticles is truncated, so not the whole // deltaTime is really used. We store the unused fraction for the next frame. _leftoverParticles = numberOfParticles - (float)Math.Floor(numberOfParticles); }
protected override void OnBeginUpdate(TimeSpan deltaTime) { // If the particle system has moved a minimum distance, then we emit the // next particle of the ribbon. Vector3F newPosition = ParticleSystem.Pose.Position; if ((newPosition - _lastPosition).LengthSquared >= 0.3f) { ParticleSystem.AddParticles(1, this); _lastPosition = ParticleSystem.Pose.Position; } }
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)); }
public AnimatedTextureSample(Microsoft.Xna.Framework.Game game) : base(game) { _beeSwarm = BeeSwarm.Create(ContentManager); _beeSwarm.Pose = new Pose(new Vector3F(0, 4, 0)); // Create 100 bees. _beeSwarm.AddParticles(100); ParticleSystemService.ParticleSystems.Add(_beeSwarm); _particleSystemNode = new ParticleSystemNode(_beeSwarm); GraphicsScreen.Scene.Children.Add(_particleSystemNode); }
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)); }
// this function is called when we want to demo the explosion effect. it // updates the timeTillExplosion timer, and starts another explosion effect // when the timer reaches zero. private void UpdateExplosions(float dt) { timeTillExplosion -= dt; if (timeTillExplosion < 0) { Vector2 where = Vector2.Zero; // create the explosion at some random point on the screen. where.X = ParticleHelpers.RandomBetween(0, graphics.GraphicsDevice.Viewport.Width); where.Y = ParticleHelpers.RandomBetween(0, graphics.GraphicsDevice.Viewport.Height); // the overall explosion effect is actually comprised of two particle // systems: the fiery bit, and the smoke behind it. add particles to // both of those systems. explosion.AddParticles(where, Vector2.Zero); smoke.AddParticles(where, Vector2.Zero); // reset the timer. timeTillExplosion = TimeBetweenExplosions; AudioManager.PlaySoundEffect("explosion_medium"); } }
//Update the playfield area, clearing lines, updating score and playing sounds protected void UpdatePlayfield() { //GamePad.SetVibration(PlayerIndex.One, leftMotorVibration,rightMotorVibration); //Check for full rows int aNumberOfLinesCleared = mPlayfield.CheckRows(); if (aNumberOfLinesCleared > 0) { rumble = true; sounds.Play(Sound.SoundFX.LineClear); game = ScreenManager.Game; explosion = (ParticleSystem)ScreenManager.Game.Components[0]; smoke = (ParticleSystem)ScreenManager.Game.Components[1]; particleLocation = new Vector2(mPlayfield.ClearLineXpos, mPlayfield.ClearLineYpos); explosion.AddParticles(particleLocation); smoke.AddParticles(particleLocation); mNumberOfLinesCleared += 1; mTotalLines += 1; } else { if (mNumberOfLinesCleared >= 3) { mSound.Play(Sound.SoundFX.Reward); mScore += 1000; } else if (mNumberOfLinesCleared > 0) { mSound.Play(Sound.SoundFX.LineClear); mScore += 100 * mNumberOfLinesCleared; //mAutoFallThreshold -= .1F * (mLevel) * 100; }//end if (mNumberOfLinesCleared >= 3) mNumberOfLinesCleared = 0; }//end if (aNumberOfLinesCleared > 0) }
private ParticleSystem CreateParticleCloud(ContentManager content) { var ps = new ParticleSystem { Name = "Cloud", MaxNumberOfParticles = 10, Shape = new BoxShape(400, 300, 400), // This bounding shape must be big enough to include all particles! ReferenceFrame = ParticleReferenceFrame.Local, }; ps.Parameters.AddUniform <float>(ParticleParameterNames.Lifetime).DefaultValue = float.PositiveInfinity; ps.Parameters.AddVarying <Vector3>(ParticleParameterNames.Position); ps.Effectors.Add(new StartPositionEffector { Parameter = ParticleParameterNames.Position, Distribution = new SphereDistribution { Center = new Vector3(0), InnerRadius = 0, OuterRadius = 1, Scale = new Vector3(100, 50, 100), }, }); ps.Parameters.AddVarying <float>(ParticleParameterNames.Angle); ps.Effectors.Add(new StartValueEffector <float> { Parameter = ParticleParameterNames.Angle, Distribution = new UniformDistributionF(-0.5f, 0.5f), }); ps.Parameters.AddVarying <float>(ParticleParameterNames.Size); ps.Effectors.Add(new StartValueEffector <float> { Parameter = ParticleParameterNames.Size, Distribution = new UniformDistributionF(100, 200), }); ps.Parameters.AddUniform <Vector3>(ParticleParameterNames.Color); ps.Effectors.Add(new StartValueEffector <Vector3> { Parameter = ParticleParameterNames.Color, DefaultValue = new Vector3(0.6f), }); ps.Parameters.AddUniform <float>(ParticleParameterNames.Alpha).DefaultValue = 0.5f; // DigitalRune Graphics supports "texture atlases": The class PackedTexture // describes a single texture or tile set packed into a texture atlas. The // clouds texture in this example consists of 2 tiles. ps.Parameters.AddUniform <PackedTexture>(ParticleParameterNames.Texture).DefaultValue = new PackedTexture( "Clouds", content.Load <Texture2D>("Particles/Clouds"), Vector2F.Zero, Vector2F.One, 2, 1); // The particle parameter "AnimationTime" determines which tile is used, // where 0 = first tile, 1 = last tile. // --> Chooses a random tile for each particle when it is created. ps.Parameters.AddVarying <float>(ParticleParameterNames.AnimationTime); ps.Effectors.Add(new StartValueEffector <float> { Parameter = ParticleParameterNames.AnimationTime, Distribution = new UniformDistributionF(0, 1), }); // It looks better with back-to-front sorting, but for better performance we skip sorting. ps.Parameters.AddUniform <bool>(ParticleParameterNames.IsDepthSorted).DefaultValue = false; ps.Parameters.AddUniform <BillboardOrientation>(ParticleParameterNames.BillboardOrientation).DefaultValue = BillboardOrientation.ViewpointOriented; // Create some particles. ps.AddParticles(ps.MaxNumberOfParticles); return(ps); }