Пример #1
0
        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)));

            MeshDemo.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)));
        }
Пример #2
0
        public unsafe override void Initialize(ContentArchive content, Camera camera)
        {
            camera.Position = new Vector3(-20f, 13, -20f);
            camera.Yaw      = MathHelper.Pi * 3f / 4;
            camera.Pitch    = MathHelper.Pi * 0.1f;
            Simulation      = Simulation.Create(BufferPool, new NoCollisionCallbacks());
            //Simulation.PoseIntegrator.Gravity = new Vector3(0, -10, 0);

            var       box                 = new Box(0.5f, 1.5f, 1f);
            var       capsule             = new Capsule(0, 0.5f);
            var       sphere              = new Sphere(0.5f);
            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              = 16;
            const int length              = 16;
            var       spacing             = new Vector3(2.01f);
            var       halfSpacing         = spacing / 2;
            float     randomizationSubset = 0.9f;
            var       randomizationSpan   = (spacing - new Vector3(1)) * randomizationSubset;
            var       randomizationBase   = randomizationSpan * -0.5f;
            var       random              = new Random(5);

            for (int i = 0; i < width; ++i)
            {
                for (int j = 0; j < height; ++j)
                {
                    for (int k = 0; k < length; ++k)
                    {
                        var r        = new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                        var location = spacing * (new Vector3(i, j, k) + new Vector3(-width, -height, -length) * 0.5f) + randomizationBase + r * randomizationSpan;

                        BepuUtilities.Quaternion orientation;
                        orientation.X = -1 + 2 * (float)random.NextDouble();
                        orientation.Y = -1 + 2 * (float)random.NextDouble();
                        orientation.Z = -1 + 2 * (float)random.NextDouble();
                        orientation.W = 0.01f + (float)random.NextDouble();
                        orientation.Normalize();

                        TypedIndex shapeIndex;
                        switch ((i + j + k) % 3)
                        {
                        case 0:
                            shapeIndex = boxIndex;
                            break;

                        case 1:
                            shapeIndex = capsuleIndex;
                            break;

                        default:
                            shapeIndex = sphereIndex;
                            break;
                        }

                        if ((i + j + k) % 2 == 1)
                        {
                            var bodyDescription = new BodyDescription
                            {
                                Activity = new BodyActivityDescription {
                                    MinimumTimestepCountUnderThreshold = 32, SleepThreshold = -0.1f
                                },
                                Pose = new RigidPose
                                {
                                    Orientation = orientation,
                                    Position    = location
                                },
                                Collidable = new CollidableDescription
                                {
                                    Continuity = new ContinuousDetectionSettings {
                                        Mode = ContinuousDetectionMode.Discrete
                                    },
                                    SpeculativeMargin = 0.1f,
                                    Shape             = shapeIndex
                                }
                            };
                            Simulation.Bodies.Add(bodyDescription);
                        }
                        else
                        {
                            var staticDescription = new StaticDescription
                            {
                                Pose = new RigidPose
                                {
                                    Orientation = orientation,
                                    Position    = location
                                },
                                Collidable = new CollidableDescription
                                {
                                    Continuity = new ContinuousDetectionSettings {
                                        Mode = ContinuousDetectionMode.Discrete
                                    },
                                    SpeculativeMargin = 0.1f,
                                    Shape             = shapeIndex
                                }
                            };
                            Simulation.Statics.Add(staticDescription);
                        }
                    }
                }
            }

            const int planeWidth  = 128;
            const int planeHeight = 128;

            MeshDemo.CreateDeformedPlane(planeWidth, planeHeight,
                                         (int x, int y) =>
            {
                return(new Vector3(x - planeWidth / 2, 1 * MathF.Cos(x / 4f) * MathF.Sin(y / 4f), y - planeHeight / 2));
            }, new Vector3(1, 3, 1), BufferPool, out var planeMesh);
            Simulation.Statics.Add(new StaticDescription(
                                       new Vector3(0, -10, 0), BepuUtilities.Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), MathF.PI / 4),
                                       new CollidableDescription(Simulation.Shapes.Add(planeMesh), 0.1f)));

            int raySourceCount = 3;

            QuickList <QuickList <TestRay, Buffer <TestRay> >, Buffer <QuickList <TestRay, Buffer <TestRay> > > > .Create(BufferPool.SpecializeFor <QuickList <TestRay, Buffer <TestRay> > >(), raySourceCount, out raySources);

            raySources.Count = raySourceCount;

            //Spew rays all over the place, starting inside the shape cube.
            int     randomRayCount = 1 << 14;
            ref var randomRays     = ref raySources[0];
        public unsafe override void Initialize(ContentArchive content, Camera camera)
        {
            camera.Position = new Vector3(20, 10, 20);
            camera.Yaw      = MathF.PI;
            camera.Pitch    = 0;
            var masks = new BodyProperty <ulong>();

            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 = Quaternion.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.
            MeshDemo.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));