private void TwoBodyJointConstraint(Func <KinematicsSystem, IConstraintSolver> solver) { var a = new PhysicalParticle(); var b = new PhysicalParticle(); var c = new JointConstraint(a, Vector2.UnitX, b, -Vector2.UnitX); b.Position = 2 * Vector2.UnitX; b.Velocity.X = 10; system.AddParticles(a, b); system.AddConstraints(c); var constraintSolver = solver(system); var integrator = new KinematicsIntegrator(system, constraintSolver) { MaximumTimeStep = 0.01f, MaxStepsPerFrame = 500 }; integrator.Integrate(1); a.Position.X.Should().BeApproximately(5, tolerance); b.Position.X.Should().BeApproximately(7, tolerance); a.Velocity.X.Should().BeApproximately(5, tolerance); b.Velocity.X.Should().BeApproximately(5, tolerance); }
public KinematicsSystem Initialize(Size area) { system = new KinematicsSystem(); const int boxSize = 20; for (int i = 0; i < rows; i++) { double y = area.Height - boxSize * (i + 0.5); for (int j = 0; j < rows - i; j++) { int x = (area.Width - boxSize * 2 * rows) / 2 + boxSize * 2 * j + boxSize * i; var particle = new PhysicalParticle { Position = new Vector2(x, y), Polygon = new Rectangle(-boxSize, -boxSize / 2, boxSize * 2, boxSize).ToPolygon(), InertialMoment = boxSize * boxSize }; system.AddParticles(particle); } } system.AddForceField(new ConstantGravityField()); system.AddConstraints(new TwoBodyConstraint(new CollisionConstraint(), new AllPairs())); system.AddConstraints(new RectangleBoundaryConstraint(new Rectangle(Point.Zero, area))); return(system); }
public KinematicsSystem Initialize(Size area) { circleRadius = area.Height * 0.375f; circlePosition = new Vector2(area.Width * .5f, area.Height * .5f); var particlePosition = circlePosition + Vector2.FromPolarDegrees(circleRadius, -60) + new Vector2(boxSize * .5f, boxSize * .5f) ; //+ new Vector2(0, boxSize * .5f); system = new KinematicsSystem(); system.AddParticles(new PhysicalParticle { Position = particlePosition, //Angle = -(float)Math.PI / 4, InertialMoment = boxSize * boxSize / 12f, }); system.AddConstraints(new CirclePerimeterOffcenterConstraint( system.Particles[0], circlePosition, circleRadius, new Vector2(-boxSize * .5f, -boxSize * .5f))); system.AddForceField(new ConstantGravityField()); return(system); }
public static void CreateChain(KinematicsSystem system, int boxCount, float boxSize, Vector2 particlePosition) { for (int i = 0; i < boxCount; i++) { particlePosition += new Vector2(boxSize, 0); system.AddParticles( new PhysicalParticle { Position = particlePosition, InertialMoment = boxSize * boxSize / 12f, } ); if (i > 0) { PhysicalParticle last = system.Particles[i - 1]; PhysicalParticle current = system.Particles[i]; // Use the sign to alternate which corner is attached. This allows the chain to spread out diagonally. int sign = 2 * (i % 2) - 1; system.AddConstraints(new JointConstraint( last, new Vector2(boxSize * .5f, sign * boxSize * .5f), current, new Vector2(-boxSize * .5f, sign * boxSize * .5f))); } } }
public KinematicsSystem Initialize(Size area) { boxes.Clear(); system = new KinematicsSystem(); for (int i = 0; i < BoxCount; i++) { boxes.Add(new PhysicalParticle { Polygon = new Rectangle(-boxSize / 2, -boxSize / 2, boxSize, boxSize).ToPolygon(), Position = new Vector2( area.Width * 0.5 + (BoxCount / 2 - i) * boxSize, area.Height * 0.3 ), Velocity = new Vector2(0, (BoxCount / 2 - i) * 100) }); if (i > 0) { var constraint = new JointConstraint(boxes[i - 1], new Vector2(-boxSize * 0.5, 0), boxes[i], new Vector2(boxSize * 0.5, 0)); system.AddConstraints(constraint); } } sphere = new PhysicalParticle { Position = new Vector2(area.Width * 0.5, area.Height * 0.25) }; var constraintBox = boxes[boxes.Count / 2]; system.AddConstraints( new CirclePerimeterOffcenterConstraint(constraintBox, sphere.Position, 40, constraintBox.Polygon[0])); system.AddParticles(boxes.ToArray()); system.AddForceField(new ConstantGravityField()); return(system); }
public KinematicsSystem Initialize(Size area) { screenArea = area; ParticleGenerator generator = new ParticleGenerator(); system = new KinematicsSystem(); system.AddParticles(generator.Generate(particleCount, new Vector2(screenArea.Width / 2, 100), Vector2.Zero)); system.AddForceField(new ConstantGravityField()); system.AddConstraints(new RectangleBoundaryConstraint(new Rectangle(Point.Zero, area))); return(system); }
public KinematicsSystem Initialize(Size area) { circleRadius = area.Height * 0.375f; circlePosition = new Vector2(area.Width * .5f, area.Height * .5f); var particlePosition = circlePosition + Vector2.FromPolarDegrees(circleRadius, -90) + new Vector2(0, boxSize * .5f); system = new KinematicsSystem(); GeometryBuilder.CreateChain(system, BoxCount, boxSize, particlePosition); system.AddConstraints(new CirclePerimeterOffcenterConstraint( system.Particles.First(), circlePosition, circleRadius, new Vector2(-boxSize * .5f, -boxSize * .5f))); system.AddForceField(new ConstantGravityField()); return(system); }
public KinematicsSystem Initialize(Size area) { screenArea = area; ParticleGenerator generator = new ParticleGenerator(); system = new KinematicsSystem(); system.AddParticles(generator.Generate(particleCount, i => new Vector2(50 + 200 * i, 200), i => Vector2.Zero)); system.AddConstraints(new TwoBodyConstraint(new CollisionConstraint(), new AllPairs())); system.Particles.First().Velocity.X = 100; return(system); }
public KinematicsSystem Initialize(Size area) { system = new KinematicsSystem(); var planet = new PhysicalParticle { Position = (Vector2)area / 2, Polygon = new RegularPolygonBuilder().BuildPolygon(6, 60), Mass = 10, InertialMoment = 5 * 60 * 60, }; system.AddParticles(planet); GenerateParticles(planet); system.AddConstraints(new TwoBodyConstraint(new CollisionConstraint(), new AllPairs())); system.AddForceField(new PlanetGravity(planet, 4000000)); return(system); }
public KinematicsSystem Initialize(Size area) { circleRadius = area.Height * 0.375f; circlePosition = new Vector2(area.Width * 0.5f, area.Height * 0.5f); var particlePosition = circlePosition + Vector2.FromPolarDegrees(circleRadius, -60); system = new KinematicsSystem(); system.AddParticles(new PhysicalParticle { Position = particlePosition, Angle = 1.5f, AngularVelocity = 20f, }); system.AddConstraints(new CirclePerimeterConstraint(system.Particles[0], circlePosition, circleRadius)); system.AddForceField(new ConstantGravityField()); return(system); }