Exemplo n.º 1
0
        public unsafe override void Initialize(Camera camera)
        {
            camera.Position = new Vector3(-20, 10, -20);
            //camera.Yaw = MathHelper.Pi ;
            camera.Yaw = MathHelper.Pi * 3f / 4;
            //camera.Pitch = MathHelper.PiOver2 * 0.999f;
            Simulation = Simulation.Create(BufferPool, new TestCallbacks());

            var         shape = new Box(1f, 3f, 2f);
            BodyInertia localInertia;

            localInertia.InverseMass = 1f;
            shape.ComputeLocalInverseInertia(localInertia.InverseMass, out localInertia.InverseInertiaTensor);
            //capsuleInertia.InverseInertiaTensor = new Triangular3x3();
            var       shapeIndex     = Simulation.Shapes.Add(ref shape);
            const int width          = 8;
            const int height         = 16;
            const int length         = 8;
            var       latticeSpacing = 1.0f;
            var       latticeOffset  = 0;// -0.5f * width * latticeSpacing;

            SimulationSetup.BuildLattice(
                new RegularGridBuilder(new Vector3(1, 3, 2), new Vector3(latticeOffset, 1.5f, latticeOffset), localInertia, shapeIndex),
                new ConstraintlessLatticeBuilder(),
                width, height, length, Simulation, out var bodyHandles, out var constraintHandles);
            Simulation.PoseIntegrator.Gravity = new Vector3(0, -10, 0);
            Simulation.Deterministic          = false;
            //Simulation.Bodies.ActiveSet.Velocities[0].Linear = new Vector3(-2, 0, 0);
            //Simulation.Solver.IterationCount = 100;

            //var sphere = new Sphere(0.5f);
            //var sphereShapeIndex = Simulation.Shapes.Add(ref sphere);
            //BodyInertia sphereLocalInertia;
            //sphereLocalInertia.InverseMass = 1;
            //sphere.ComputeLocalInverseInertia(sphereLocalInertia.InverseMass, out sphereLocalInertia.InverseInertiaTensor);
            //for (int j = 0; j < 16; ++j)
            //{
            //    var bodyDescription = new BodyDescription
            //    {
            //        Pose = new RigidPose
            //        {
            //            Position = new Vector3(-16, 0.5f + j, 0),
            //            Orientation = BepuUtilities.Quaternion.Identity
            //        },
            //        LocalInertia = sphereLocalInertia,
            //        Collidable = new CollidableDescription
            //        {
            //            Continuity = new ContinuousDetectionSettings(),
            //            SpeculativeMargin = 0.04f,
            //            Shape = sphereShapeIndex
            //        },
            //        Activity = new BodyActivityDescription
            //        {
            //            SleepThreshold = -.1f,
            //            MinimumTimestepCountUnderThreshold = 32
            //        }
            //    };
            //    Simulation.Bodies.Add(ref bodyDescription);
            //}

            var         staticShape      = new Box(1, 1, 1);
            var         staticShapeIndex = Simulation.Shapes.Add(ref staticShape);
            const int   staticGridWidth  = 100;
            const float staticSpacing    = 1.2f;
            var         gridOffset       = -0.5f * staticGridWidth * staticSpacing;

            for (int i = 0; i < staticGridWidth; ++i)
            {
                for (int j = 0; j < staticGridWidth; ++j)
                {
                    var staticDescription = new StaticDescription
                    {
                        Collidable = new CollidableDescription
                        {
                            Continuity = new ContinuousDetectionSettings {
                                Mode = ContinuousDetectionMode.Discrete
                            },
                            Shape             = staticShapeIndex,
                            SpeculativeMargin = 0.1f
                        },
                        Pose = new RigidPose
                        {
                            Position = new Vector3(
                                1 + gridOffset + i * staticSpacing,
                                -0.5f,
                                0.5f + gridOffset + j * staticSpacing),
                            //Orientation = BepuUtilities.Quaternion.Identity
                            Orientation = BepuUtilities.Quaternion.CreateFromAxisAngle(Vector3.Normalize(new Vector3(1 + i, i * j % 10, -10 + -j)), (i ^ j) * 0.5f * (MathHelper.PiOver4))
                                          //Orientation = BepuUtilities.Quaternion.CreateFromAxisAngle(Vector3.Normalize(new Vector3(0, 0, 1)), MathHelper.Pi)
                        }
                    };
                    Simulation.Statics.Add(ref staticDescription);
                }
            }
        }
Exemplo n.º 2
0
        public unsafe override void Initialize(Camera camera)
        {
            camera.Position = new Vector3(-3f, 3, -3f);
            camera.Yaw      = MathHelper.Pi * 3f / 4;
            camera.Pitch    = MathHelper.Pi * 0.1f;
            Simulation      = Simulation.Create(BufferPool, new TestCallbacks());
            var       shape          = new Sphere(0.5f);
            var       shapeIndex     = Simulation.Shapes.Add(ref shape);
            const int width          = 16;
            const int height         = 16;
            const int length         = 16;
            var       latticeSpacing = 1.1f;
            var       latticeOffset  = -0.5f * width * latticeSpacing;

            SimulationSetup.BuildLattice(
                new RegularGridBuilder(new Vector3(latticeSpacing, 1.0f, latticeSpacing), new Vector3(latticeOffset, 100, latticeOffset), 1f / (shape.Radius * shape.Radius * 2 / 3), shapeIndex),
                new BallSocketConstraintBuilder(),
                width, height, length, Simulation, out var bodyHandles, out var constraintHandles);
            Simulation.PoseIntegrator.Gravity = new Vector3(0, -10, 0);
            Simulation.Deterministic          = true;

            var         staticShape              = new Sphere(4);
            var         staticShapeIndex         = Simulation.Shapes.Add(ref staticShape);
            const int   staticGridWidthInSpheres = 16;
            const float staticSpacing            = 6;

            for (int i = 0; i < staticGridWidthInSpheres; ++i)
            {
                for (int j = 0; j < staticGridWidthInSpheres; ++j)
                {
                    var staticDescription = new StaticDescription
                    {
                        Collidable = new CollidableDescription
                        {
                            Continuity = new ContinuousDetectionSettings {
                                Mode = ContinuousDetectionMode.Discrete
                            },
                            Shape             = staticShapeIndex,
                            SpeculativeMargin = 0.1f
                        },
                        Pose = new RigidPose
                        {
                            Position = new Vector3(
                                -staticGridWidthInSpheres * staticSpacing * 0.5f + i * staticSpacing,
                                -4,
                                -staticGridWidthInSpheres * staticSpacing * 0.5f + j * staticSpacing),
                            Orientation = BepuUtilities.Quaternion.Identity
                        }
                    };
                    Simulation.Statics.Add(ref staticDescription);
                }
            }

            //ref var velocity = ref Simulation.Bodies.Velocities[Simulation.Bodies.HandleToIndex[bodyHandles[width]]];
            //velocity.Linear = new Vector3(0.1f, 0, 0.1f);
            //velocity.Angular = new Vector3();

            //Simulation.Solver.IterationCount = 100;

            Console.WriteLine(Simulation.Solver.CountConstraints());
        }
Exemplo n.º 3
0
        public unsafe override void Initialize(Camera camera)
        {
            camera.Position = new Vector3(-3f, 3, -3f);
            camera.Yaw      = MathHelper.Pi * 3f / 4;
            camera.Pitch    = MathHelper.Pi * 0.1f;
            Simulation      = Simulation.Create(BufferPool, new TestCallbacks(),
                                                new SimulationAllocationSizes
            {
                Bodies = 1,
                ConstraintCountPerBodyEstimate = 1,
                Constraints             = 1,
                ConstraintsPerTypeBatch = 1,
                Islands       = 1,
                ShapesPerType = 1,
                Statics       = 1
            });

            var         shape = new Sphere(0.5f);
            BodyInertia sphereInertia;

            sphereInertia.InverseMass = 1;
            shape.ComputeLocalInverseInertia(sphereInertia.InverseMass, out sphereInertia.InverseInertiaTensor);
            var       shapeIndex     = Simulation.Shapes.Add(ref shape);
            const int width          = 16;
            const int height         = 16;
            const int length         = 16;
            var       latticeSpacing = 1.1f;
            var       latticeOffset  = -0.5f * width * latticeSpacing;

            SimulationSetup.BuildLattice(
                new RegularGridBuilder(new Vector3(latticeSpacing, 1.1f, latticeSpacing), new Vector3(latticeOffset, 10, latticeOffset), sphereInertia, shapeIndex),
                new ConstraintlessLatticeBuilder(),
                width, height, length, Simulation, out var bodyHandles, out var constraintHandles);
            Simulation.PoseIntegrator.Gravity = new Vector3(0, -1, 0);
            Simulation.Deterministic          = false;

            var         staticShape              = new Sphere(4);
            var         staticShapeIndex         = Simulation.Shapes.Add(ref staticShape);
            const int   staticGridWidthInSpheres = 100;
            const float staticSpacing            = 6;

            for (int i = 0; i < staticGridWidthInSpheres; ++i)
            {
                for (int j = 0; j < staticGridWidthInSpheres; ++j)
                {
                    var staticDescription = new StaticDescription
                    {
                        Collidable = new CollidableDescription
                        {
                            Continuity = new ContinuousDetectionSettings {
                                Mode = ContinuousDetectionMode.Discrete
                            },
                            Shape             = staticShapeIndex,
                            SpeculativeMargin = 0.1f
                        },
                        Pose = new RigidPose
                        {
                            Position = new Vector3(
                                -staticGridWidthInSpheres * staticSpacing * 0.5f + i * staticSpacing,
                                -4,
                                -staticGridWidthInSpheres * staticSpacing * 0.5f + j * staticSpacing),
                            Orientation = BepuUtilities.Quaternion.Identity
                        }
                    };
                    Simulation.Statics.Add(ref staticDescription);
                }
            }

            //ref var velocity = ref Simulation.Bodies.Velocities[Simulation.Bodies.HandleToIndex[bodyHandles[width]]];
            //velocity.Linear = new Vector3(0.1f, 0, 0.1f);
            //velocity.Angular = new Vector3();

            //Simulation.Solver.IterationCount = 100;
        }
Exemplo n.º 4
0
        public unsafe override void Initialize(ContentArchive content, Camera camera)
        {
            camera.Position = new Vector3(-3f, 3, -3f);
            camera.Yaw      = MathHelper.Pi * 3f / 4;
            camera.Pitch    = MathHelper.Pi * 0.1f;
            Simulation      = Simulation.Create(BufferPool, new DemoNarrowPhaseCallbacks(), new DemoPoseIntegratorCallbacks(new Vector3(0, -10, 0)),
                                                initialAllocationSizes: new SimulationAllocationSizes
            {
                Bodies = 1,
                ConstraintCountPerBodyEstimate = 1,
                Constraints             = 1,
                ConstraintsPerTypeBatch = 1,
                Islands       = 1,
                ShapesPerType = 1,
                Statics       = 1
            });

            var shape = new Sphere(0.5f);

            shape.ComputeInertia(1, out var sphereInertia);
            var       shapeIndex     = Simulation.Shapes.Add(shape);
            const int width          = 12;
            const int height         = 4;
            const int length         = 12;
            var       latticeSpacing = 5.1f;
            var       latticeOffset  = -0.5f * width * latticeSpacing;

            SimulationSetup.BuildLattice(
                new RegularGridBuilder(new Vector3(latticeSpacing, 1.5f, latticeSpacing), new Vector3(latticeOffset, 10, latticeOffset), sphereInertia, shapeIndex),
                new ConstraintlessLatticeBuilder(),
                width, height, length, Simulation, out var bodyHandles, out var constraintHandles);
            Simulation.Deterministic = false;

            var         staticShape              = new Sphere(4);
            var         staticShapeIndex         = Simulation.Shapes.Add(staticShape);
            const int   staticGridWidthInSpheres = 100;
            const float staticSpacing            = 6;

            for (int i = 0; i < staticGridWidthInSpheres; ++i)
            {
                for (int j = 0; j < staticGridWidthInSpheres; ++j)
                {
                    var staticDescription = new StaticDescription
                    {
                        Collidable = new CollidableDescription
                        {
                            Continuity = new ContinuousDetectionSettings {
                                Mode = ContinuousDetectionMode.Discrete
                            },
                            Shape             = staticShapeIndex,
                            SpeculativeMargin = 0.1f
                        },
                        Pose = new RigidPose
                        {
                            Position = new Vector3(
                                -staticGridWidthInSpheres * staticSpacing * 0.5f + i * staticSpacing,
                                -4,
                                -staticGridWidthInSpheres * staticSpacing * 0.5f + j * staticSpacing),
                            Orientation = Quaternion.Identity
                        }
                    };
                    Simulation.Statics.Add(staticDescription);
                }
            }
        }
Exemplo n.º 5
0
        public unsafe override void Initialize(Camera camera)
        {
            camera.Position = new Vector3(-10, 5, -10);
            //camera.Yaw = MathHelper.Pi ;
            camera.Yaw = MathHelper.Pi * 3f / 4;
            //camera.Pitch = MathHelper.Pi * 0.1f;
            Simulation = Simulation.Create(BufferPool, new TestCallbacks());

            var         shape = new Capsule(.5f, 3.5f);
            BodyInertia localInertia;

            localInertia.InverseMass = 1f;
            shape.ComputeLocalInverseInertia(localInertia.InverseMass, out localInertia.InverseInertiaTensor);
            //capsuleInertia.InverseInertiaTensor = new Triangular3x3();
            var       shapeIndex     = Simulation.Shapes.Add(ref shape);
            const int width          = 4;
            const int height         = 4;
            const int length         = 4;
            var       latticeSpacing = 1.1f;
            var       latticeOffset  = 0;// -0.5f * width * latticeSpacing;

            SimulationSetup.BuildLattice(
                new RegularGridBuilder(new Vector3(latticeSpacing, 2.1f, latticeSpacing), new Vector3(latticeOffset, 7, latticeOffset), localInertia, shapeIndex),
                new ConstraintlessLatticeBuilder(),
                width, height, length, Simulation, out var bodyHandles, out var constraintHandles);
            Simulation.PoseIntegrator.Gravity = new Vector3(0, -1, 0);
            Simulation.Deterministic          = false;

            //var bodyDescription = new BodyDescription
            //{
            //    Pose = new RigidPose
            //    {
            //        Position = new Vector3(0, 5, -0.5f),
            //        //Orientation = BepuUtilities.Quaternion.Identity
            //        Orientation = BepuUtilities.Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), MathHelper.PiOver2)
            //    },
            //    LocalInertia = new BodyInertia(),
            //    Collidable = new CollidableDescription
            //    {
            //        Continuity = new ContinuousDetectionSettings(),
            //        SpeculativeMargin = 0.1f,
            //        Shape = shapeIndex
            //    },
            //    Activity = new BodyActivityDescription
            //    {
            //        SleepThreshold = -.1f,
            //        MinimumTimestepCountUnderThreshold = 32
            //    },
            //    //Velocity = new BodyVelocity { Angular = new Vector3(0, (rowIndex % 2 - 0.5f) * 20, 0) }
            //};
            //Simulation.Bodies.Add(ref bodyDescription);

            var         staticShape      = new Sphere(4f);
            var         staticShapeIndex = Simulation.Shapes.Add(ref staticShape);
            const int   staticGridWidth  = 64;
            const float staticSpacing    = 6;
            var         gridOffset       = -0.5f * staticGridWidth * staticSpacing;

            for (int i = 0; i < staticGridWidth; ++i)
            {
                for (int j = 0; j < staticGridWidth; ++j)
                {
                    var staticDescription = new StaticDescription
                    {
                        Collidable = new CollidableDescription
                        {
                            Continuity = new ContinuousDetectionSettings {
                                Mode = ContinuousDetectionMode.Discrete
                            },
                            Shape             = staticShapeIndex,
                            SpeculativeMargin = 0.1f
                        },
                        Pose = new RigidPose
                        {
                            Position = new Vector3(
                                gridOffset + i * staticSpacing,
                                -4,
                                gridOffset + j * staticSpacing),
                            Orientation = BepuUtilities.Quaternion.CreateFromAxisAngle(Vector3.Normalize(new Vector3(1, 0, 1)), MathHelper.PiOver4)
                        }
                    };
                    Simulation.Statics.Add(ref staticDescription);
                }
            }
        }