private static Vector3 CalculateExternalForces(Particle p) { // not efficient at all Vector3 force = new Vector3(0, -GRAVITY * p.Mass, 0); return force; }
private void LoadParticleSystem(string configFile) { _particles = new List<Particle>(); _constraints = new List<Constraint>(); XmlDocument doc = new XmlDocument(); doc.Load(configFile); // load transforms Vector3 translation = GetVectorFromString(doc.SelectSingleNode("/ParticleSystem/Transform").Attributes["Translation"].Value); Vector3 scale = GetVectorFromString(doc.SelectSingleNode("/ParticleSystem/Transform").Attributes["Scale"].Value); // load particles foreach (XmlElement particleElement in doc.SelectNodes("/ParticleSystem/Particles/Particle")) { Vector3 position = GetVectorFromString(particleElement.Attributes["Position"].Value); position *= scale; position += translation; Particle particle = new Particle(position, this.Game); if (particleElement.HasAttribute("Mass")) { string massString = particleElement.Attributes["Mass"].Value; if (massString == "INFINITE_MASS") particle.Mass = Particle.INFINITE_MASS; else particle.Mass = Convert.ToSingle(massString); } _particles.Add(particle); } // load constraints foreach (XmlElement constraintElement in doc.SelectNodes("/ParticleSystem/Constraints/*")) { string constraintType = constraintElement.Name; Constraint constraint = null; switch (constraintType) { case "DistanceConstraint" : Particle particleA = _particles[Convert.ToInt32(constraintElement.Attributes["ParticleA"].Value)]; Particle particleB = _particles[Convert.ToInt32(constraintElement.Attributes["ParticleB"].Value)]; float restLength; if (constraintElement.HasAttribute("RestLength")) restLength = Convert.ToSingle(constraintElement.Attributes["RestLength"].Value); else restLength = Vector3.Distance(particleA.Position, particleB.Position); constraint = new DistanceConstraint(this, particleA, particleB, restLength); if (constraintElement.HasAttribute("Stiffness")) constraint.Stiffness = Convert.ToSingle(constraintElement.Attributes["Stiffness"].Value); break; } if (constraint != null) _constraints.Add(constraint); } }
private void GenerateCollisionConstraints(Particle p, List<Constraint> collisionConstraints) { // collide with terrain SimpleTerrain terrain = (SimpleTerrain) this.Game.Services.GetService(typeof(SimpleTerrain)); if (terrain != null) { float terrainHeight = terrain.GetHeight(p.CandidatePosition.X, p.CandidatePosition.Z); if (p.CandidatePosition.Y < terrainHeight) { collisionConstraints.Add(new GroundCollisionConstraint(this, p, terrainHeight)); p.WasInvolvedInCollision = true; } } // collide with meshes foreach (GameComponent gameComponent in this.Game.Components) { if (gameComponent is Mesh && gameComponent != _graphicObject) { Mesh mesh = (Mesh) gameComponent; if (mesh.BoundingSphere.Contains(p.CandidatePosition) == ContainmentType.Contains) { collisionConstraints.Add(new SphereCollisionConstraint(this, p, mesh.BoundingSphere)); p.WasInvolvedInCollision = true; this._collidedWithMesh = true; } } } }
public GroundCollisionConstraint(ParticleSystem particleSystem, Particle particle, float terrainHeight) : this(particleSystem, particle, terrainHeight, 1) { }
public GroundCollisionConstraint(ParticleSystem particleSystem, Particle particle, float terrainHeight, float stiffness) : base(particleSystem, stiffness) { _particle = particle; _terrainHeight = terrainHeight; }
public SphereCollisionConstraint(ParticleSystem particleSystem, Particle particle, BoundingSphere boundingSphere) : this(particleSystem, particle, boundingSphere, 1) { }
public SphereCollisionConstraint(ParticleSystem particleSystem, Particle particle, BoundingSphere boundingSphere, float stiffness) : base(particleSystem, stiffness) { _particle = particle; _boundingSphere = boundingSphere; }