public override void Update(float delta) { Frame = RelativeFrame.FromPoint(Body, Vector3.Zero); DxVector3 position = new DxVector3(Body.LinearPosition.X, Body.LinearPosition.Y, Body.LinearPosition.Z); DxQuaternion quat = new DxQuaternion(Body.AngularPosition.X, Body.AngularPosition.Y, Body.AngularPosition.Z, Body.AngularPosition.W); Transform = Matrix.RotationQuaternion(quat) * Matrix.Translation(position); Vector3 steering = new Vector3(); steering.X = (Frame.LinearPosition.X < 0) ? 3 : -3; steering.Z = (Frame.LinearPosition.Z < 0) ? 3 : -3; const int Thrust = 150; steering.Y = Thrust; if (Frame.LinearPosition.Y > 0) { steering.Y = Math.Min(Thrust, Frame.LinearPosition.Y + 20); } //Body.AccumulateLocalCentralForce(force); Body.AccumulateLocalForce(steering, new Vector3(0, -2, 0)); //Body.AccumulateGlobalCentralForce(new Vector3(y: -9.81f)); Body.LinearDampening = new Vector3(0.5f); Body.AngularDampening = new Vector3(0.5f); Body.Integrate(delta); Body.ClearForces(); // Eject particles countdown += delta; while (countdown > 0.01f) { var body = new Body(); body.LinearPosition = RelativeFrame.ToGlobalPoint(Body, new Vector3(y: -8)); body.LinearVelocity = Body.LinearVelocity; body.AngularPosition = Body.AngularPosition; body.ApplyLocalCentralImpulse(new Vector3(0, -steering.Y, 0)); body.AccumulateGlobalCentralForce(new Vector3(y: 10)); body.LinearDampening = new Vector3(0.2f); Particles.Insert(0, body); countdown -= 0.01f; } if (Particles.Count > 100) { Particles.RemoveRange(100, Particles.Count - 100); } ParticleMesh.IndexCount = Particles.Count; for (int i = 0; i < Particles.Count; i++) { Particles[i].Integrate(delta); ParticleMesh.Dynamic[i].Position = Particles[i].LinearPosition; } ParticleMesh.UpdateDynamic(); }
public void Update() { if (!Dead) { if (Finished) { Dead = true; } else { if (shouldAdd && Enabled) { Particle particle = DefaultParticle; if (Settings.Burst) { for (int i = 0; i < Settings.BurstSize; i++) { if (!Settings.SpawnInCircle) { particle = new Particle(Settings, Rectangle.RandomVectorInside()); } else { particle = new Particle(Settings, Helper.RandomPointOnCircle(Position, Helper.Rand.Next(Settings.CircleRadius))); } } shouldAdd = false; } else if (decimalDensity) { if (currentDensity >= 1) { if (Scale == Vector2.Zero) { particle = new Particle(Settings, Position); } else { if (!Settings.SpawnInCircle) { particle = new Particle(Settings, Rectangle.RandomVectorInside()); } else { particle = new Particle(Settings, Helper.RandomPointOnCircle(Position, Helper.Rand.Next(Settings.CircleRadius))); } } currentDensity = 0; } else { currentDensity += Game.DeltaTime * Settings.DensityIncreaser; } } else { for (int i = 0; i < Settings.Density; i++) { if (Scale == Vector2.Zero) { particle = new Particle(Settings, Position); } else { if (!Settings.SpawnInCircle) { particle = new Particle(Settings, Rectangle.RandomVectorInside()); } else { particle = new Particle(Settings, Helper.RandomPointOnCircle(Position, Helper.Rand.Next(Settings.CircleRadius))); } } } } if (particle != DefaultParticle) { if (!Settings.SpawnParticleAtTheEnd) { Particles.Insert(0, particle); } else { Particles.Add(particle); } } if (Settings.OneTime) { lifetime -= Game.DeltaTime; if (lifetime < 0) { shouldAdd = false; } } } if (Particles.Count == 0 && !shouldAdd) { Finished = true; } if (Settings.MaxParticles != 0 && Particles.Count > Settings.MaxParticles) { int count = Particles.Count - Settings.MaxParticles - 1; if (!Settings.SpawnParticleAtTheEnd) { Particles.RemoveRange(Settings.MaxParticles + 1, count); } else { Particles.RemoveRange(0, count); } } foreach (var p in Particles.ToArray()) { if (p.Dead) { Particles.Remove(p); } else { p.Update(); } } } } }