Advertisement
Guest User

Untitled

a guest
May 13th, 2012
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8.  
  9. using Jitter.LinearMath;
  10. using Jitter.Collision.Shapes;
  11. using Jitter.Dynamics;
  12. using Jitter.Collision;
  13.  
  14. namespace XNAEngine.Entities
  15. {
  16. class PhysicsEntity : RenderableEntity
  17. {
  18. RigidBody PhysicsBody;
  19.  
  20. public PhysicsEntity(Model Model, Jitter.World World)
  21. : base(Model)
  22. {
  23.  
  24. /*List<TriangleVertexIndices> indices = new List<TriangleVertexIndices>();
  25. List<Vector3> vertices = new List<Vector3>();
  26.  
  27. ExtractData(vertices, indices, Model);
  28.  
  29. List<JVector> jvertices = new List<JVector>(vertices.Count);
  30. foreach (Vector3 vertex in vertices) jvertices.Add(new JVector(vertex.X, vertex.Y, vertex.Z));
  31.  
  32. ConvexHullShape PhysMesh = new ConvexHullShape(jvertices);*/
  33.  
  34. List<CompoundShape.TransformedShape> transformedShapes = new List<CompoundShape.TransformedShape>();
  35.  
  36. Matrix[] bones_ = new Matrix[Model.Bones.Count];
  37. Model.CopyAbsoluteBoneTransformsTo(bones_);
  38.  
  39. foreach (ModelMesh mm in Model.Meshes)
  40. {
  41. transformedShapes.Add( ExtractData(mm, bones_) );
  42. }
  43.  
  44. CompoundShape PhysMesh = new CompoundShape(transformedShapes);
  45.  
  46. PhysicsBody = new RigidBody(PhysMesh);
  47. PhysicsBody.EnableDebugDraw = true;
  48. World.AddBody(PhysicsBody);
  49. }
  50.  
  51. public void Debug(Jitter.IDebugDrawer debugDraw)
  52. {
  53. PhysicsBody.DebugDraw(debugDraw);
  54. }
  55.  
  56. public override Vector3 GetPosition()
  57. {
  58. return new Vector3(PhysicsBody.Position.X, PhysicsBody.Position.Y, PhysicsBody.Position.Z);
  59. }
  60.  
  61. public override Matrix GetRotation()
  62. {
  63. return ToXNAMatrix(PhysicsBody.Orientation);
  64. }
  65.  
  66. public Vector3 GetVelocity()
  67. {
  68. return new Vector3(PhysicsBody.LinearVelocity.X, PhysicsBody.LinearVelocity.Y, PhysicsBody.LinearVelocity.Z);
  69. }
  70.  
  71. public Vector3 GetAngVelocity()
  72. {
  73. return new Vector3(PhysicsBody.AngularVelocity.X, PhysicsBody.AngularVelocity.Y, PhysicsBody.AngularVelocity.Z);
  74. }
  75.  
  76. public bool GetIsStatic()
  77. {
  78. return PhysicsBody.IsStatic;
  79. }
  80.  
  81. public override void SetPosition(Vector3 Position)
  82. {
  83. PhysicsBody.Position = new JVector(Position.X, Position.Y, Position.Z);
  84. }
  85.  
  86. public override void SetRotation(Matrix Rotation)
  87. {
  88. PhysicsBody.Orientation = ToJitterMatrix(Rotation);
  89. }
  90.  
  91. public void SetVelocity(Vector3 Velocity)
  92. {
  93. PhysicsBody.LinearVelocity = new JVector(Velocity.X, Velocity.Y, Velocity.Z);
  94. }
  95.  
  96. public void SetAngVelocity(Vector3 AngVelocity)
  97. {
  98. PhysicsBody.AngularVelocity = new JVector(AngVelocity.X, AngVelocity.Y, AngVelocity.Z);
  99. }
  100.  
  101. public void SetIsStatic(bool Static)
  102. {
  103. PhysicsBody.IsStatic = Static;
  104. }
  105.  
  106. protected Matrix ToXNAMatrix(JMatrix Matrix)
  107. {
  108. return new Matrix ( Matrix.M11 ,
  109. Matrix.M12 ,
  110. Matrix.M13 ,
  111. 0.0f ,
  112. Matrix.M21 ,
  113. Matrix.M22 ,
  114. Matrix.M23 ,
  115. 0.0f ,
  116. Matrix.M31 ,
  117. Matrix.M32 ,
  118. Matrix.M33 ,
  119. 0.0f , 0.0f , 0.0f , 0.0f , 1.0f);
  120. }
  121.  
  122. protected JMatrix ToJitterMatrix(Matrix matrix)
  123. {
  124. JMatrix result;
  125. result.M11 = matrix.M11;
  126. result.M12 = matrix.M12;
  127. result.M13 = matrix.M13;
  128. result.M21 = matrix.M21;
  129. result.M22 = matrix.M22;
  130. result.M23 = matrix.M23;
  131. result.M31 = matrix.M31;
  132. result.M32 = matrix.M32;
  133. result.M33 = matrix.M33;
  134. return result;
  135.  
  136. }
  137.  
  138. protected void ExtractData(List<Vector3> vertices, List<TriangleVertexIndices> indices, Model model)
  139. {
  140. Matrix[] bones_ = new Matrix[model.Bones.Count];
  141. model.CopyAbsoluteBoneTransformsTo(bones_);
  142. foreach (ModelMesh mm in model.Meshes)
  143. {
  144. Matrix xform = bones_[mm.ParentBone.Index];
  145. foreach (ModelMeshPart mmp in mm.MeshParts)
  146. {
  147. int offset = vertices.Count;
  148. Vector3[] a = new Vector3[mmp.NumVertices];
  149. mmp.VertexBuffer.GetData<Vector3>(mmp.VertexOffset * mmp.VertexBuffer.VertexDeclaration.VertexStride,
  150. a, 0, mmp.NumVertices, mmp.VertexBuffer.VertexDeclaration.VertexStride);
  151. for (int i = 0; i != a.Length; ++i)
  152. Vector3.Transform(ref a[i], ref xform, out a[i]);
  153. vertices.AddRange(a);
  154.  
  155. if (mmp.IndexBuffer.IndexElementSize != IndexElementSize.SixteenBits)
  156. throw new Exception(
  157. String.Format("Model uses 32-bit indices, which are not supported."));
  158. short[] s = new short[mmp.PrimitiveCount * 3];
  159. mmp.IndexBuffer.GetData<short>(mmp.StartIndex * 2, s, 0, mmp.PrimitiveCount * 3);
  160. TriangleVertexIndices[] tvi = new TriangleVertexIndices[mmp.PrimitiveCount];
  161. for (int i = 0; i != tvi.Length; ++i)
  162. {
  163. tvi[i].I0 = s[i * 3 + 0] + offset;
  164. tvi[i].I1 = s[i * 3 + 1] + offset;
  165. tvi[i].I2 = s[i * 3 + 2] + offset;
  166. }
  167. indices.AddRange(tvi);
  168. }
  169. }
  170. }
  171.  
  172. protected CompoundShape.TransformedShape ExtractData(ModelMesh mm, Matrix[] bones_)
  173. {
  174. List<TriangleVertexIndices> indices = new List<TriangleVertexIndices>();
  175. List<Vector3> vertices = new List<Vector3>();
  176.  
  177. Matrix xform = bones_[mm.ParentBone.Index];
  178. foreach (ModelMeshPart mmp in mm.MeshParts)
  179. {
  180. int offset = vertices.Count;
  181. Vector3[] a = new Vector3[mmp.NumVertices];
  182. mmp.VertexBuffer.GetData<Vector3>(mmp.VertexOffset * mmp.VertexBuffer.VertexDeclaration.VertexStride,
  183. a, 0, mmp.NumVertices, mmp.VertexBuffer.VertexDeclaration.VertexStride);
  184. //for (int i = 0; i != a.Length; ++i)
  185. // Vector3.Transform(ref a[i], ref xform, out a[i]);
  186. vertices.AddRange(a);
  187.  
  188. if (mmp.IndexBuffer.IndexElementSize != IndexElementSize.SixteenBits)
  189. throw new Exception(
  190. String.Format("Model uses 32-bit indices, which are not supported."));
  191. short[] s = new short[mmp.PrimitiveCount * 3];
  192. mmp.IndexBuffer.GetData<short>(mmp.StartIndex * 2, s, 0, mmp.PrimitiveCount * 3);
  193. TriangleVertexIndices[] tvi = new TriangleVertexIndices[mmp.PrimitiveCount];
  194. for (int i = 0; i != tvi.Length; ++i)
  195. {
  196. tvi[i].I0 = s[i * 3 + 0] + offset;
  197. tvi[i].I1 = s[i * 3 + 1] + offset;
  198. tvi[i].I2 = s[i * 3 + 2] + offset;
  199. }
  200. indices.AddRange(tvi);
  201. }
  202.  
  203. List<JVector> jvertices = new List<JVector>(vertices.Count);
  204. foreach (Vector3 vertex in vertices) jvertices.Add(new JVector(vertex.X, vertex.Y, vertex.Z));
  205.  
  206. Shape convexHull = new ConvexHullShape(jvertices);
  207.  
  208. Vector3 translation, scale;
  209. Quaternion rotation;
  210.  
  211. xform.Decompose(out scale, out rotation, out translation);
  212.  
  213. return new CompoundShape.TransformedShape(convexHull, ToJitterMatrix(Matrix.CreateFromQuaternion(rotation)), new JVector(translation.X, translation.Y, translation.Z));
  214. }
  215. }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement