/** * Init cable particles * * Creates the cable particles along the cable length * and binds the start and end tips to their respective game objects. */ void InitCableParticles() { // Calculate segments to use if (totalSegments > 0) { segments = totalSegments; } else { segments = Mathf.CeilToInt(cableLength * segmentsPerUnit); } Vector3 cableDirection = (endPoint.position - transform.position).normalized; float initialSegmentLength = cableLength / segments; points = new CableParticle[segments + 1]; // Foreach point for (int pointIdx = 0; pointIdx <= segments; pointIdx++) { // Initial position Vector3 initialPosition = transform.position + (cableDirection * (initialSegmentLength * pointIdx)); points[pointIdx] = new CableParticle(initialPosition); } // Bind start and end particles with their respective gameobjects CableParticle start = points[0]; CableParticle end = points[segments]; start.Bind(this.transform); end.Bind(endPoint.transform); }
void InitCableParticles() { if (totalSegments > 0) { segments = totalSegments; } else { segments = Mathf.CeilToInt(cableLength * segmentsPerUnit); } Vector3 cableDirection = (endPoint.position - transform.position).normalized; float initialSegmentLength = cableLength / segments; points = new CableParticle[segments + 1]; for (int pointIdx = 0; pointIdx <= segments; pointIdx++) { Vector3 initialPosition = transform.position + (cableDirection * (initialSegmentLength * pointIdx)); points[pointIdx] = new CableParticle(initialPosition); } CableParticle start = points[0]; CableParticle end = points[segments]; start.Bind(this.transform); end.Bind(endPoint.transform); }
/** * Stiffness constraint **/ void SolveColission() { for (int SegIdx = 0; SegIdx < segments + 1; SegIdx++) { CableParticle particleA = points[SegIdx]; if (particleA.IsFree() && particleA.Position.y < (cableWidth)) { particleA.Position += (cableWidth - particleA.Position.y) * Vector3.up - particleA.Velocity * friction; } } }
private void SolveDistanceConstraint() { float segmentLength = cableLength / segments; for (int i = 0; i < segments; i++) { CableParticle particleA = points[i]; CableParticle particleB = points[i + 1]; SolveDistanceConstraint(particleA, particleB, segmentLength); } }
void SolveDistanceConstraint() { float segmentLength = cableLength / segments; for (int SegIdx = 0; SegIdx < segments; SegIdx++) { CableParticle particleA = points[SegIdx]; CableParticle particleB = points[SegIdx + 1]; SolveDistanceConstraint(particleA, particleB, segmentLength); } }
void SolveDistanceConstraint(CableParticle particleA, CableParticle particleB, float segmentLength) { Vector3 delta = particleB.Position - particleA.Position; float currentDistance = delta.magnitude; float errorFactor = (currentDistance - segmentLength) / currentDistance; if (particleA.IsFree() && particleB.IsFree()) { particleA.Position += errorFactor * 0.5f * delta; particleB.Position -= errorFactor * 0.5f * delta; } else if (particleA.IsFree()) { particleA.Position += errorFactor * delta; } else if (particleB.IsFree()) { particleB.Position -= errorFactor * delta; } }
/** * Distance Constraint * * This is the main constrains that keeps the cable particles "tied" together. */ void SolveDistanceConstraint(CableParticle particleA, CableParticle particleB, float segmentLength) { // Find current vector between particles Vector3 delta = particleB.Position - particleA.Position; // float currentDistance = delta.magnitude; float errorFactor = (currentDistance - segmentLength) / currentDistance; // Only move free particles to satisfy constraints if (particleA.IsFree() && particleB.IsFree()) { particleA.Position += errorFactor * 0.5f * delta; particleB.Position -= errorFactor * 0.5f * delta; } else if (particleA.IsFree()) { particleA.Position += errorFactor * delta; } else if (particleB.IsFree()) { particleB.Position -= errorFactor * delta; } }
/** * TODO: I'll implement this constraint to reinforce cable stiffness * * As the system has more particles, the verlet integration aproach * may get way too loose cable simulation. This constraint is intended * to reinforce the cable stiffness. * // throw new System.NotImplementedException (); **/ void SolveStiffnessConstraint(CableParticle cableParticle, float distance) { }