/// <summary> /// creates a faux angle constraint by figuring out the required distance from a to c for the given angle /// </summary> /// <param name="a">The alpha component.</param> /// <param name="center">Center.</param> /// <param name="c">C.</param> /// <param name="stiffness">Stiffness.</param> /// <param name="angleInDegrees">Angle in degrees.</param> public static DistanceConstraint create( Particle a, Particle center, Particle c, float stiffness, float angleInDegrees ) { var aToCenter = Vector2.Distance( a.position, center.position ); var cToCenter = Vector2.Distance( c.position, center.position ); var distance = Mathf.sqrt( aToCenter * aToCenter + cToCenter * cToCenter - ( 2 * aToCenter * cToCenter * Mathf.cos( angleInDegrees * Mathf.deg2Rad ) ) ); return new DistanceConstraint( a, c, stiffness, distance ); }
public LineSegments( Vector2[] vertices, float stiffness ) { for( var i = 0; i < vertices.Length; i++ ) { var p = new Particle( vertices[i] ); addParticle( p ); if( i > 0 ) addConstraint( new DistanceConstraint( particles.buffer[i], particles.buffer[i - 1], stiffness ) ); } }
public DistanceConstraint( Particle first, Particle second, float stiffness, float distance = -1 ) { _particleOne = first; _particleTwo = second; this.stiffness = stiffness; if( distance > -1 ) restingDistance = distance; else restingDistance = Vector2.Distance( first.position, second.position ); }
public AngleConstraint( Particle a, Particle center, Particle c, float stiffness ) { _particleA = a; _centerParticle = center; _particleC = c; this.stiffness = stiffness; // not need for this Constraint to collide. There will be DistanceConstraints to do that if necessary collidesWithColliders = false; angleInRadians = angleBetweenParticles(); }
Particle createTreeBranch( Particle parent, int i, int nMax, float segmentCoef, Vector2 normal, float branchLength, float theta ) { var particle = new Particle( parent.position + ( normal * ( branchLength * segmentCoef ) ) ); addParticle( particle ); addConstraint( new DistanceConstraint( parent, particle, 0.7f ) ); if( i < nMax ) { var aRot = Mathf.rotateAround( normal, Vector2.Zero, -theta * Mathf.rad2Deg ); var bRot = Mathf.rotateAround( normal, Vector2.Zero, theta * Mathf.rad2Deg ); var a = createTreeBranch( particle, i + 1, nMax, segmentCoef * segmentCoef, aRot, branchLength, theta ); var b = createTreeBranch( particle, i + 1, nMax, segmentCoef * segmentCoef, bRot, branchLength, theta ); var jointStrength = Mathf.lerp( 0.9f, 0, (float)i / nMax ); addConstraint( new AngleConstraint( parent, particle, a, jointStrength ) ); addConstraint( new AngleConstraint( parent, particle, b, jointStrength ) ); } return particle; }