public static float DistanceBetweenPointAndLineSegment(ref Vector2 point, ref Vector2 start, ref Vector2 end) { if (start == end) { return(Vector2.Distance(point, start)); } Vector2 v = end - start; Vector2 w = point - start; float c1 = Vector2.Dot(w, v); if (c1 <= 0) { return(Vector2.Distance(point, start)); } float c2 = Vector2.Dot(v, v); if (c2 <= c1) { return(Vector2.Distance(point, end)); } float b = c1 / c2; Vector2 pointOnLine = start + v * b; return(Vector2.Distance(point, pointOnLine)); }
private void AdjustSprings(float timeStep) { foreach (var pair in _springs) { Spring spring = pair.Value; spring.Update(timeStep, Definition.KSpring, Definition.InfluenceRadius); if (spring.Active) { float L = spring.RestLength; float distance; Vector2.Distance(ref spring.P0.Position, ref spring.P1.Position, out distance); if (distance > (L + (Definition.YieldRatioStretch * L))) { spring.RestLength += timeStep * Definition.Plasticity * (distance - L - (Definition.YieldRatioStretch * L)); } else if (distance < (L - (Definition.YieldRatioCompress * L))) { spring.RestLength -= timeStep * Definition.Plasticity * (L - (Definition.YieldRatioCompress * L) - distance); } } else { _springsToRemove.Add(pair.Key); } } for (int i = 0; i < _springsToRemove.Count; ++i) { _springs.Remove(_springsToRemove[i]); } }
private void InitializePlasticity() { _isPlasticityInitialized = true; _springs.Clear(); float q; foreach (Particle pa in Particles) { foreach (Particle pb in Particles) { if (pa.GetHashCode() == pb.GetHashCode()) { continue; } Vector2.Distance(ref pa.Position, ref pb.Position, out q); Vector2.Subtract(ref pb.Position, ref pa.Position, out _rij); _rij /= q; if (q < RestLength) { _springs.Add(new Spring2(pa, pb, q)); } } pa.Velocity = Vector2.Zero; } }
/// <summary> /// Activate the explosion at the specified position. /// </summary> /// <param name="pos">The position (center) of the explosion.</param> /// <param name="radius">The radius of the explosion.</param> /// <param name="force">The force applied</param> /// <param name="maxForce">A maximum amount of force. When force gets over this value, it will be equal to maxForce</param> /// <returns>A list of bodies and the amount of force that was applied to them.</returns> public Dictionary <Body, Vector2> Activate(Vector2 pos, float radius, float force, float maxForce = float.MaxValue) { HashSet <Body> affectedBodies = new HashSet <Body>(); AABB aabb; aabb.LowerBound = pos - new Vector2(radius); aabb.UpperBound = pos + new Vector2(radius); // Query the world for bodies within the radius. World.QueryAABB(fixture => { if (Vector2.Distance(fixture.Body.Position, pos) <= radius) { if (!affectedBodies.Contains(fixture.Body)) { affectedBodies.Add(fixture.Body); } } return(true); }, ref aabb); return(ApplyImpulse(pos, radius, force, maxForce, affectedBodies)); }
public static bool IsCollisionPossible( PhysicalVector2 position1, PhysicalVector2 position2 ) { float distance = PhysicalVector2.Distance(position1, position2); return(distance < 64.0f * 6); }
public List <Vector3> SubdivideEvenly(int divisions) { List <Vector3> verts = new List <Vector3>(); float length = GetLength(); float deltaLength = length / divisions + 0.001f; float t = 0.000f; // we always start at the first control point Vector2 start = ControlPoints[0]; Vector2 end = GetPosition(t); // increment t until we are at half the distance while (deltaLength * 0.5f >= Vector2.Distance(start, end)) { end = GetPosition(t); t += 0.0001f; if (t >= 1f) { break; } } start = end; // for each box for (int i = 1; i < divisions; i++) { Vector2 normal = GetPositionNormal(t); float angle = (float)Math.Atan2(normal.Y, normal.X); verts.Add(new Vector3(end.X, end.Y, angle)); // until we reach the correct distance down the curve while (deltaLength >= Vector2.Distance(start, end)) { end = GetPosition(t); t += 0.00001f; if (t >= 1f) { break; } } if (t >= 1f) { break; } start = end; } return(verts); }
public float GetLength() { List <Vector2> verts = GetVertices(ControlPoints.Count * 25); float length = 0; for (int i = 1; i < verts.Count; i++) { length += Vector2.Distance(verts[i - 1], verts[i]); } if (Closed) { length += Vector2.Distance(verts[ControlPoints.Count - 1], verts[0]); } return(length); }
private Dictionary <Body, Vector2> ApplyImpulse(Vector2 pos, float radius, float force, float maxForce, HashSet <Body> overlappingBodies) { Dictionary <Body, Vector2> forces = new Dictionary <Body, Vector2>(overlappingBodies.Count); foreach (Body overlappingBody in overlappingBodies) { if (IsActiveOn(overlappingBody)) { float distance = Vector2.Distance(pos, overlappingBody.Position); float forcePercent = GetPercent(distance, radius); Vector2 forceVector = pos - overlappingBody.Position; forceVector *= 1f / (float)Math.Sqrt(forceVector.X * forceVector.X + forceVector.Y * forceVector.Y); forceVector *= Math.Min(force * forcePercent, maxForce); forceVector *= -1; overlappingBody.ApplyLinearImpulse(forceVector); forces.Add(overlappingBody, forceVector); } } return(forces); }
public static float Distance(Vector2 a, Vector2 b) { return(MonoGameVector2.Distance(a.MonoGameVector, b.MonoGameVector)); }
public static float Distance(Vector2 v1, Vector2 v2) { return(Vector.Distance(v1, v2)); }
public void Update() { Vector2.Distance(ref PA.Position, ref PB.Position, out CurrentDistance); }