Exemple #1
0
        private PhyObject CreateVanilla(ObjectState state, CollidableDescription collidableDescription, BodyInertia bodyInertia)
        {
            PhyObject phy;

            if (state.mass != 0)
            {
                BodyDescription boxDescription = BodyDescription.CreateDynamic(state.position, bodyInertia,
                                                                               collidableDescription,
                                                                               new BodyActivityDescription(0.01f));

                boxDescription.Pose = new RigidPose(state.position, state.quaternion);
                var bodyHandle = Simulation.Bodies.Add(boxDescription);



                phy = SetUpPhyObject(bodyHandle, state);
                objectsHandlers.Add(bodyHandle, phy);
            }
            else
            {
                StaticDescription description = new StaticDescription(state.position, state.quaternion, collidableDescription);
                StaticHandle      handle      = Simulation.Statics.Add(description);
                //collidableMaterials.Allocate(handle) = new SimpleMaterial { FrictionCoefficient = 1, MaximumRecoveryVelocity = float.MaxValue, SpringSettings = new SpringSettings(1f, 1f) };
                phy = SetUpPhyObject(handle, state);
                staticObjectsHandlers.Add(handle, (StaticPhyObject)phy);
                //objectsHandlers.Add(handle,phy);
            }


            return(phy);
        }
 public MovingPlatform(CollidableDescription collidable, double timeOffset, float goalSatisfactionTime, Simulation simulation, Func <double, RigidPose> poseCreator)
 {
     PoseCreator = poseCreator;
     BodyHandle  = simulation.Bodies.Add(BodyDescription.CreateKinematic(poseCreator(timeOffset), collidable, new BodyActivityDescription(-1)));
     InverseGoalSatisfactionTime = 1f / goalSatisfactionTime;
     TimeOffset = timeOffset;
 }
Exemple #3
0
 internal override void CreateAndAddCollidableDescription(
     BepuPhysicsComponent physicsComponent,
     BepuSimulation xenkoSimulation,
     out TypedIndex shapeTypeIndex,
     out CollidableDescription collidableDescription)
 {
     CreateAndAddCollidableDescription <Sphere>(
         physicsComponent, xenkoSimulation, out shapeTypeIndex, out collidableDescription);
 }
Exemple #4
0
 static void ComputeCollisions(CollisionTaskRegistry registry, Shapes shapes, BufferPool pool,
     ref Manifolds manifolds, CollidableDescription a, CollidableDescription b, ref Buffer<RigidPose> posesA, ref Buffer<RigidPose> posesB, Buffer<int> remapIndices, int pairCount)
 {
     var batcher = new CollisionBatcher<BatcherCallbacks>(pool, shapes, registry, 1 / 60f, new BatcherCallbacks { Pool = pool, Manifolds = manifolds });
     for (int i = 0; i < pairCount; ++i)
     {
         var index = remapIndices[i];
         ref var poseA = ref posesA[index];
         ref var poseB = ref posesB[index];
Exemple #5
0
        protected override void OnAttach()
        {
            //this will set all the properties in the native side object
            base.OnAttach();

            Entity.Transform.UpdateWorldMatrix();
            Entity.Transform.GetWorldTransformation(out var entityPosition, out var entityRotation, out var scale);

            var position = new System.Numerics.Vector3(entityPosition.X, entityPosition.Y, entityPosition.Z);
            var rotation = new System.Numerics.Quaternion(entityRotation.X, entityRotation.Y,
                                                          entityRotation.Z, entityRotation.W);

            var collidableDescription = new CollidableDescription(ColliderShape.InternalShapeIndex, SpeculativeMargin);

            staticHandle = Simulation.AddStatic(new StaticDescription(position, rotation, collidableDescription));

            Simulation.physicsBodySettings.Allocate(staticHandle) = new SimplePhysicsBodySettings
            {
                CustomDamping = false,
                CustomGravity = false,
            };

            Simulation.physicsMaterials.Allocate(staticHandle) = new SimplePhysicsMaterial
            {
                FrictionCoefficient     = 1f,
                MaximumRecoveryVelocity = 2f,
                SpringSettings          = new BepuPhysics.Constraints.SpringSettings(30, 1)
            };

            Simulation.physicsTags.Allocate(staticHandle) = new SimplePhysicsTag
            {
                ComponentId = Id
            };

            Simulation.collisionProps.Allocate(staticHandle) = new CollisionProperty
            {
                GenerateOverlapEvents = GenerateOverlapEvents,

                CollisionEnabled = CollisionPresets.CollisionEnabled,
                ObjectType       = CollisionPresets.ObjectType,

                Visibility = CollisionPresets.CollisionResponses.TraceResponse.Visibility,
                Camera     = CollisionPresets.CollisionResponses.TraceResponse.Camera,

                WorldStatic  = CollisionPresets.CollisionResponses.ObjectResponse.WorldStatic,
                WorldDynamic = CollisionPresets.CollisionResponses.ObjectResponse.WorldDynamic,
                Pawn         = CollisionPresets.CollisionResponses.ObjectResponse.Pawn,
                PhysicsBody  = CollisionPresets.CollisionResponses.ObjectResponse.PhysicsBody,
            };

            if (GenerateOverlapEvents)
            {
                Simulation.RegisterOverlapListener(staticHandle);
            }
        }
Exemple #6
0
        private PhyObject CreateSphere(SphereState state)
        {
            var sphere = new Sphere(state.radius);
            CollidableDescription collidableDescription = new CollidableDescription(Simulation.Shapes.Add(sphere), 0.1f);
            BodyInertia           bodyInertia;

            sphere.ComputeInertia(state.mass, out bodyInertia);

            var phy = CreateVanilla(state, collidableDescription, bodyInertia);

            return(phy);
        }
Exemple #7
0
        //Default constructor
        public EnemyEntity(Vector3 SpawnLocation)
        {
            //Store the entities default location then add a new physics collider into the world physics simulation
            DefaultLocation           = SpawnLocation;
            EntityColliderShape       = new Cylinder(0.6f, 1.7f);
            EntityColliderDescription = new CollidableDescription(Program.World.World.Shapes.Add(EntityColliderShape), 0.25f);
            EntityColliderShape.ComputeInertia(1, out var Inertia);
            Program.World.World.Bodies.Add(BodyDescription.CreateDynamic(new RigidPose(SpawnLocation, Quaternion.Identity), Inertia, EntityColliderDescription, new BodyActivityDescription(0.01f)));

            //Pass it to the entity manager to be stored with all the others and set the enemies type value
            EntityManager.AddEntity(this);
            this.Type = "Skeleton Warrior";
        }
Exemple #8
0
        private PhyObject CreateBox(BoxState state)
        {
            var box = new Box(state.halfSize.X, state.halfSize.Y, state.halfSize.Z);

            CollidableDescription collidableDescription = new CollidableDescription(Simulation.Shapes.Add(box), 0.1f);
            BodyInertia           bodyInertia;

            box.ComputeInertia(state.mass, out bodyInertia);


            var phy = CreateVanilla(state, collidableDescription, bodyInertia);

            return(phy);
        }
Exemple #9
0
        private PhyObject CreateMesh(BoxState state)
        {
            LoadModel(server.GetMesh(state.mesh), out var mesh, state.halfSize);

            //fs.Close();

            CollidableDescription collidableDescription = new CollidableDescription(Simulation.Shapes.Add(mesh), 0.1f);

            mesh.ComputeClosedInertia(state.mass, out var bodyInertia);

            var phy = CreateVanilla(state, collidableDescription, bodyInertia);

            objects.Add(state.uID, phy);

            return(phy);
        }
Exemple #10
0
        //Initialize the characters body in the physics simulation
        public void InitializeBody(Simulation World, Vector3 Location)
        {
            if (BodyActive)
            {
                return;
            }
            BodyActive            = true;
            BodyShape             = new Capsule(0.5f, 2);
            BodyIndex             = World.Shapes.Add(BodyShape);
            CollidableDescription = new CollidableDescription(BodyIndex, 0.1f);
            BodyShape.ComputeInertia(1, out var Inertia);
            Vector3 SpawnLocation = new Vector3(Location.X, Location.Y + 1.5f, Location.Z);

            BodyPose            = new RigidPose(SpawnLocation, Quaternion.Identity);
            ActivityDescription = new BodyActivityDescription(0.01f);
            BodyDescription     = BodyDescription.CreateKinematic(BodyPose, CollidableDescription, ActivityDescription);
            BodyHandle          = World.Bodies.Add(BodyDescription);
        }
Exemple #11
0
        public static TriggerBox Create(PhysicsSimulation simulation, Vector3 position, Quaternion rotation, Vector3 size)
        {
            var shape = new Box(size.X, size.Y, size.Z);

            var index = simulation.Simulation.Shapes.Add(shape);

            var collidable = new CollidableDescription(index, 0.01f);

            var descriptor = new StaticDescription(position, rotation, collidable);

            var handle = simulation.Simulation.Statics.Add(descriptor);

            var obj = new TriggerBox(simulation, handle);

            simulation.Register(obj);

            return(obj);
        }
        public override void Initialize(ContentArchive content, Camera camera)
        {
            camera.Position = new Vector3(0, 4, -6);
            camera.Yaw      = MathHelper.Pi;

            Simulation = Simulation.Create(BufferPool, new IndexReportingNarrowPhaseCallbacks(), new DemoPoseIntegratorCallbacks(new Vector3(0, 0f, 0)));

            var builder = new CompoundBuilder(BufferPool, Simulation.Shapes, 4);

            builder.Add(new Sphere(0.5f), new RigidPose(new Vector3(-1, 0, 0)), 1);
            builder.Add(new Capsule(0.5f, 1f), new RigidPose(new Vector3(0, 0, 0)), 1);
            builder.Add(new Box(1f, 1f, 1f), new RigidPose(new Vector3(1, 0, 0)), 1);
            builder.BuildDynamicCompound(out var children, out var inertia, out var center);

            var compoundCollidable = new CollidableDescription(Simulation.Shapes.Add(new Compound(children)), 0.1f);

            Simulation.Bodies.Add(BodyDescription.CreateDynamic(new Vector3(0, 2, 0), inertia, compoundCollidable, new BodyActivityDescription(0.01f)));
            Simulation.Bodies.Add(BodyDescription.CreateDynamic(new Vector3(0, 4, 0), inertia, compoundCollidable, new BodyActivityDescription(0.01f)));

            Simulation.Statics.Add(new StaticDescription(new Vector3(), new CollidableDescription(Simulation.Shapes.Add(new Box(100, 1, 100)), 0.1f)));
        }
Exemple #13
0
        public static CylinderBody Create(PhysicsSimulation simulation, Vector3 position, Quaternion rotation, Vector2 size)
        {
            var shape = new Cylinder(size.X, size.Y);

            shape.ComputeInertia(1, out var inertia);

            var index = simulation.Simulation.Shapes.Add(shape);

            var collidable = new CollidableDescription(index, 0.1f);

            var activity = new BodyActivityDescription(0);

            var pose = new RigidPose(position, rotation);

            var descriptor = BodyDescription.CreateDynamic(pose, inertia, collidable, activity);

            var handle = simulation.Simulation.Bodies.Add(descriptor);

            var obj = new CylinderBody(simulation, handle);

            simulation.Register(obj);

            return(obj);
        }
        public unsafe override void Initialize(ContentArchive content, Camera camera)
        {
            camera.Position = new Vector3(25, 4, 40);
            camera.Yaw      = 0;
            Simulation      = Simulation.Create(BufferPool, new TestCallbacks());

            Simulation.PoseIntegrator.Gravity = new Vector3(0, -10, 0);

            var shapeA      = new Box(.75f, 1, .5f);
            var shapeIndexA = Simulation.Shapes.Add(shapeA);
            var collidableA = new CollidableDescription(shapeIndexA, 0.1f);
            var shapeB      = new Box(.75f, 1, .5f);
            var shapeIndexB = Simulation.Shapes.Add(shapeB);
            var collidableB = new CollidableDescription(shapeIndexB, 0.1f);
            var activity    = new BodyActivityDescription(0.01f);

            shapeA.ComputeInertia(1, out var inertiaA);
            shapeA.ComputeInertia(1, out var inertiaB);
            var nextX = -10f;

            {
                var x = GetNextPosition(ref nextX);
                var a = Simulation.Bodies.Add(BodyDescription.CreateDynamic(new Vector3(x, 3, 0), inertiaA, collidableA, activity));
                var b = Simulation.Bodies.Add(BodyDescription.CreateDynamic(new Vector3(x, 5, 0), inertiaB, collidableB, activity));
                Simulation.Solver.Add(a, b, new BallSocket {
                    LocalOffsetA = new Vector3(0, 1, 0), LocalOffsetB = new Vector3(0, -1, 0), SpringSettings = new SpringSettings(30, 1)
                });
            }
            {
                var x = GetNextPosition(ref nextX);
                var a = Simulation.Bodies.Add(BodyDescription.CreateKinematic(new Vector3(x, 3, 0), collidableA, activity));
                var b = Simulation.Bodies.Add(BodyDescription.CreateDynamic(new Vector3(x, 5, 0), inertiaB, collidableB, activity));
                Simulation.Solver.Add(a, b, new BallSocket {
                    LocalOffsetA = new Vector3(0, 1, 0), LocalOffsetB = new Vector3(0, -1, 0), SpringSettings = new SpringSettings(30, 1)
                });
                Simulation.Solver.Add(a, b, new AngularHinge {
                    LocalHingeAxisA = new Vector3(0, 1, 0), LocalHingeAxisB = new Vector3(0, 1, 0), SpringSettings = new SpringSettings(30, 1)
                });
            }
            {
                var x = GetNextPosition(ref nextX);
                var a = Simulation.Bodies.Add(BodyDescription.CreateKinematic(new Vector3(x, 3, 0), collidableA, activity));
                var b = Simulation.Bodies.Add(BodyDescription.CreateDynamic(new Vector3(x, 5, 0), inertiaB, collidableB, activity));
                Simulation.Solver.Add(a, b, new Hinge
                {
                    LocalOffsetA    = new Vector3(0, 1, 0),
                    LocalHingeAxisA = new Vector3(0, 1, 0),
                    LocalOffsetB    = new Vector3(0, -1, 0),
                    LocalHingeAxisB = new Vector3(0, 1, 0),
                    SpringSettings  = new SpringSettings(30, 1)
                });
            }
            {
                var x = GetNextPosition(ref nextX);
                var a = Simulation.Bodies.Add(BodyDescription.CreateKinematic(new Vector3(x, 3, 0), collidableA, activity));
                var b = Simulation.Bodies.Add(BodyDescription.CreateDynamic(new Vector3(x, 5, 0), inertiaB, collidableB, activity));
                Simulation.Solver.Add(a, b, new BallSocket {
                    LocalOffsetA = new Vector3(0, 1, 0), LocalOffsetB = new Vector3(0, -1, 0), SpringSettings = new SpringSettings(30, 1)
                });
                Simulation.Solver.Add(a, b, new AngularSwivelHinge {
                    LocalSwivelAxisA = new Vector3(1, 0, 0), LocalHingeAxisB = new Vector3(0, 1, 0), SpringSettings = new SpringSettings(30, 1)
                });
                Simulation.Solver.Add(a, b, new SwingLimit {
                    AxisLocalA = new Vector3(0, 1, 0), AxisLocalB = new Vector3(0, 1, 0), MaximumSwingAngle = MathHelper.PiOver2, SpringSettings = new SpringSettings(30, 1)
                });
            }
            {
                var x = GetNextPosition(ref nextX);
                var a = Simulation.Bodies.Add(BodyDescription.CreateKinematic(new Vector3(x, 3, 0), collidableA, activity));
                var b = Simulation.Bodies.Add(BodyDescription.CreateDynamic(new Vector3(x, 5, 0), inertiaB, collidableB, activity));
                Simulation.Solver.Add(a, b, new SwivelHinge
                {
                    LocalOffsetA     = new Vector3(0, 1, 0),
                    LocalSwivelAxisA = new Vector3(1, 0, 0),
                    LocalOffsetB     = new Vector3(0, -1, 0),
                    LocalHingeAxisB  = new Vector3(0, 1, 0),
                    SpringSettings   = new SpringSettings(30, 1)
                });
                Simulation.Solver.Add(a, b, new SwingLimit {
                    AxisLocalA = new Vector3(0, 1, 0), AxisLocalB = new Vector3(0, 1, 0), MaximumSwingAngle = MathHelper.PiOver2, SpringSettings = new SpringSettings(30, 1)
                });
            }
            {
                var x = GetNextPosition(ref nextX);
                var a = Simulation.Bodies.Add(BodyDescription.CreateKinematic(new Vector3(x, 3, 0), collidableA, activity));
                var b = Simulation.Bodies.Add(BodyDescription.CreateDynamic(new Vector3(x, 5, 0), inertiaB, collidableB, activity));
                Simulation.Solver.Add(a, b, new BallSocket {
                    LocalOffsetA = new Vector3(0, 1, 0), LocalOffsetB = new Vector3(0, -1, 0), SpringSettings = new SpringSettings(30, 1)
                });
                Simulation.Solver.Add(a, b, new TwistServo
                {
                    LocalBasisA    = RagdollDemo.CreateBasis(new Vector3(0, 1, 0), new Vector3(1, 0, 0)),
                    LocalBasisB    = RagdollDemo.CreateBasis(new Vector3(0, 1, 0), new Vector3(1, 0, 0)),
                    TargetAngle    = MathHelper.PiOver4,
                    SpringSettings = new SpringSettings(30, 1),
                    ServoSettings  = new ServoSettings(float.MaxValue, 0, float.MaxValue)
                });
            }
            {
                var x = GetNextPosition(ref nextX);
                var a = Simulation.Bodies.Add(BodyDescription.CreateKinematic(new Vector3(x, 3, 0), collidableA, activity));
                var b = Simulation.Bodies.Add(BodyDescription.CreateDynamic(new Vector3(x, 5, 0), inertiaB, collidableB, activity));
                Simulation.Solver.Add(a, b, new BallSocket {
                    LocalOffsetA = new Vector3(0, 1, 0), LocalOffsetB = new Vector3(0, -1, 0), SpringSettings = new SpringSettings(30, 1)
                });
                Simulation.Solver.Add(a, b, new TwistLimit
                {
                    LocalBasisA    = RagdollDemo.CreateBasis(new Vector3(0, 1, 0), new Vector3(1, 0, 0)),
                    LocalBasisB    = RagdollDemo.CreateBasis(new Vector3(0, 1, 0), new Vector3(1, 0, 0)),
                    MinimumAngle   = MathHelper.Pi * -0.5f,
                    MaximumAngle   = MathHelper.Pi * 0.95f,
                    SpringSettings = new SpringSettings(30, 1),
                });
                Simulation.Solver.Add(a, b, new AngularHinge {
                    LocalHingeAxisA = new Vector3(0, 1, 0), LocalHingeAxisB = new Vector3(0, 1, 0), SpringSettings = new SpringSettings(30, 1)
                });
            }
            {
                var x = GetNextPosition(ref nextX);
                var a = Simulation.Bodies.Add(BodyDescription.CreateKinematic(new Vector3(x, 3, 0), collidableA, activity));
                var b = Simulation.Bodies.Add(BodyDescription.CreateDynamic(new Vector3(x, 5, 0), inertiaB, collidableB, activity));
                Simulation.Solver.Add(a, b, new BallSocket {
                    LocalOffsetA = new Vector3(0, 1, 0), LocalOffsetB = new Vector3(0, -1, 0), SpringSettings = new SpringSettings(30, 1)
                });
                Simulation.Solver.Add(a, b, new TwistMotor
                {
                    LocalAxisA     = new Vector3(0, 1, 0),
                    LocalAxisB     = new Vector3(0, 1, 0),
                    TargetVelocity = MathHelper.Pi * 2,
                    Settings       = new MotorSettings(float.MaxValue, 0.1f)
                });
                Simulation.Solver.Add(a, b, new AngularHinge {
                    LocalHingeAxisA = new Vector3(0, 1, 0), LocalHingeAxisB = new Vector3(0, 1, 0), SpringSettings = new SpringSettings(30, 1)
                });
            }
            {
                var x = GetNextPosition(ref nextX);
                var a = Simulation.Bodies.Add(BodyDescription.CreateKinematic(new Vector3(x, 3, 0), collidableA, activity));
                var b = Simulation.Bodies.Add(BodyDescription.CreateDynamic(new Vector3(x, 5, 0), inertiaB, collidableB, activity));
                Simulation.Solver.Add(a, b, new BallSocket {
                    LocalOffsetA = new Vector3(0, 1, 0), LocalOffsetB = new Vector3(0, -1, 0), SpringSettings = new SpringSettings(30, 1)
                });
                Simulation.Solver.Add(a, b, new AngularServo
                {
                    TargetRelativeRotationLocalA = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), MathHelper.PiOver2),
                    ServoSettings  = new ServoSettings(float.MaxValue, 0, 12f),
                    SpringSettings = new SpringSettings(30, 1)
                });
            }
            {
                var x = GetNextPosition(ref nextX);
                var a = Simulation.Bodies.Add(BodyDescription.CreateDynamic(new Vector3(x, 3, 0), inertiaA, collidableA, activity));
                var b = Simulation.Bodies.Add(BodyDescription.CreateDynamic(new Vector3(x, 5, 0), inertiaB, collidableB, activity));
                Simulation.Solver.Add(a, b, new BallSocket {
                    LocalOffsetA = new Vector3(0, 1, 0), LocalOffsetB = new Vector3(0, -1, 0), SpringSettings = new SpringSettings(30, 1)
                });
                Simulation.Solver.Add(a, b, new AngularMotor {
                    TargetVelocityLocalA = new Vector3(0, 1, 0), Settings = new MotorSettings(15, 0.0001f)
                });
            }
            {
                var x            = GetNextPosition(ref nextX);
                var aDescription = BodyDescription.CreateDynamic(new Vector3(x, 3, 0), inertiaA, collidableA, activity);
                var bDescription = BodyDescription.CreateDynamic(new Vector3(x, 5, 0), inertiaB, collidableB, activity);
                //aDescription.Velocity.Angular = new Vector3(0, 0, 5);
                var a = Simulation.Bodies.Add(aDescription);
                var b = Simulation.Bodies.Add(bDescription);
                Simulation.Solver.Add(a, b, new Weld {
                    LocalOffset = new Vector3(0, 2, 0), LocalOrientation = Quaternion.Identity, SpringSettings = new SpringSettings(30, 1)
                });
            }
            {
                var x      = GetNextPosition(ref nextX);
                var sphere = new Sphere(0.125f);
                //Treat each vertex as a point mass that cannot rotate.
                var sphereInertia = new BodyInertia {
                    InverseMass = 1
                };
                var sphereCollidable = new CollidableDescription(Simulation.Shapes.Add(sphere), 0.1f);
                var a                   = new Vector3(x, 3, 0);
                var b                   = new Vector3(x, 4, 0);
                var c                   = new Vector3(x, 3, 1);
                var d                   = new Vector3(x + 1, 3, 0);
                var aDescription        = BodyDescription.CreateDynamic(a, sphereInertia, sphereCollidable, activity);
                var bDescription        = BodyDescription.CreateDynamic(b, sphereInertia, sphereCollidable, activity);
                var cDescription        = BodyDescription.CreateDynamic(c, sphereInertia, sphereCollidable, activity);
                var dDescription        = BodyDescription.CreateDynamic(d, sphereInertia, sphereCollidable, activity);
                var aHandle             = Simulation.Bodies.Add(aDescription);
                var bHandle             = Simulation.Bodies.Add(bDescription);
                var cHandle             = Simulation.Bodies.Add(cDescription);
                var dHandle             = Simulation.Bodies.Add(dDescription);
                var distanceSpringiness = new SpringSettings(3f, 1);
                Simulation.Solver.Add(aHandle, bHandle, new CenterDistanceConstraint(Vector3.Distance(a, b), distanceSpringiness));
                Simulation.Solver.Add(aHandle, cHandle, new CenterDistanceConstraint(Vector3.Distance(a, c), distanceSpringiness));
                Simulation.Solver.Add(aHandle, dHandle, new CenterDistanceConstraint(Vector3.Distance(a, d), distanceSpringiness));
                Simulation.Solver.Add(bHandle, cHandle, new CenterDistanceConstraint(Vector3.Distance(b, c), distanceSpringiness));
                Simulation.Solver.Add(bHandle, dHandle, new CenterDistanceConstraint(Vector3.Distance(b, d), distanceSpringiness));
                Simulation.Solver.Add(cHandle, dHandle, new CenterDistanceConstraint(Vector3.Distance(c, d), distanceSpringiness));
                Simulation.Solver.Add(aHandle, bHandle, cHandle, dHandle, new VolumeConstraint(a, b, c, d, new SpringSettings(30, 1)));
            }
            {
                var x            = GetNextPosition(ref nextX);
                var aDescription = BodyDescription.CreateDynamic(new Vector3(x, 3, 0), inertiaA, collidableA, activity);
                var bDescription = BodyDescription.CreateDynamic(new Vector3(x, 6, 0), inertiaB, collidableB, activity);
                var a            = Simulation.Bodies.Add(aDescription);
                var b            = Simulation.Bodies.Add(bDescription);
                Simulation.Solver.Add(a, b, new DistanceServo(new Vector3(0, 0.55f, 0), new Vector3(0, -0.55f, 0), 1.9f, new SpringSettings(30, 1), ServoSettings.Default));
            }
            {
                var x            = GetNextPosition(ref nextX);
                var aDescription = BodyDescription.CreateDynamic(new Vector3(x, 3, 0), inertiaA, collidableA, activity);
                var bDescription = BodyDescription.CreateDynamic(new Vector3(x, 6, 0), inertiaB, collidableB, activity);
                var a            = Simulation.Bodies.Add(aDescription);
                var b            = Simulation.Bodies.Add(bDescription);
                Simulation.Solver.Add(a, b, new DistanceLimit(new Vector3(0, 0.55f, 0), new Vector3(0, -0.55f, 0), 1f, 3, new SpringSettings(30, 1)));
            }
            {
                var x      = GetNextPosition(ref nextX);
                var sphere = new Sphere(0.125f);
                //Treat each vertex as a point mass that cannot rotate.
                var sphereInertia = new BodyInertia {
                    InverseMass = 1
                };
                var sphereCollidable = new CollidableDescription(Simulation.Shapes.Add(sphere), 0.1f);
                var a                   = new Vector3(x, 3, 0);
                var b                   = new Vector3(x, 4, 0);
                var c                   = new Vector3(x + 1, 3, 0);
                var aDescription        = BodyDescription.CreateDynamic(a, sphereInertia, sphereCollidable, activity);
                var bDescription        = BodyDescription.CreateDynamic(b, sphereInertia, sphereCollidable, activity);
                var cDescription        = BodyDescription.CreateDynamic(c, sphereInertia, sphereCollidable, activity);
                var aHandle             = Simulation.Bodies.Add(aDescription);
                var bHandle             = Simulation.Bodies.Add(bDescription);
                var cHandle             = Simulation.Bodies.Add(cDescription);
                var distanceSpringiness = new SpringSettings(3f, 1);
                Simulation.Solver.Add(aHandle, bHandle, new CenterDistanceConstraint(Vector3.Distance(a, b), distanceSpringiness));
                Simulation.Solver.Add(aHandle, cHandle, new CenterDistanceConstraint(Vector3.Distance(a, c), distanceSpringiness));
                Simulation.Solver.Add(bHandle, cHandle, new CenterDistanceConstraint(Vector3.Distance(b, c), distanceSpringiness));
                Simulation.Solver.Add(aHandle, bHandle, cHandle, new AreaConstraint(a, b, c, new SpringSettings(30, 1)));
            }
            {
                var x            = GetNextPosition(ref nextX);
                var aDescription = BodyDescription.CreateDynamic(new Vector3(x, 3, 0), default, collidableA, activity);
        public unsafe override void Initialize(ContentArchive content, Camera camera)
        {
            camera.Position = new Vector3(0, 25, 80);
            camera.Yaw      = 0;
            camera.Pitch    = 0;
            timestepper     = new SubsteppingTimestepper(8);
            Simulation      = Simulation.Create(BufferPool, new DemoNarrowPhaseCallbacks()
            {
                ContactSpringiness = new SpringSettings(120, 120)
            }, new DemoPoseIntegratorCallbacks(new Vector3(0, -10, 0)), timestepper, 8);

            rolloverInfo = new RolloverInfo();
            {
                //We'll create a 0 level arm rope like the one from the RopeStabilityDemo. No skip constraints, though- and the mass ratio will be 1000:1 instead of 100:1!
                var         startLocation  = new Vector3(15, 40, 0);
                const float bodySpacing    = 0.3f;
                const float bodyRadius     = 0.5f;
                var         springSettings = new SpringSettings(240, 480);
                var         bodyHandles    = RopeStabilityDemo.BuildRope(Simulation, startLocation, 12, bodyRadius, bodySpacing, 0, 1, 0, springSettings);

                var bigWreckingBall = new Sphere(5);
                bigWreckingBall.ComputeInertia(1000, out var bigWreckingBallInertia);

                RopeStabilityDemo.AttachWreckingBall(Simulation, bodyHandles, bodyRadius, bodySpacing, 0, bigWreckingBall.Radius, bigWreckingBallInertia, Simulation.Shapes.Add(bigWreckingBall), springSettings);
                rolloverInfo.Add(startLocation + new Vector3(0, 2, 0), "1000:1 mass ratio");
            }

            {
                //Stack with a heavy block on top. Note that the contact springiness we chose in the DemoNarrowPhaseCallbacks above is important to making the stack resist the weight of the top block.
                //It's also the reason why we need higher substeps- 120hz frequency is too high for 60hz solving! Watch what happens when you drop the substep count to 3.
                //(Note that the demos timestep frequency is 60hz, so 4 substeps is a 240hz solve rate- twice the 120hz contact frequency.)
                var boxShape = new Box(4, 0.5f, 6f);
                boxShape.ComputeInertia(1, out var boxInertia);
                //Note that sleeping is disabled with a negative velocity threshold. We want to watch the stack as we change simulation settings; if it's inactive, it won't respond!
                var boxDescription = BodyDescription.CreateDynamic(new Vector3(), boxInertia, new CollidableDescription(Simulation.Shapes.Add(boxShape), 0.1f), new BodyActivityDescription(-1f));
                for (int i = 0; i < 20; ++i)
                {
                    boxDescription.Pose = new RigidPose(new Vector3(0, 0.5f + boxShape.Height * (i + 0.5f), 0), QuaternionEx.CreateFromAxisAngle(Vector3.UnitY, MathF.PI * 0.05f * i));
                    Simulation.Bodies.Add(boxDescription);
                }
                var topBlockShape = new Box(8, 2, 8);
                topBlockShape.ComputeInertia(200, out var topBlockInertia);
                Simulation.Bodies.Add(
                    BodyDescription.CreateDynamic(boxDescription.Pose.Position + new Vector3(0, boxShape.HalfHeight + 1f, 0), topBlockInertia,
                                                  new CollidableDescription(Simulation.Shapes.Add(topBlockShape), 0.1f), new BodyActivityDescription(-1f)));

                rolloverInfo.Add(boxDescription.Pose.Position + new Vector3(0, 4, 0), "200:1 mass ratio");
            }

            {
                //Now a weird rotating multi-arm thing. Long constraint sequences with high leverages under stress are a really tough problem for iterative velocity solvers.
                //(Fortunately, all 5 degrees of freedom of each hinge constraint are solved analytically, so the convergence issues aren't quite as bad as they could be.)
                var basePosition  = new Vector3(-20, 20, 0);
                var boxShape      = new Box(0.5f, 0.5f, 3f);
                var boxCollidable = new CollidableDescription(Simulation.Shapes.Add(boxShape), 0.1f);
                boxShape.ComputeInertia(1, out var boxInertia);
                var linkDescription = BodyDescription.CreateDynamic(new Vector3(), boxInertia, boxCollidable, new BodyActivityDescription(0.01f));


                for (int chainIndex = 0; chainIndex < 4; ++chainIndex)
                {
                    linkDescription.Pose.Position = basePosition + new Vector3(0, 0, chainIndex * 15);
                    var previousLinkHandle = Simulation.Bodies.Add(BodyDescription.CreateKinematic(linkDescription.Pose.Position, boxCollidable, new BodyActivityDescription(0.01f)));
                    for (int linkIndex = 0; linkIndex < 8; ++linkIndex)
                    {
                        var previousPosition = linkDescription.Pose.Position;
                        var offset           = new Vector3(boxShape.Width * 1.05f, 0, boxShape.Length - boxShape.Width);
                        linkDescription.Pose.Position += offset;
                        var linkHandle = Simulation.Bodies.Add(linkDescription);
                        Simulation.Solver.Add(previousLinkHandle, linkHandle, new Hinge
                        {
                            LocalHingeAxisA = Vector3.UnitX,
                            LocalHingeAxisB = Vector3.UnitX,
                            LocalOffsetA    = offset * 0.5f,
                            LocalOffsetB    = offset * -0.5f,
                            //Once again, the choice of high stiffness makes this potentially unstable without substepping.
                            SpringSettings = new SpringSettings(120, 1)
                        });
                        Simulation.Solver.Add(previousLinkHandle, linkHandle, new AngularAxisMotor
                        {
                            LocalAxisA     = Vector3.UnitX,
                            TargetVelocity = .25f,
                            Settings       = new MotorSettings(float.MaxValue, 0.0001f)
                        });
                        previousLinkHandle = linkHandle;
                    }
                }
                rolloverInfo.Add(basePosition + new Vector3(0, 4, 0), "High stiffness, long lever arm motorized chain");
            }


            Simulation.Statics.Add(new StaticDescription(new Vector3(0, 0, 0), new CollidableDescription(Simulation.Shapes.Add(new Box(200, 1, 200)), 0.1f)));
        }
Exemple #16
0
        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));
Exemple #17
0
        //    public override Vector3 Scaling
        //    {
        //        get => cachedScaling;
        //        set
        //        {
        //            base.Scaling = value;

        //            foreach (var colliderShape in colliderShapes)
        //            {
        //                colliderShape.Scaling = cachedScaling;
        //            }
        //        }
        //    }

        internal override void CreateAndAddCollidableDescription(
            BepuPhysicsComponent physicsComponent, BepuSimulation xenkoSimulation, out TypedIndex shapeTypeIndex, out CollidableDescription collidableDescription)
        {
            //throw new System.NotImplementedException();
            shapeTypeIndex        = default;
            collidableDescription = default;
        }