public override void ApplyForce() { x2 = new Vector3(PS.MousePosition); if (!PS.MouseDown) p1 = null; if (PS.MouseDown && p1 == null) { float min = Single.PositiveInfinity; p1 = null; foreach (Particle p in PS.Particles) { float dist = (p.Position - x2).LengthSquared; if (dist < min) { p1 = p; min = dist; restLength = (x2 - p1.Position).LengthSquared; } } restLength = (float)Math.Sqrt(restLength); } activated = p1 != null; if (activated) { Vector3 diff = x2 - p1.Position; float currentLength = diff.Length; diff.Normalize(); float dvDot = -Vector3.Dot(diff, p1.Velocity); diff *= Constants.Physics.STIFFNESS_STRETCH * (currentLength - restLength) + Constants.Physics.DAMPING_MASS * dvDot; p1.Force += diff * p1.Mass; } }
/// <summary> /// Constructs a new stretching spring force. /// </summary> /// <param name="p1">A particle.</param> /// <param name="p2">A particle.</param> public SpringForceStretch(Particle p1, Particle p2) { this.p1 = p1; this.p2 = p2; restLength = (p2.Position - p1.Position).Length; }
public CircleConstraint(Particle p1, Vector3 x2) { this.p1 = p1; this.x2 = x2; radius = (x2 - p1.Position).Length; }
/// <summary> /// Constructs a new spring bending force. /// </summary> /// <param name="p0">A particle that should be part of a SpringForceStretch along with p1.</param> /// <param name="p1">A particle that should be part of SpringForceStretch objects with both p0 and p2.</param> /// <param name="p2">A particle that should be part of a SpringForceStretch along with p1.</param> public SpringForceBend(Particle p0, Particle p1, Particle p2) { this.p0 = p0; this.p1 = p1; this.p2 = p2; }
/// <summary> /// Constructs a new pin constraint. /// </summary> /// <param name="p1">The particle to pin.</param> public PinConstraint(Particle p1) { this.p1 = p1; }
/// <summary> /// Creates a particle given a position and adds it to the particle system. /// </summary> /// <param name="position">The position of the newly created particle.</param> /// <returns>The newly created particle.</returns> public Particle CreateParticle(Vector3 position) { Particle p = new Particle(position); particles.Add(p); return p; }