/// <summary> /// Initializes a new instance of the <see cref="BoxColliderShape"/> class. /// </summary> /// <param name="is2D">If this cube is a 2D quad</param> /// <param name="size">The size of the cube</param> public BoxColliderShape(bool is2D, Vector3 size) { Type = ColliderShapeTypes.Box; Is2D = is2D; BoxSize = size; //Box is not working properly when in a convex2dshape, Z cannot be 0 cachedScaling = Is2D ? new Vector3(1, 1, 0.001f) : Vector3.One; if (is2D) { size.Z = 0.001f; } var shape = new BulletSharp.BoxShape(size / 2) { LocalScaling = cachedScaling, }; if (Is2D) { InternalShape = new BulletSharp.Convex2DShape(shape) { LocalScaling = cachedScaling }; } else { InternalShape = shape; } DebugPrimitiveMatrix = Matrix.Scaling(size * DebugScaling); }
/// <summary> /// Initializes a new instance of the <see cref="SphereColliderShape"/> class. /// </summary> /// <param name="is2D">if set to <c>true</c> [is2 d].</param> /// <param name="radius">The radius.</param> public SphereColliderShape(bool is2D, float radius) { Type = ColliderShapeTypes.Sphere; Is2D = is2D; var shape = new BulletSharp.SphereShape(radius) { LocalScaling = Vector3.One }; if (Is2D) { InternalShape = new BulletSharp.Convex2DShape(shape) { LocalScaling = new Vector3(1, 1, 0) }; } else { InternalShape = shape; } DebugPrimitiveMatrix = Matrix.Scaling(2 * radius * 1.01f); if (Is2D) { DebugPrimitiveMatrix.M33 = 0f; } }
/// <summary> /// Initializes a new instance of the <see cref="SphereColliderShape"/> class. /// </summary> /// <param name="is2D">if set to <c>true</c> [is2 d].</param> /// <param name="radius">The radius.</param> public SphereColliderShape(bool is2D, float radiusParam) { Type = ColliderShapeTypes.Sphere; Is2D = is2D; Radius = radiusParam; cachedScaling = Is2D ? new Vector3(1, 1, 0) : Vector3.One; var shape = new BulletSharp.SphereShape(Radius) { LocalScaling = cachedScaling, }; if (Is2D) { InternalShape = new BulletSharp.Convex2DShape(shape) { LocalScaling = cachedScaling }; } else { InternalShape = shape; } DebugPrimitiveMatrix = Matrix.Scaling(2 * Radius * DebugScaling); if (Is2D) { DebugPrimitiveMatrix.M33 = 0f; } }
/// <summary> /// Initializes a new instance of the <see cref="SphereColliderShape"/> class. /// </summary> /// <param name="is2D">if set to <c>true</c> [is2 d].</param> /// <param name="radius">The radius.</param> public SphereColliderShape(bool is2D, float radius) { Type = ColliderShapeTypes.Sphere; Is2D = is2D; Radius = radius; var shape = new BulletSharp.SphereShape(radius); if (Is2D) { InternalShape = new BulletSharp.Convex2DShape(shape) { LocalScaling = new Vector3(1, 1, 0) }; } else { InternalShape = shape; } if (!PhysicsEngine.Singleton.CreateDebugPrimitives) { return; } DebugPrimitive = GeometricPrimitive.Sphere.New(PhysicsEngine.Singleton.DebugGraphicsDevice); DebugPrimitiveScaling = Matrix.Scaling(radius * 2 * 1.01f); }
/// <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> /// Initializes a new instance of the <see cref="SphereColliderShape"/> class. /// </summary> /// <param name="is2D">if set to <c>true</c> [is2 d].</param> /// <param name="radius">The radius.</param> public SphereColliderShape(bool is2D, float radius) { Type = ColliderShapeTypes.Sphere; Is2D = is2D; var shape = new BulletSharp.SphereShape(radius); if (Is2D) { InternalShape = new BulletSharp.Convex2DShape(shape) { LocalScaling = new Vector3(1, 1, 0) }; } else { InternalShape = shape; } DebugPrimitiveMatrix = Is2D ? Matrix.Scaling(new Vector3(radius * 2 * 1.01f, radius * 2 * 1.01f, 1.0f)) : Matrix.Scaling(radius * 2 * 1.01f); }
/// <summary> /// Initializes a new instance of the <see cref="BoxColliderShape"/> class. /// </summary> /// <param name="is2D">If this cube is a 2D quad</param> /// <param name="size">The size of the cube</param> public BoxColliderShape(bool is2D, Vector3 size, Vector3?offset = null, Quaternion?localrot = null) { Type = ColliderShapeTypes.Box; Is2D = is2D; BoxSize = size; //Box is not working properly when in a convex2dshape, Z cannot be 0 cachedScaling = Is2D ? new Vector3(1, 1, 0.001f) : Vector3.One; if (is2D) { size.Z = 0.001f; } var shape = new BulletSharp.BoxShape(size / 2) { LocalScaling = cachedScaling, }; if (Is2D) { InternalShape = new BulletSharp.Convex2DShape(shape) { LocalScaling = cachedScaling }; } else { InternalShape = shape; } DebugPrimitiveMatrix = Matrix.Scaling(size * DebugScaling); if (offset.HasValue || localrot.HasValue) { LocalOffset = offset ?? Vector3.Zero; LocalRotation = localrot ?? Quaternion.Identity; UpdateLocalTransformations(); } }
/// <summary> /// Initializes a new instance of the <see cref="SphereColliderShape"/> class. /// </summary> /// <param name="is2D">if set to <c>true</c> [is2 d].</param> /// <param name="radius">The radius.</param> public SphereColliderShape(bool is2D, float radiusParam, Vector3?offset = null) { Type = ColliderShapeTypes.Sphere; Is2D = is2D; Radius = radiusParam; cachedScaling = Is2D ? new Vector3(1, 1, 0) : Vector3.One; var shape = new BulletSharp.SphereShape(Radius) { LocalScaling = cachedScaling, }; if (Is2D) { InternalShape = new BulletSharp.Convex2DShape(shape) { LocalScaling = cachedScaling }; } else { InternalShape = shape; } DebugPrimitiveMatrix = Matrix.Scaling(2 * Radius * DebugScaling); if (Is2D) { DebugPrimitiveMatrix.M33 = 0f; } if (offset.HasValue) { LocalOffset = offset.Value; UpdateLocalTransformations(); } }