/// <summary> /// When overridden in the derived class, handles processing the <paramref name="particle"/> when /// it is updated. Only valid if <see cref="ParticleModifier.ProcessOnUpdate"/> is set. /// </summary> /// <param name="emitter">The <see cref="ParticleEmitter"/> that the <paramref name="particle"/> /// came from.</param> /// <param name="particle">The <see cref="Particle"/> to process.</param> /// <param name="elapsedTime">The amount of time that has elapsed since the <paramref name="emitter"/> /// was last updated.</param> protected override void HandleProcessUpdated(ParticleEmitter emitter, Particle particle, int elapsedTime) { // Get the absolute position Vector2 absolutePosition; emitter.GetAbsoultePosition(ref _position, out absolutePosition); // Get the distance from the particle Vector2 distance; Vector2.Subtract(ref absolutePosition, ref particle.Position, out distance); // Check if the particle is in range if (distance.LengthSquared() >= _radiusSquared) { return; } // Normalize the distance vector to get the force Vector2 force; Vector2.Normalize(ref distance, out force); // Adjust the force by the strength and elapsed time Vector2.Multiply(ref force, _strength * elapsedTime, out force); // Apply the force to the particle particle.ApplyForce(ref force); }
public void Simulate(double dt) { if (dragging) { VectorN dragForce = new VectorN(2); dragForce.v[0] = mousex - dragged.pos.v[0]; dragForce.v[1] = mousey - dragged.pos.v[1]; dragForce.Scale(50 * dragged.mass); dragged.ApplyForce(dragForce); dragged.Unfreeze(); } for (int i = 0; i < particles.Count; i++) { if (particles[i] != dragged) { particles[i].ApplyGravity(); } } dynamics.SimulateFast(dt); CheckBounds(); }
/// <summary> /// Processes the specified Particle. /// </summary> public override void Process(float totalSeconds, float elapsedSeconds, ref Particle particle, object tag) { Vector2 deltaGrav; Vector2.Multiply(ref this.Gravity, elapsedSeconds, out deltaGrav); particle.ApplyForce(ref deltaGrav); }
private float _strength; //Gravity strength in pixels per second. /// <summary> /// Processes an active particle. /// </summary> /// <param name="totalTime">Total game time in whole and fractional seconds.</param> /// <param name="elapsedTime">Elapsed game time in whole and fractional seconds.</param> /// <param name="particle">The particle to be updated.</param> /// <param name="age">The age of the particle in the range of zero to one inclusive.</param> public override void ProcessActiveParticle(float totalTime, float elapsedTime, ref Particle particle, float age) { Vector2 force = this._gravity; Vector2.Multiply(ref force, this._strength * elapsedTime, out force); particle.ApplyForce(ref force); }
private float _coefficient; //The damping coefficient. /// <summary> /// Processes an active particle. /// </summary> /// <param name="totalTime">Total game time in whole and fractional seconds.</param> /// <param name="elapsedTime">Elapsed game time in whole and fractional seconds.</param> /// <param name="particle">The particle to be processed.</param> /// <param name="age">The age of the particle in the range 0-1.</param> public override void ProcessActiveParticle(float totalTime, float elapsedTime, ref Particle particle, float age) { Vector2 momentum = particle.Momentum; Vector2.Multiply(ref momentum, this._coefficient, out momentum); Vector2.Negate(ref momentum, out momentum); particle.ApplyForce(ref momentum); }
/// <summary> /// Processes the specified Particle. /// </summary> public override void Process(float totalSeconds, float elapsedSeconds, ref Particle particle, object tag) { Vector2 distance; // Calculate the distance between the Particle and the gravity well... Vector2.Subtract(ref this.Position, ref particle.Position, out distance); // Check to see if the Particle is within range of the gravity well... if (distance.LengthSquared() < this.RadiusSquared) { Vector2 force; // We can re-use the distance vector, and normalise it as the force vector... Vector2.Normalize(ref distance, out force); // Adjust the force vector based on the strength of the gravity well and the time delta... Vector2.Multiply(ref force, this.Strength * elapsedSeconds, out force); particle.ApplyForce(ref force); } }
void Update() { for (int i = 0; i < ParticlesCount; i++) { Particle particle = particles[i]; Vector3 position = particle.transform.position; int x = Mathf.FloorToInt((position.x - (transform.position.x - size * 0.5f)) / size * resolution); int y = Mathf.FloorToInt((position.y - (transform.position.y - size * 0.5f)) / size * resolution); int z = Mathf.FloorToInt((position.z - (transform.position.z - size * 0.5f)) / size * resolution); particle.ApplyForce(field[x, y, z] * 0.01f); particle.Update(); if (particle.transform.position.x <= (transform.position.x - size * 0.5f) || particle.transform.position.x >= (transform.position.x + size * 0.5f) || particle.transform.position.y <= (transform.position.y - size * 0.5f) || particle.transform.position.y >= (transform.position.y + size * 0.5f) || particle.transform.position.z <= (transform.position.z - size * 0.5f) || particle.transform.position.z >= (transform.position.z + size * 0.5f)) { particle.transform.position = NewPosition(); } } UpdateFlowField(); }
/// <summary> /// When overridden in the derived class, handles processing the <paramref name="particle"/> when /// it is updated. Only valid if <see cref="ParticleModifier.ProcessOnUpdate"/> is set. /// </summary> /// <param name="emitter">The <see cref="ParticleEmitter"/> that the <paramref name="particle"/> /// came from.</param> /// <param name="particle">The <see cref="Particle"/> to process.</param> /// <param name="elapsedTime">The amount of time that has elapsed since the <paramref name="emitter"/> /// was last updated.</param> protected override void HandleProcessUpdated(ParticleEmitter emitter, Particle particle, int elapsedTime) { // Get the absolute position Vector2 absolutePosition; emitter.GetAbsoultePosition(ref _position, out absolutePosition); // Get the distance from the particle Vector2 distance; Vector2.Subtract(ref absolutePosition, ref particle.Position, out distance); // Check if the particle is in range if (distance.LengthSquared() >= _radiusSquared) return; // Normalize the distance vector to get the force Vector2 force; Vector2.Normalize(ref distance, out force); // Adjust the force by the strength and elapsed time Vector2.Multiply(ref force, _strength * elapsedTime, out force); // Apply the force to the particle particle.ApplyForce(ref force); }
public static void PhysicsString() { PhysicsManager mgr = new PhysicsManager(); Particle a = new Particle(1.0f) { IsAffectedByAirFriction = true }; Particle b = new Particle(2.0f) { IsAffectedByAirFriction = true }; StringJoint stringJ = new StringJoint(a, b, 1.0f, 100.0f, 0.15f); mgr.PhysicsObjects.Add(a); mgr.PhysicsObjects.Add(b); mgr.PhysicsObjects.Add(stringJ); a.Position = Game.LocalPlayer.Character.GetOffsetPosition(new Vector3(0f, 2.0f, 1.0f)); b.Position = Game.LocalPlayer.Character.GetOffsetPosition(new Vector3(0f, 3.25f, 1.0f)); Vector3 positionChange = new Vector3(); mgr.AfterSolve += () => { if (positionChange != Vector3.Zero) { a.ApplyForce(positionChange); } }; while (true) { GameFiber.Yield(); positionChange = new Vector3(); const float moveValue = 25f; if (Game.IsKeyDownRightNow(Keys.NumPad6)) { positionChange.X += moveValue; } if (Game.IsKeyDownRightNow(Keys.NumPad4)) { positionChange.X -= moveValue; } if (Game.IsKeyDownRightNow(Keys.NumPad8)) { positionChange.Y -= moveValue; } if (Game.IsKeyDownRightNow(Keys.NumPad2)) { positionChange.Y += moveValue; } if (Game.IsKeyDownRightNow(Keys.NumPad9)) { positionChange.Z += moveValue; } if (Game.IsKeyDownRightNow(Keys.NumPad3)) { positionChange.Z -= moveValue; } mgr.Update(); } }
/// <summary> /// Processes an active particle. /// </summary> /// <param name="totalTime">Total game time in whole and fractional seconds.</param> /// <param name="elapsedTime">Elapsed game time in whole and fractional seconds.</param> /// <param name="particle">The particle which is ready to be processed.</param> /// <param name="age">The age of the particle in the range 0-1.</param> public override void ProcessActiveParticle(float totalTime, float elapsedTime, ref Particle particle, float age) { float distance; Vector2.DistanceSquared(ref this._position, ref particle.Position, out distance); if (distance < this._radiusSq) { float effect = 1f - (distance / this._radiusSq); Vector2 force; Vector2.Subtract(ref this._position, ref particle.Position, out force); Vector2.Normalize(ref force, out force); Vector2.Transform(ref force, ref this._rotation, out force); Vector2.Multiply(ref force, effect * elapsedTime * this._vorticity, out force); particle.ApplyForce(ref force); } }