Skip to content
Permalink
Browse files

[Physics] Provide read access to collider shape's data

  • Loading branch information
Eideren committed Jun 20, 2019
1 parent a6c8e90 commit 0dda0553c3187556a190ac4675806e1c92851886
@@ -11,6 +11,8 @@ namespace Xenko.Physics
{
public class BoxColliderShape : ColliderShape
{
public readonly Vector3 BoxSize;

/// <summary>
/// Initializes a new instance of the <see cref="BoxColliderShape"/> class.
/// </summary>
@@ -20,6 +22,7 @@ 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

@@ -13,9 +13,9 @@ namespace Xenko.Physics
{
public class CapsuleColliderShape : ColliderShape
{
public readonly float capsuleLength;
public readonly float capsuleRadius;
public readonly ShapeOrientation shapeOrientation;
public readonly float Length;
public readonly float Radius;
public readonly ShapeOrientation Orientation;

/// <summary>
/// Initializes a new instance of the <see cref="CapsuleColliderShape"/> class.
@@ -29,9 +29,9 @@ public CapsuleColliderShape(bool is2D, float radius, float length, ShapeOrientat
Type = ColliderShapeTypes.Capsule;
Is2D = is2D;

capsuleLength = length;
capsuleRadius = radius;
shapeOrientation = orientation;
Length = length;
Radius = radius;
Orientation = orientation;

Matrix rotation;
CapsuleShape shape;
@@ -75,7 +75,7 @@ public CapsuleColliderShape(bool is2D, float radius, float length, ShapeOrientat

public override MeshDraw CreateDebugPrimitive(GraphicsDevice device)
{
return GeometricPrimitive.Capsule.New(device, capsuleLength, capsuleRadius).ToMeshDraw();
return GeometricPrimitive.Capsule.New(device, Length, Radius).ToMeshDraw();
}

public override Vector3 Scaling
@@ -84,7 +84,7 @@ public override Vector3 Scaling
set
{
Vector3 newScaling;
switch (shapeOrientation)
switch (Orientation)
{
case ShapeOrientation.UpX:
{
@@ -12,49 +12,56 @@ namespace Xenko.Physics
{
public class ConeColliderShape : ColliderShape
{
public readonly float Height;
public readonly float Radius;
public readonly ShapeOrientation Orientation;

/// <summary>
/// Initializes a new instance of the <see cref="ConeColliderShape"/> class.
/// </summary>
/// <param name="orientation">Up axis.</param>
/// <param name="radius">The radius of the cone</param>
/// <param name="height">The height of the cone</param>
public ConeColliderShape(float height, float radius, ShapeOrientation orientation)
public ConeColliderShape(float heightParam, float radiusParam, ShapeOrientation orientationParam)
{
Type = ColliderShapeTypes.Cone;
Is2D = false; //always false for cone
Height = heightParam;
Radius = radiusParam;
Orientation = orientationParam;

Matrix rotation;

cachedScaling = Vector3.One;

switch (orientation)
switch (Orientation)
{
case ShapeOrientation.UpX:
InternalShape = new BulletSharp.ConeShapeX(radius, height)
InternalShape = new BulletSharp.ConeShapeX(Radius, Height)
{
LocalScaling = cachedScaling,
};
rotation = Matrix.RotationZ((float)Math.PI / 2.0f);
break;
case ShapeOrientation.UpY:
InternalShape = new BulletSharp.ConeShape(radius, height)
InternalShape = new BulletSharp.ConeShape(Radius, Height)
{
LocalScaling = cachedScaling,
};
rotation = Matrix.Identity;
break;
case ShapeOrientation.UpZ:
InternalShape = new BulletSharp.ConeShapeZ(radius, height)
InternalShape = new BulletSharp.ConeShapeZ(Radius, Height)
{
LocalScaling = cachedScaling,
};
rotation = Matrix.RotationX((float)Math.PI / 2.0f);
break;
default:
throw new ArgumentOutOfRangeException(nameof(orientation));
throw new ArgumentOutOfRangeException(nameof(Orientation));
}

DebugPrimitiveMatrix = Matrix.Scaling(new Vector3(radius * 2, height, radius * 2) * DebugScaling) * rotation;
DebugPrimitiveMatrix = Matrix.Scaling(new Vector3(Radius * 2, Height, Radius * 2) * DebugScaling) * rotation;
}

public override MeshDraw CreateDebugPrimitive(GraphicsDevice device)
@@ -12,52 +12,56 @@ namespace Xenko.Physics
{
public class CylinderColliderShape : ColliderShape
{
private readonly ShapeOrientation shapeOrientation;
public readonly ShapeOrientation Orientation;
public readonly float Height;
public readonly float Radius;

/// <summary>
/// Initializes a new instance of the <see cref="CylinderColliderShape"/> class.
/// </summary>
/// <param name="orientation">Up axis.</param>
/// <param name="radius">The radius of the cylinder</param>
/// <param name="height">The height of the cylinder</param>
public CylinderColliderShape(float height, float radius, ShapeOrientation orientation)
public CylinderColliderShape(float heightParam, float radiusParam, ShapeOrientation orientationParam)
{
Type = ColliderShapeTypes.Cylinder;
Is2D = false; //always false for cylinders
Height = heightParam;
Radius = radiusParam;

Matrix rotation;

cachedScaling = Vector3.One;
shapeOrientation = orientation;
Orientation = orientationParam;

switch (orientation)
switch (Orientation)
{
case ShapeOrientation.UpX:
InternalShape = new BulletSharp.CylinderShapeX(new Vector3(height / 2, radius, radius))
InternalShape = new BulletSharp.CylinderShapeX(new Vector3(Height / 2, Radius, Radius))
{
LocalScaling = cachedScaling,
};
rotation = Matrix.RotationZ((float)Math.PI / 2.0f);
break;
case ShapeOrientation.UpY:
InternalShape = new BulletSharp.CylinderShape(new Vector3(radius, height / 2, radius))
InternalShape = new BulletSharp.CylinderShape(new Vector3(Radius, Height / 2, Radius))
{
LocalScaling = cachedScaling,
};
rotation = Matrix.Identity;
break;
case ShapeOrientation.UpZ:
InternalShape = new BulletSharp.CylinderShapeZ(new Vector3(radius, radius, height / 2))
InternalShape = new BulletSharp.CylinderShapeZ(new Vector3(Radius, Radius, Height / 2))
{
LocalScaling = cachedScaling,
};
rotation = Matrix.RotationX((float)Math.PI / 2.0f);
break;
default:
throw new ArgumentOutOfRangeException(nameof(orientation));
throw new ArgumentOutOfRangeException(nameof(Orientation));
}

DebugPrimitiveMatrix = Matrix.Scaling(new Vector3(radius * 2, height, radius * 2) * DebugScaling) * rotation;
DebugPrimitiveMatrix = Matrix.Scaling(new Vector3(Radius * 2, Height, Radius * 2) * DebugScaling) * rotation;
}

public override MeshDraw CreateDebugPrimitive(GraphicsDevice device)
@@ -11,19 +11,22 @@ namespace Xenko.Physics
{
public class SphereColliderShape : ColliderShape
{
public readonly float Radius;

/// <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)
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)
var shape = new BulletSharp.SphereShape(Radius)
{
LocalScaling = cachedScaling,
};
@@ -37,7 +40,7 @@ public SphereColliderShape(bool is2D, float radius)
InternalShape = shape;
}

DebugPrimitiveMatrix = Matrix.Scaling(2 * radius * DebugScaling);
DebugPrimitiveMatrix = Matrix.Scaling(2 * Radius * DebugScaling);
if (Is2D)
{
DebugPrimitiveMatrix.M33 = 0f;
@@ -12,6 +12,9 @@ namespace Xenko.Physics
{
public class StaticPlaneColliderShape : ColliderShape
{
public readonly Vector3 Normal;
public readonly float Offset;

/// <summary>
/// Initializes a new instance of the <see cref="StaticPlaneColliderShape"/> class.
/// A static plane that is solid to infinity on one side.
@@ -20,20 +23,22 @@ public class StaticPlaneColliderShape : ColliderShape
/// </summary>
/// <param name="normal">The normal.</param>
/// <param name="offset">The offset.</param>
public StaticPlaneColliderShape(Vector3 normal, float offset)
public StaticPlaneColliderShape(Vector3 normalParam, float offsetParam)
{
Type = ColliderShapeTypes.StaticPlane;
Is2D = false;
Normal = normalParam;
Offset = offsetParam;

cachedScaling = Vector3.One;

InternalShape = new BulletSharp.StaticPlaneShape(normal, offset)
InternalShape = new BulletSharp.StaticPlaneShape(Normal, Offset)
{
LocalScaling = cachedScaling,
};

Matrix rotationMatrix;
var oY = Vector3.Normalize(normal);
var oY = Vector3.Normalize(Normal);
var oZ = Vector3.Cross(Vector3.UnitX, oY);
if (oZ.Length() > MathUtil.ZeroTolerance)
{
@@ -55,7 +60,7 @@ public StaticPlaneColliderShape(Vector3 normal, float offset)
0, 0, 0, 1);
}

DebugPrimitiveMatrix = Matrix.Translation(offset * Vector3.UnitY) * rotationMatrix;
DebugPrimitiveMatrix = Matrix.Translation(Offset * Vector3.UnitY) * rotationMatrix;
}

public override MeshDraw CreateDebugPrimitive(GraphicsDevice device)

0 comments on commit 0dda055

Please sign in to comment.
You can’t perform that action at this time.