Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PhysicsColliderShapeDataConverter.cs
Go to the documentation of this file.
1 using System;
2 
3 using SiliconStudio.Core.Mathematics;
4 using SiliconStudio.Core.Serialization.Assets;
5 using SiliconStudio.Core.Serialization.Converters;
6 
7 namespace SiliconStudio.Paradox.Physics
8 {
9  public class PhysicsColliderShapeDataConverter : DataConverter<PhysicsColliderShapeData, PhysicsColliderShape>
10  {
11  public override void ConstructFromData(ConverterContext converterContext, PhysicsColliderShapeData data, ref PhysicsColliderShape obj)
12  {
13  throw new NotImplementedException();
14  }
15 
16  private static ColliderShape CreateShape(IColliderShapeDesc desc)
17  {
18  ColliderShape shape = null;
19 
20  var type = desc.GetType();
21  if (type == typeof(Box2DColliderShapeDesc))
22  {
23  var boxDesc = (Box2DColliderShapeDesc)desc;
24  var rotation = Quaternion.Identity; //todo
25  shape = new Box2DColliderShape(boxDesc.HalfExtent) { LocalOffset = boxDesc.LocalOffset, LocalRotation = rotation };
26  }
27  else if (type == typeof(BoxColliderShapeDesc))
28  {
29  var boxDesc = (BoxColliderShapeDesc)desc;
30  var rotation = Quaternion.Identity; //todo
31  shape = new BoxColliderShape(boxDesc.HalfExtents) { LocalOffset = boxDesc.LocalOffset, LocalRotation = rotation };
32  }
33  else if (type == typeof(CapsuleColliderShapeDesc))
34  {
35  var capsuleDesc = (CapsuleColliderShapeDesc)desc;
36  var rotation = Quaternion.Identity; //todo
37  shape = new CapsuleColliderShape(capsuleDesc.Is2D, capsuleDesc.Radius, capsuleDesc.Height, capsuleDesc.UpAxis) { LocalOffset = capsuleDesc.LocalOffset, LocalRotation = rotation };
38  }
39  else if (type == typeof(CylinderColliderShapeDesc))
40  {
41  var cylinderDesc = (CylinderColliderShapeDesc)desc;
42  var rotation = Quaternion.Identity; //todo
43  shape = new CylinderColliderShape(cylinderDesc.HalfExtents, cylinderDesc.UpAxis) { LocalOffset = cylinderDesc.LocalOffset, LocalRotation = rotation };
44  }
45  else if (type == typeof(SphereColliderShapeDesc))
46  {
47  var sphereDesc = (SphereColliderShapeDesc)desc;
48  shape = new SphereColliderShape(sphereDesc.Is2D, sphereDesc.Radius) { LocalOffset = sphereDesc.LocalOffset };
49  }
50  else if (type == typeof(StaticPlaneColliderShapeDesc))
51  {
52  var planeDesc = (StaticPlaneColliderShapeDesc)desc;
53  shape = new StaticPlaneColliderShape(planeDesc.Normal, planeDesc.Offset);
54  }
55  else if (type == typeof(ConvexHullColliderShapeDesc))
56  {
57  var convexDesc = (ConvexHullColliderShapeDesc)desc;
58 
59  //Optimize performance and focus on less shapes creation since this shape could be nested
60 
61  if (convexDesc.ConvexHulls.Count == 1)
62  {
63  if (convexDesc.ConvexHulls[0].Count == 1)
64  {
65  shape = new ConvexHullColliderShape(convexDesc.ConvexHulls[0][0], convexDesc.ConvexHullsIndices[0][0])
66  {
67  NeedsCustomCollisionCallback = true
68  };
69 
70  shape.UpdateLocalTransformations();
71 
72  return shape;
73  }
74 
75  if (convexDesc.ConvexHulls[0].Count <= 1) return null;
76 
77  var subCompound = new CompoundColliderShape
78  {
79  NeedsCustomCollisionCallback = true
80  };
81 
82  for (var i = 0; i < convexDesc.ConvexHulls[0].Count; i++)
83  {
84  var verts = convexDesc.ConvexHulls[0][i];
85  var indices = convexDesc.ConvexHullsIndices[0][i];
86 
87  var subHull = new ConvexHullColliderShape(verts, indices);
88  subHull.UpdateLocalTransformations();
89  subCompound.AddChildShape(subHull);
90  }
91 
92  subCompound.UpdateLocalTransformations();
93 
94  return subCompound;
95  }
96 
97  if (convexDesc.ConvexHulls.Count <= 1) return null;
98 
99  var compound = new CompoundColliderShape
100  {
101  NeedsCustomCollisionCallback = true
102  };
103 
104  for (var i = 0; i < convexDesc.ConvexHulls.Count; i++)
105  {
106  var verts = convexDesc.ConvexHulls[i];
107  var indices = convexDesc.ConvexHullsIndices[i];
108 
109  if (verts.Count == 1)
110  {
111  var subHull = new ConvexHullColliderShape(verts[0], indices[0]);
112  subHull.UpdateLocalTransformations();
113  compound.AddChildShape(subHull);
114  }
115  else if (verts.Count > 1)
116  {
117  var subCompound = new CompoundColliderShape();
118 
119  for (var b = 0; b < verts.Count; b++)
120  {
121  var subVerts = verts[b];
122  var subIndex = indices[b];
123 
124  var subHull = new ConvexHullColliderShape(subVerts, subIndex);
125  subHull.UpdateLocalTransformations();
126  subCompound.AddChildShape(subHull);
127  }
128 
129  subCompound.UpdateLocalTransformations();
130 
131  compound.AddChildShape(subCompound);
132  }
133  }
134 
135  compound.UpdateLocalTransformations();
136 
137  return compound;
138  }
139 
140  if (shape != null) shape.UpdateLocalTransformations();
141 
142  return shape;
143  }
144 
145  public override void ConvertFromData(ConverterContext converterContext, PhysicsColliderShapeData data, ref PhysicsColliderShape obj)
146  {
147  if (data.ColliderShapes.Count == 1) //single shape case
148  {
149  obj = new PhysicsColliderShape(CreateShape(data.ColliderShapes[0]));
150  }
151  else if (data.ColliderShapes.Count > 1) //need a compound shape in this case
152  {
154  var compound = (CompoundColliderShape)obj.Shape;
155  foreach (var desc in data.ColliderShapes)
156  {
157  compound.AddChildShape(CreateShape(desc));
158  }
159  }
160  }
161 
162  public override void ConvertToData(ConverterContext converterContext, ref PhysicsColliderShapeData data, PhysicsColliderShape obj)
163  {
164  throw new NotImplementedException();
165  }
166  }
167 }
override void ConvertFromData(ConverterContext converterContext, PhysicsColliderShapeData data, ref PhysicsColliderShape obj)
function b
Base class for converters to/from a data type.
override void ConstructFromData(ConverterContext converterContext, PhysicsColliderShapeData data, ref PhysicsColliderShape obj)
using SiliconStudio.Paradox. Physics
override void ConvertToData(ConverterContext converterContext, ref PhysicsColliderShapeData data, PhysicsColliderShape obj)