public CapsuleShape(float radius, float height) { Radius = radius; Height = height; BulletShape = new BulletSharp.CapsuleShape(radius, height); BulletShape.UserObject = this; }
/// <summary> /// Initializes a new instance of the <see cref="CapsuleColliderShape"/> class. /// </summary> /// <param name="is2D">if set to <c>true</c> [is2 d].</param> /// <param name="radius">The radius.</param> /// <param name="height">The height.</param> /// <param name="upAxis">Up axis.</param> public CapsuleColliderShape(bool is2D, float radius, float height, Vector3 upAxis) { Type = ColliderShapeTypes.Capsule; Is2D = is2D; Radius = radius; Height = height; UpAxis = upAxis; BulletSharp.CapsuleShape shape; Matrix rotation; //http://en.wikipedia.org/wiki/Capsule_(geometry) var h = radius * 2 + height; if (upAxis == Vector3.UnitX) { shape = new BulletSharp.CapsuleShapeX(radius, height); rotation = Matrix.RotationZ((float)Math.PI / 2.0f); } else if (upAxis == Vector3.UnitZ) { shape = new BulletSharp.CapsuleShapeZ(radius, height); rotation = Matrix.RotationX((float)Math.PI / 2.0f); } else //default to Y { UpAxis = Vector3.UnitY; shape = new BulletSharp.CapsuleShape(radius, height); rotation = Matrix.Identity; } if (Is2D) { InternalShape = new BulletSharp.Convex2DShape(shape) { LocalScaling = new Vector3(1, 1, 0) }; } else { InternalShape = shape; } if (!PhysicsEngine.Singleton.CreateDebugPrimitives) { return; } DebugPrimitive = GeometricPrimitive.Capsule.New(PhysicsEngine.Singleton.DebugGraphicsDevice); DebugPrimitiveScaling = Matrix.Scaling(new Vector3(radius * 2, h / 2, radius * 2) * 1.01f) * rotation; }
/// <summary> /// Initializes a new instance of the <see cref="CapsuleColliderShape"/> class. /// </summary> /// <param name="is2D">if set to <c>true</c> [is2 d].</param> /// <param name="radius">The radius.</param> /// <param name="height">The height.</param> /// <param name="upAxis">Up axis.</param> public CapsuleColliderShape(bool is2D, float radius, float height, Vector3 upAxis) { Type = ColliderShapeTypes.Capsule; Is2D = is2D; BulletSharp.CapsuleShape shape; Matrix rotation; //http://en.wikipedia.org/wiki/Capsule_(geometry) var h = radius * 2 + height; if (upAxis == Vector3.UnitX) { shape = new BulletSharp.CapsuleShapeX(radius, height); rotation = Matrix.RotationZ((float)Math.PI / 2.0f); } else if (upAxis == Vector3.UnitZ) { shape = new BulletSharp.CapsuleShapeZ(radius, height); rotation = Matrix.RotationX((float)Math.PI / 2.0f); } else //default to Y { shape = new BulletSharp.CapsuleShape(radius, height); rotation = Matrix.Identity; } if (Is2D) { InternalShape = new BulletSharp.Convex2DShape(shape) { LocalScaling = new Vector3(1, 1, 0) }; } else { InternalShape = shape; } DebugPrimitiveMatrix = Matrix.Scaling(new Vector3(radius * 2, h / 2, Is2D ? 1.0f : radius * 2) * 1.01f) * rotation; }
/// <summary> /// Create the collision capsule. /// </summary> /// <param name="radius">Capsule radius.</param> /// <param name="height">Capsule height.</param> /// <param name="axis">Capsule axis direction.</param> public CollisionCapsule(float radius = 1f, float height = 1f, CapsuleDirectionAxis axis = CapsuleDirectionAxis.Y) { _axisType = axis; switch (_axisType) { case CapsuleDirectionAxis.X: _shape = new BulletSharp.CapsuleShapeX(radius, height); break; case CapsuleDirectionAxis.Y: _shape = new BulletSharp.CapsuleShape(radius, height); break; case CapsuleDirectionAxis.Z: _shape = new BulletSharp.CapsuleShapeZ(radius, height); break; } }
/// <summary> /// Initializes a new instance of the <see cref="CapsuleColliderShape"/> class. /// </summary> /// <param name="is2D">if set to <c>true</c> [is2 d].</param> /// <param name="radius">The radius.</param> /// <param name="height">The height.</param> /// <param name="upAxis">Up axis.</param> public CapsuleColliderShape(bool is2D, float radius, float height, Vector3 upAxis) { Type = ColliderShapeTypes.Capsule; Is2D = is2D; BulletSharp.CapsuleShape shape; Matrix rotation; //http://en.wikipedia.org/wiki/Capsule_(geometry) var h = radius * 2 + height; if (upAxis == Vector3.UnitX) { shape = new BulletSharp.CapsuleShapeX(radius, height); rotation = Matrix.RotationZ((float)Math.PI / 2.0f); } else if (upAxis == Vector3.UnitZ) { shape = new BulletSharp.CapsuleShapeZ(radius, height); rotation = Matrix.RotationX((float)Math.PI / 2.0f); } else //default to Y { shape = new BulletSharp.CapsuleShape(radius, height); rotation = Matrix.Identity; } if (Is2D) { InternalShape = new BulletSharp.Convex2DShape(shape) { LocalScaling = new Vector3(1, 1, 0) }; } else { InternalShape = shape; } DebugPrimitiveMatrix = Matrix.Scaling(new Vector3(radius * 2, h / 2, Is2D ? 1.0f : radius * 2) * 1.01f) * rotation; }
public void SetPhysics() { LogHelper.LogInfo("character physics setting"); #if _USE_BEPU_PHYSICS mCharacterController = new BEPUphysics.Character.CharacterController(new BEPUutilities.Vector3(GetLocation().x, GetLocation().y, GetLocation().z), 1.0f, 1.0f * 0.7f, 1.0f * 0.3f, 0.5f, 0.001f, 10f, 0.8f, 1.3f , 5.0f // standing speed , 3f, 1.5f, 1000, 0f, 0f, 0f, 0f ); World.Instance(WorldId).space.Add(mCharacterController); #elif _USE_BULLET_SHARP /* * m_collisionShape = new BulletSharp.CapsuleShape(0.5f, 0.5f); * * m_collisionObject = new BulletSharp.PairCachingGhostObject(); * m_collisionObject.CollisionShape = m_collisionShape; * m_collisionObject.CollisionFlags = BulletSharp.CollisionFlags.CharacterObject; * mCharacterController = new BulletSharp.KinematicCharacterController(m_collisionObject, (BulletSharp.ConvexShape)m_collisionShape, 0.01f); * * * BulletSharp.Math.Matrix worldTrans = new BulletSharp.Math.Matrix(); * worldTrans.M41 = 7f; * worldTrans.M42 = 10f; * worldTrans.M43 = 0; * m_collisionObject.WorldTransform = worldTrans; * m_collisionObject.UserObject = this; * * World.Instance(worldId).world.AddCollisionObject(m_collisionObject, BulletSharp.CollisionFilterGroups.DefaultFilter, BulletSharp.CollisionFilterGroups.Everything); * World.Instance(worldId).world.AddAction(mCharacterController); */ //m_collisionObject = new BulletSharp.RigidBody(new BulletSharp.RigidBodyConstructionInfo(1f, new BulletSharp.DefaultMotionState(), new BulletSharp.CapsuleShape(0.5f, 0.5f), new BulletSharp.Math.Vector3(0, 1f, 0))); //m_collisionObject.CollisionFlags = BulletSharp.CollisionFlags.StaticObject; //BulletSharp.Math.Matrix worldTrans = new BulletSharp.Math.Matrix(); //worldTrans.M41 = 0f; //worldTrans.M42 =1f; //worldTrans.M43 = 0; //m_collisionObject.WorldTransform = worldTrans; //World.Instance(worldId).world.AddRigidBody((BulletSharp.RigidBody)m_collisionObject, BulletSharp.CollisionFilterGroups.DefaultFilter, BulletSharp.CollisionFilterGroups.Everything); ghostObject = new BulletSharp.PairCachingGhostObject(); ghostObject.WorldTransform = BulletSharp.Math.Matrix.Translation(7f, 10f, 0f); World.Instance(worldId).Broadphase.OverlappingPairCache.SetInternalGhostPairCallback(new BulletSharp.GhostPairCallback()); const float characterHeight = 0.5f; const float characterWidth = 0.5f; var capsule = new BulletSharp.CapsuleShape(characterWidth, characterHeight); //capsule.Margin = 0f; ghostObject.CollisionShape = capsule; ghostObject.CollisionFlags = BulletSharp.CollisionFlags.CharacterObject; const float stepHeight = 0.35f; BulletSharp.Math.Vector3 up = new BulletSharp.Math.Vector3(Vector3.up.x, Vector3.up.y, Vector3.up.z); character = new BulletSharp.KinematicCharacterController(ghostObject, capsule, stepHeight, ref up); World.Instance(worldId).world.AddCollisionObject(ghostObject, BulletSharp.CollisionFilterGroups.CharacterFilter, BulletSharp.CollisionFilterGroups.StaticFilter | BulletSharp.CollisionFilterGroups.DefaultFilter); World.Instance(worldId).world.AddAction(character); #elif _USE_BEPU_PHYSICS_V2 var initialPosition = new System.Numerics.Vector3(7f, 10f, 0f); float speculativeMargin = 0.1f; float mass = 1; float maximumHorizontalForce = 20; float maximumVerticalGlueForce = 100; float jumpVelocity = 6; //float speed = 4; float maximumSlope = (float)Math.PI * 0.4f; var shape = new Capsule(0.5f, 1); var shapeIndex = World.Instance(worldId).characters.Simulation.Shapes.Add(shape); var bodyHandle = World.Instance(worldId).characters.Simulation.Bodies.Add(BodyDescription.CreateDynamic(initialPosition, new BodyInertia { InverseMass = 1f / mass }, new CollidableDescription(shapeIndex, speculativeMargin), new BodyActivityDescription(shape.Radius * 0.02f))); character = World.Instance(worldId).characters.AllocateCharacter(bodyHandle); character.LocalUp = new System.Numerics.Vector3(0, 1, 0); character.CosMaximumSlope = (float)Math.Cos(maximumSlope); character.JumpVelocity = jumpVelocity; character.MaximumVerticalForce = maximumVerticalGlueForce; character.MaximumHorizontalForce = maximumHorizontalForce; character.MinimumSupportDepth = shape.Radius * -0.01f; character.MinimumSupportContinuationDepth = -speculativeMargin; body = World.Instance(worldId).characters.Simulation.Bodies.GetBodyReference(bodyHandle); #endif }