public unsafe override void Initialize(ContentArchive content, Camera camera) { camera.Position = new Vector3(-5f, 5.5f, 5f); camera.Yaw = MathHelper.Pi / 4; camera.Pitch = MathHelper.Pi * 0.15f; var filters = new BodyProperty <DeformableCollisionFilter>(); Simulation = Simulation.Create(BufferPool, new DeformableCallbacks { Filters = filters }, new DemoPoseIntegratorCallbacks(new Vector3(0, -10, 0))); var meshContent = content.Load <MeshContent>("Content\\newt.obj"); float cellSize = 0.1f; DumbTetrahedralizer.Tetrahedralize(meshContent.Triangles, cellSize, BufferPool, out var vertices, out var vertexSpatialIndices, out var cellVertexIndices, out var tetrahedraVertexIndices); var weldSpringiness = new SpringSettings(30f, 0); var volumeSpringiness = new SpringSettings(30f, 1); for (int i = 0; i < 5; ++i) { NewtDemo.CreateDeformable(Simulation, new Vector3(i * 3, 5 + i * 1.5f, 0), Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), MathF.PI * (i * 0.55f)), 1f, cellSize, weldSpringiness, volumeSpringiness, i, filters, ref vertices, ref vertexSpatialIndices, ref cellVertexIndices, ref tetrahedraVertexIndices); } BufferPool.Return(ref vertices); vertexSpatialIndices.Dispose(BufferPool); BufferPool.Return(ref cellVertexIndices); BufferPool.Return(ref tetrahedraVertexIndices); Simulation.Bodies.Add(BodyDescription.CreateConvexDynamic(new Vector3(0, 100, -.5f), 10, Simulation.Shapes, new Sphere(5))); Simulation.Statics.Add(new StaticDescription(new Vector3(0, -0.5f, 0), new CollidableDescription(Simulation.Shapes.Add(new Box(1500, 1, 1500)), 0.1f))); Simulation.Statics.Add(new StaticDescription(new Vector3(0, -1.5f, 0), new CollidableDescription(Simulation.Shapes.Add(new Sphere(3)), 0.1f))); var bulletShape = new Sphere(0.5f); bulletShape.ComputeInertia(.25f, out var bulletInertia); bulletDescription = BodyDescription.CreateDynamic(RigidPose.Identity, bulletInertia, new CollidableDescription(Simulation.Shapes.Add(bulletShape), 1f), new BodyActivityDescription(0.01f)); DemoMeshHelper.LoadModel(content, BufferPool, "Content\\newt.obj", new Vector3(20), out var mesh); Simulation.Statics.Add(new StaticDescription(new Vector3(200, 0.5f, 120), Quaternion.CreateFromAxisAngle(Vector3.UnitY, -3 * MathHelper.PiOver4), new CollidableDescription(Simulation.Shapes.Add(mesh), 0.1f))); }
public override void Initialize(ContentArchive content, Camera camera) { camera.Position = new Vector3(130, 50, 130); camera.Yaw = -MathF.PI * 0.25f; camera.Pitch = 0.4f; characterControllers = new CharacterControllers(BufferPool); Simulation = Simulation.Create(BufferPool, new CharacterNarrowphaseCallbacks(characterControllers), new DemoPoseIntegratorCallbacks(new Vector3(0, -10, 0))); DemoMeshHelper.LoadModel(content, BufferPool, @"Content\newt.obj", new Vector3(-10, 10, -10), out var newtMesh); var newtShape = Simulation.Shapes.Add(newtMesh); newts = new QuickList <SponsorNewt>(sponsors2.Count, BufferPool); newtArenaMin = new Vector2(-100); newtArenaMax = new Vector2(100); random = new Random(6); for (int i = 0; i < sponsors2.Count; ++i) { ref var newt = ref newts.AllocateUnsafely(); newt = new SponsorNewt(Simulation, newtShape, 0, newtArenaMin, newtArenaMax, random, i); }
public override void Initialize(ContentArchive content, Camera camera) { camera.Position = new Vector3(0, 8, -10); camera.Yaw = MathHelper.Pi; Simulation = Simulation.Create(BufferPool, new DemoNarrowPhaseCallbacks(), new DemoPoseIntegratorCallbacks(new Vector3(0, -10, 0)), new PositionFirstTimestepper()); DemoMeshHelper.LoadModel(content, BufferPool, @"Content\newt.obj", Vector3.One, out var mesh); new Box(2.5f, 1, 4).ComputeInertia(1, out var approximateInertia); var meshShapeIndex = Simulation.Shapes.Add(mesh); for (int meshIndex = 0; meshIndex < 3; ++meshIndex) { Simulation.Bodies.Add( BodyDescription.CreateDynamic(new Vector3(0, 2 + meshIndex * 2, 0), approximateInertia, new CollidableDescription(meshShapeIndex, 0.1f), new BodyActivityDescription(0.01f))); } var compoundBuilder = new CompoundBuilder(BufferPool, Simulation.Shapes, 12); for (int i = 0; i < mesh.Triangles.Length; ++i) { compoundBuilder.Add(mesh.Triangles[i], RigidPose.Identity, 1); } compoundBuilder.BuildDynamicCompound(out var children, out var compoundInertia); var compound = new BigCompound(children, Simulation.Shapes, BufferPool); var compoundShapeIndex = Simulation.Shapes.Add(compound); compoundBuilder.Dispose(); for (int i = 0; i < 3; ++i) { Simulation.Bodies.Add(BodyDescription.CreateDynamic(new Vector3(5, 2 + i * 2, 0), compoundInertia, new CollidableDescription(compoundShapeIndex, 0.1f), new BodyActivityDescription(0.01f))); } var staticShape = new Box(1500, 1, 1500); var staticShapeIndex = Simulation.Shapes.Add(staticShape); Simulation.Statics.Add(new StaticDescription(new Vector3(0, -0.5f, 0), new CollidableDescription(staticShapeIndex, 0.1f))); }
public override void Initialize(ContentArchive content, Camera camera) { camera.Position = new Vector3(130, 50, 130); camera.Yaw = -MathF.PI * 0.25f; camera.Pitch = 0.4f; characterControllers = new CharacterControllers(BufferPool); //Using a PositionLastTimestepper since we control the newts by velocity. Not as critical since they're kinematic and the position targets won't seek to cause undesired penetrations anyway, //but it'll avoid integrating velocities into positions before the solver has a chance to intervene. Simulation = Simulation.Create(BufferPool, new CharacterNarrowphaseCallbacks(characterControllers), new DemoPoseIntegratorCallbacks(new Vector3(0, -10, 0)), new PositionLastTimestepper()); DemoMeshHelper.LoadModel(content, BufferPool, @"Content\newt.obj", new Vector3(-10, 10, -10), out var newtMesh); var newtShape = Simulation.Shapes.Add(newtMesh); newts = new QuickList <SponsorNewt>(sponsors2.Count, BufferPool); newtArenaMin = new Vector2(-100); newtArenaMax = new Vector2(100); random = new Random(6); for (int i = 0; i < sponsors2.Count; ++i) { ref var newt = ref newts.AllocateUnsafely(); newt = new SponsorNewt(Simulation, newtShape, 0, newtArenaMin, newtArenaMax, random, i); }
public unsafe override void Initialize(ContentArchive content, Camera camera) { camera.Position = new Vector3(-32f, 20.5f, 61f); camera.Yaw = MathHelper.Pi * 0.3f; camera.Pitch = MathHelper.Pi * -0.05f; filters = new BodyProperty <SubgroupCollisionFilter>(BufferPool); Simulation = Simulation.Create(BufferPool, new SubgroupFilteredCallbacks() { CollisionFilters = filters }, new DemoPoseIntegratorCallbacks(new Vector3(0, -10, 0))); Simulation.Statics.Add(new StaticDescription(new Vector3(0, -0.5f, 0), new CollidableDescription(Simulation.Shapes.Add(new Box(1500, 1, 1500)), 0.1f))); Simulation.Statics.Add(new StaticDescription(new Vector3(0, 10, 0), new CollidableDescription(Simulation.Shapes.Add(new Box(70, 20, 80)), 0.1f))); Simulation.Statics.Add(new StaticDescription(new Vector3(0, 7.5f, 0), new CollidableDescription(Simulation.Shapes.Add(new Box(80, 15, 90)), 0.1f))); Simulation.Statics.Add(new StaticDescription(new Vector3(0, 5, 0), new CollidableDescription(Simulation.Shapes.Add(new Box(90, 10, 100)), 0.1f))); Simulation.Statics.Add(new StaticDescription(new Vector3(0, 2.5f, 0), new CollidableDescription(Simulation.Shapes.Add(new Box(100, 5, 110)), 0.1f))); //High fidelity simulation isn't super important on this one. Simulation.Solver.IterationCount = 2; DemoMeshHelper.LoadModel(content, BufferPool, "Content\\newt.obj", new Vector3(30), out var mesh); Simulation.Statics.Add(new StaticDescription(new Vector3(0, 20, 0), Quaternion.CreateFromAxisAngle(Vector3.UnitY, 0), new CollidableDescription(Simulation.Shapes.Add(mesh), 0.1f))); }
public unsafe override void Initialize(ContentArchive content, Camera camera) { camera.Position = new Vector3(20, 10, 20); camera.Yaw = MathF.PI; camera.Pitch = 0; characters = new CharacterControllers(BufferPool); Simulation = Simulation.Create(BufferPool, new CharacterNarrowphaseCallbacks(characters), new DemoPoseIntegratorCallbacks(new Vector3(0, -10, 0))); CreateCharacter(new Vector3(0, 2, -4)); //Create a bunch of legos to hurt your feet on. var random = new Random(5); var origin = new Vector3(-3f, 0.5f, 0); var spacing = new Vector3(0.5f, 0, -0.5f); for (int i = 0; i < 12; ++i) { for (int j = 0; j < 12; ++j) { var position = origin + new Vector3(i, 0, j) * spacing; var orientation = QuaternionEx.CreateFromAxisAngle(Vector3.Normalize(new Vector3(0.0001f) + new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble())), 10 * (float)random.NextDouble()); var shape = new Box(0.1f + 0.3f * (float)random.NextDouble(), 0.1f + 0.3f * (float)random.NextDouble(), 0.1f + 0.3f * (float)random.NextDouble()); var collidable = new CollidableDescription(Simulation.Shapes.Add(shape), 0.1f); shape.ComputeInertia(1, out var inertia); var choice = (i + j) % 3; switch (choice) { case 0: Simulation.Bodies.Add(BodyDescription.CreateDynamic(new RigidPose(position, orientation), inertia, collidable, new BodyActivityDescription(0.01f))); break; case 1: Simulation.Bodies.Add(BodyDescription.CreateKinematic(new RigidPose(position, orientation), collidable, new BodyActivityDescription(0.01f))); break; case 2: Simulation.Statics.Add(new StaticDescription(position, orientation, collidable)); break; } } } //Add some spinning fans to get slapped by. var bladeDescription = BodyDescription.CreateConvexDynamic(new Vector3(), 3, Simulation.Shapes, new Box(10, 0.2f, 2)); var bladeBaseDescription = BodyDescription.CreateConvexKinematic(new Vector3(), Simulation.Shapes, new Box(0.2f, 1, 0.2f)); for (int i = 0; i < 3; ++i) { bladeBaseDescription.Pose.Position = new Vector3(-22, 1, i * 11); bladeDescription.Pose.Position = new Vector3(-22, 1.7f, i * 11); var baseHandle = Simulation.Bodies.Add(bladeBaseDescription); var bladeHandle = Simulation.Bodies.Add(bladeDescription); Simulation.Solver.Add(baseHandle, bladeHandle, new Hinge { LocalHingeAxisA = Vector3.UnitY, LocalHingeAxisB = Vector3.UnitY, LocalOffsetA = new Vector3(0, 0.7f, 0), LocalOffsetB = new Vector3(0, 0, 0), SpringSettings = new SpringSettings(30, 1) }); Simulation.Solver.Add(baseHandle, bladeHandle, new AngularAxisMotor { LocalAxisA = Vector3.UnitY, TargetVelocity = (i + 1) * (i + 1) * (i + 1) * (i + 1) * 0.2f, Settings = new MotorSettings(5 * (i + 1), 0.0001f) }); } //Include a giant newt to test character-newt behavior and to ensure thematic consistency. DemoMeshHelper.LoadModel(content, BufferPool, @"Content\newt.obj", new Vector3(15, 15, 15), out var newtMesh); Simulation.Statics.Add(new StaticDescription(new Vector3(0, 0.5f, 0), new CollidableDescription(Simulation.Shapes.Add(newtMesh), 0.1f))); //Give the newt a tongue, I guess. var tongueBase = Simulation.Bodies.Add(BodyDescription.CreateKinematic(new Vector3(0, 8.4f, 24), default, default));
public unsafe override void Initialize(ContentArchive content, Camera camera) { camera.Position = new Vector3(-10, 0, -10); //camera.Yaw = MathHelper.Pi ; camera.Yaw = MathHelper.Pi * 3f / 4; //camera.Pitch = MathHelper.PiOver2 * 0.999f; Simulation = Simulation.Create(BufferPool, new DemoNarrowPhaseCallbacks(), new DemoPoseIntegratorCallbacks(new Vector3(0, -10, 0)), new PositionFirstTimestepper()); var box = new Box(1f, 3f, 2f); var capsule = new Capsule(1f, 1f); var sphere = new Sphere(1f); box.ComputeInertia(1, out var boxInertia); capsule.ComputeInertia(1, out var capsuleInertia); sphere.ComputeInertia(1, out var sphereInertia); var boxIndex = Simulation.Shapes.Add(box); var capsuleIndex = Simulation.Shapes.Add(capsule); var sphereIndex = Simulation.Shapes.Add(sphere); const int width = 16; const int height = 3; const int length = 16; for (int i = 0; i < width; ++i) { for (int j = 0; j < height; ++j) { for (int k = 0; k < length; ++k) { var location = new Vector3(5, 5, 5) * new Vector3(i, j, k);// + new Vector3(-width * 1.5f, 1.5f, -length * 1.5f); var bodyDescription = new BodyDescription { Activity = new BodyActivityDescription { MinimumTimestepCountUnderThreshold = 32, SleepThreshold = 0.01f }, Pose = new RigidPose { Orientation = Quaternion.Identity, Position = location }, Collidable = new CollidableDescription { Continuity = new ContinuousDetectionSettings { Mode = ContinuousDetectionMode.Discrete }, SpeculativeMargin = 0.1f } }; switch ((i + j) % 3) { case 0: bodyDescription.Collidable.Shape = sphereIndex; bodyDescription.LocalInertia = sphereInertia; break; case 1: bodyDescription.Collidable.Shape = capsuleIndex; bodyDescription.LocalInertia = capsuleInertia; break; case 2: bodyDescription.Collidable.Shape = boxIndex; bodyDescription.LocalInertia = boxInertia; break; } Simulation.Bodies.Add(bodyDescription); } } } //var testShape = new Box(50, 2, 0.2f); //testShape.ComputeInertia(1, out var testInertia); //Simulation.Bodies.Add(BodyDescription.CreateDynamic(new Vector3(10, 10, 10), testInertia, new CollidableDescription(Simulation.Shapes.Add(testShape), 10.1f), new BodyActivityDescription(-0.01f))); DemoMeshHelper.LoadModel(content, BufferPool, @"Content\newt.obj", new Vector3(5, 5, 5), out var newtMesh); newtMesh.ComputeClosedInertia(10, out var newtInertia, out _); Simulation.Bodies.Add(BodyDescription.CreateDynamic(new RigidPose(new Vector3(30, 20, 30)), newtInertia, new CollidableDescription(Simulation.Shapes.Add(newtMesh), 0.1f), new BodyActivityDescription(0.01f))); Simulation.Statics.Add(new StaticDescription(new Vector3(30, 15, 30), new CollidableDescription(Simulation.Shapes.Add(new Box(15, 1, 15)), 0.1f))); DemoMeshHelper.LoadModel(content, BufferPool, @"Content\box.obj", new Vector3(5, 1, 5), out var boxMesh); Simulation.Statics.Add(new StaticDescription(new Vector3(10, 5, -20), new CollidableDescription(Simulation.Shapes.Add(boxMesh), 0.1f))); DemoMeshHelper.CreateFan(64, 16, new Vector3(1, 1, 1), BufferPool, out var fanMesh); Simulation.Statics.Add(new StaticDescription(new Vector3(-10, 0, -20), new CollidableDescription(Simulation.Shapes.Add(fanMesh), 0.1f))); const int planeWidth = 128; const int planeHeight = 128; DemoMeshHelper.CreateDeformedPlane(planeWidth, planeHeight, (int x, int y) => { return(new Vector3(x - planeWidth / 2, 1 * MathF.Cos(x / 2f) * MathF.Sin(y / 2f), y - planeHeight / 2)); }, new Vector3(2, 1, 2), BufferPool, out var planeMesh); Simulation.Statics.Add(new StaticDescription(new Vector3(64, -10, 64), BepuUtilities.QuaternionEx.CreateFromAxisAngle(new Vector3(0, 1, 0), MathF.PI / 2), new CollidableDescription(Simulation.Shapes.Add(planeMesh), 0.1f))); }