SEA3DRigidBody.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /**
  2. * SEA3D - Delta Lossy Compression
  3. * @author Sunag / http://www.sunag.com.br/
  4. */
  5. 'use strict';
  6. //
  7. // Sphere
  8. //
  9. SEA3D.Sphere = function ( name, data, sea3d ) {
  10. this.name = name;
  11. this.data = data;
  12. this.sea3d = sea3d;
  13. this.radius = data.readFloat();
  14. };
  15. SEA3D.Sphere.prototype.type = "sph";
  16. //
  17. // Box
  18. //
  19. SEA3D.Box = function ( name, data, sea3d ) {
  20. this.name = name;
  21. this.data = data;
  22. this.sea3d = sea3d;
  23. this.width = data.readFloat();
  24. this.height = data.readFloat();
  25. this.depth = data.readFloat();
  26. };
  27. SEA3D.Box.prototype.type = "box";
  28. //
  29. // Cone
  30. //
  31. SEA3D.Cone = function ( name, data, sea3d ) {
  32. this.name = name;
  33. this.data = data;
  34. this.sea3d = sea3d;
  35. this.radius = data.readFloat();
  36. this.height = data.readFloat();
  37. };
  38. SEA3D.Cone.prototype.type = "cone";
  39. //
  40. // Capsule
  41. //
  42. SEA3D.Capsule = function ( name, data, sea3d ) {
  43. this.name = name;
  44. this.data = data;
  45. this.sea3d = sea3d;
  46. this.radius = data.readFloat();
  47. this.height = data.readFloat();
  48. };
  49. SEA3D.Capsule.prototype.type = "cap";
  50. //
  51. // Cylinder
  52. //
  53. SEA3D.Cylinder = function ( name, data, sea3d ) {
  54. this.name = name;
  55. this.data = data;
  56. this.sea3d = sea3d;
  57. this.radius = data.readFloat();
  58. this.height = data.readFloat();
  59. };
  60. SEA3D.Cylinder.prototype.type = "cyl";
  61. //
  62. // Convex Geometry
  63. //
  64. SEA3D.ConvexGeometry = function ( name, data, sea3d ) {
  65. this.name = name;
  66. this.data = data;
  67. this.sea3d = sea3d;
  68. this.geometry = sea3d.getObject( data.readUInt() );
  69. this.subGeometryIndex = data.readUByte();
  70. };
  71. SEA3D.ConvexGeometry.prototype.type = "gs";
  72. //
  73. // Triangle Geometry
  74. //
  75. SEA3D.TriangleGeometry = function ( name, data, sea3d ) {
  76. this.name = name;
  77. this.data = data;
  78. this.sea3d = sea3d;
  79. this.geometry = sea3d.getObject( data.readUInt() );
  80. this.subGeometryIndex = data.readUByte();
  81. };
  82. SEA3D.TriangleGeometry.prototype.type = "sgs";
  83. //
  84. // Compound
  85. //
  86. SEA3D.Compound = function ( name, data, sea3d ) {
  87. this.name = name;
  88. this.data = data;
  89. this.sea3d = sea3d;
  90. this.compounds = [];
  91. var count = data.readUByte();
  92. for ( var i = 0; i < count; i ++ ) {
  93. this.compounds.push( {
  94. shape: sea3d.getObject( data.readUInt() ),
  95. transform: data.readMatrix()
  96. } );
  97. }
  98. };
  99. SEA3D.Compound.prototype.type = "cmps";
  100. //
  101. // Physics
  102. //
  103. SEA3D.Physics = function ( name, data, sea3d ) {
  104. this.name = name;
  105. this.data = data;
  106. this.sea3d = sea3d;
  107. this.attrib = data.readUShort();
  108. this.shape = sea3d.getObject( data.readUInt() );
  109. if ( this.attrib & 1 ) this.target = sea3d.getObject( data.readUInt() );
  110. else this.transform = data.readMatrix();
  111. };
  112. SEA3D.Physics.prototype.readTag = function ( kind, data, size ) {
  113. };
  114. //
  115. // Rigidy Body Base
  116. //
  117. SEA3D.RigidBodyBase = function ( name, data, sea3d ) {
  118. SEA3D.Physics.call( this, name, data, sea3d );
  119. if ( this.attrib & 32 ) {
  120. this.linearDamping = data.readFloat();
  121. this.angularDamping = data.readFloat();
  122. } else {
  123. this.linearDamping = 0;
  124. this.angularDamping = 0;
  125. }
  126. this.mass = data.readFloat();
  127. this.friction = data.readFloat();
  128. this.restitution = data.readFloat();
  129. };
  130. SEA3D.RigidBodyBase.prototype = Object.create( SEA3D.Physics.prototype );
  131. SEA3D.RigidBodyBase.prototype.constructor = SEA3D.RigidBodyBase;
  132. //
  133. // Rigidy Body
  134. //
  135. SEA3D.RigidBody = function ( name, data, sea3d ) {
  136. SEA3D.RigidBodyBase.call( this, name, data, sea3d );
  137. data.readTags( this.readTag.bind( this ) );
  138. };
  139. SEA3D.RigidBody.prototype = Object.create( SEA3D.RigidBodyBase.prototype );
  140. SEA3D.RigidBody.prototype.constructor = SEA3D.RigidBody;
  141. SEA3D.RigidBody.prototype.type = "rb";
  142. //
  143. // Car Controller
  144. //
  145. SEA3D.CarController = function ( name, data, sea3d ) {
  146. SEA3D.RigidBodyBase.call( this, name, data, sea3d );
  147. this.suspensionStiffness = data.readFloat();
  148. this.suspensionCompression = data.readFloat();
  149. this.suspensionDamping = data.readFloat();
  150. this.maxSuspensionTravelCm = data.readFloat();
  151. this.frictionSlip = data.readFloat();
  152. this.maxSuspensionForce = data.readFloat();
  153. this.dampingCompression = data.readFloat();
  154. this.dampingRelaxation = data.readFloat();
  155. var count = data.readUByte();
  156. this.wheel = [];
  157. for ( var i = 0; i < count; i ++ ) {
  158. this.wheel[ i ] = new SEA3D.CarController.Wheel( data, sea3d );
  159. }
  160. data.readTags( this.readTag.bind( this ) );
  161. };
  162. SEA3D.CarController.Wheel = function ( data, sea3d ) {
  163. this.data = data;
  164. this.sea3d = sea3d;
  165. var attrib = data.readUShort();
  166. this.isFront = ( attrib & 1 ) != 0,
  167. this.target = sea3d.getObject( data.readUInt() );
  168. this.pos = data.readVector3();
  169. this.dir = data.readVector3();
  170. this.axle = data.readVector3();
  171. this.radius = data.readFloat();
  172. this.suspensionRestLength = data.readFloat();
  173. };
  174. SEA3D.CarController.prototype = Object.create( SEA3D.RigidBodyBase.prototype );
  175. SEA3D.CarController.prototype.constructor = SEA3D.CarController;
  176. SEA3D.CarController.prototype.type = "carc";
  177. //
  178. // Constraints
  179. //
  180. SEA3D.Constraints = function ( name, data, sea3d ) {
  181. this.name = name;
  182. this.data = data;
  183. this.sea3d = sea3d;
  184. this.attrib = data.readUShort();
  185. this.disableCollisionsBetweenBodies = this.attrib & 1 != 0;
  186. this.targetA = sea3d.getObject( data.readUInt() );
  187. this.pointA = data.readVector3();
  188. if ( this.attrib & 2 ) {
  189. this.targetB = sea3d.getObject( data.readUInt() );
  190. this.pointB = data.readVector3();
  191. }
  192. };
  193. //
  194. // P2P Constraint
  195. //
  196. SEA3D.P2PConstraint = function ( name, data, sea3d ) {
  197. this.name = name;
  198. this.data = data;
  199. this.sea3d = sea3d;
  200. SEA3D.Constraints.call( this, name, data, sea3d );
  201. };
  202. SEA3D.P2PConstraint.prototype = Object.create( SEA3D.Constraints.prototype );
  203. SEA3D.P2PConstraint.prototype.constructor = SEA3D.P2PConstraint;
  204. SEA3D.P2PConstraint.prototype.type = "p2pc";
  205. //
  206. // Hinge Constraint
  207. //
  208. SEA3D.HingeConstraint = function ( name, data, sea3d ) {
  209. SEA3D.Constraints.call( this, name, data, sea3d );
  210. this.axisA = data.readVector3();
  211. if ( this.attrib & 1 ) {
  212. this.axisB = data.readVector3();
  213. }
  214. if ( this.attrib & 4 ) {
  215. this.limit = {
  216. low: data.readFloat(),
  217. high: data.readFloat(),
  218. softness: data.readFloat(),
  219. biasFactor: data.readFloat(),
  220. relaxationFactor: data.readFloat()
  221. };
  222. }
  223. if ( this.attrib & 8 ) {
  224. this.angularMotor = {
  225. velocity: data.readFloat(),
  226. impulse: data.readFloat()
  227. };
  228. }
  229. };
  230. SEA3D.HingeConstraint.prototype = Object.create( SEA3D.Constraints.prototype );
  231. SEA3D.HingeConstraint.prototype.constructor = SEA3D.HingeConstraint;
  232. SEA3D.HingeConstraint.prototype.type = "hnec";
  233. //
  234. // Cone Twist Constraint
  235. //
  236. SEA3D.ConeTwistConstraint = function ( name, data, sea3d ) {
  237. SEA3D.Constraints.call( this, name, data, sea3d );
  238. this.axisA = data.readVector3();
  239. if ( this.attrib & 1 ) {
  240. this.axisB = data.readVector3();
  241. }
  242. if ( this.attrib & 4 ) {
  243. this.limit = {
  244. swingSpan1: data.readFloat(),
  245. swingSpan2: data.readFloat(),
  246. twistSpan: data.readFloat(),
  247. softness: data.readFloat(),
  248. biasFactor: data.readFloat(),
  249. relaxationFactor: data.readFloat()
  250. };
  251. }
  252. };
  253. SEA3D.ConeTwistConstraint.prototype = Object.create( SEA3D.Constraints.prototype );
  254. SEA3D.ConeTwistConstraint.prototype.constructor = SEA3D.ConeTwistConstraint;
  255. SEA3D.ConeTwistConstraint.prototype.type = "ctwc";
  256. //
  257. // Extension
  258. //
  259. SEA3D.File.setExtension( function () {
  260. // PHYSICS
  261. this.addClass( SEA3D.Sphere );
  262. this.addClass( SEA3D.Box );
  263. this.addClass( SEA3D.Cone );
  264. this.addClass( SEA3D.Capsule );
  265. this.addClass( SEA3D.Cylinder );
  266. this.addClass( SEA3D.ConvexGeometry );
  267. this.addClass( SEA3D.TriangleGeometry );
  268. this.addClass( SEA3D.Compound );
  269. this.addClass( SEA3D.RigidBody );
  270. this.addClass( SEA3D.P2PConstraint );
  271. this.addClass( SEA3D.HingeConstraint );
  272. this.addClass( SEA3D.ConeTwistConstraint );
  273. this.addClass( SEA3D.CarController );
  274. } );