//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); }
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);