SEA3DRigidBody.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /**
  2. * SEA3D - Rigid Body
  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. if ( this.attrib & 2 ) this.offset = data.readMatrix();
  112. if ( this.attrib & 4 ) this.scripts = data.readScriptList( sea3d );
  113. if ( this.attrib & 16 ) this.attributes = sea3d.getObject( data.readUInt() );
  114. };
  115. SEA3D.Physics.prototype.readTag = function ( kind, data, size ) {
  116. };
  117. //
  118. // Rigidy Body Base
  119. //
  120. SEA3D.RigidBodyBase = function ( name, data, sea3d ) {
  121. SEA3D.Physics.call( this, name, data, sea3d );
  122. if ( this.attrib & 32 ) {
  123. this.linearDamping = data.readFloat();
  124. this.angularDamping = data.readFloat();
  125. } else {
  126. this.linearDamping = 0;
  127. this.angularDamping = 0;
  128. }
  129. this.mass = data.readFloat();
  130. this.friction = data.readFloat();
  131. this.restitution = data.readFloat();
  132. };
  133. SEA3D.RigidBodyBase.prototype = Object.create( SEA3D.Physics.prototype );
  134. SEA3D.RigidBodyBase.prototype.constructor = SEA3D.RigidBodyBase;
  135. //
  136. // Rigidy Body
  137. //
  138. SEA3D.RigidBody = function ( name, data, sea3d ) {
  139. SEA3D.RigidBodyBase.call( this, name, data, sea3d );
  140. data.readTags( this.readTag.bind( this ) );
  141. };
  142. SEA3D.RigidBody.prototype = Object.create( SEA3D.RigidBodyBase.prototype );
  143. SEA3D.RigidBody.prototype.constructor = SEA3D.RigidBody;
  144. SEA3D.RigidBody.prototype.type = "rb";
  145. //
  146. // Car Controller
  147. //
  148. SEA3D.CarController = function ( name, data, sea3d ) {
  149. SEA3D.RigidBodyBase.call( this, name, data, sea3d );
  150. this.suspensionStiffness = data.readFloat();
  151. this.suspensionCompression = data.readFloat();
  152. this.suspensionDamping = data.readFloat();
  153. this.maxSuspensionTravelCm = data.readFloat();
  154. this.frictionSlip = data.readFloat();
  155. this.maxSuspensionForce = data.readFloat();
  156. this.dampingCompression = data.readFloat();
  157. this.dampingRelaxation = data.readFloat();
  158. var count = data.readUByte();
  159. this.wheel = [];
  160. for ( var i = 0; i < count; i ++ ) {
  161. this.wheel[ i ] = new SEA3D.CarController.Wheel( data, sea3d );
  162. }
  163. data.readTags( this.readTag.bind( this ) );
  164. };
  165. SEA3D.CarController.Wheel = function ( data, sea3d ) {
  166. this.data = data;
  167. this.sea3d = sea3d;
  168. this.attrib = data.readUShort();
  169. this.isFront = ( this.attrib & 1 ) != 0;
  170. if ( this.attrib & 2 ) {
  171. this.target = sea3d.getObject( data.readUInt() );
  172. }
  173. if ( this.attrib & 4 ) {
  174. this.offset = data.readMatrix();
  175. }
  176. this.pos = data.readVector3();
  177. this.dir = data.readVector3();
  178. this.axle = data.readVector3();
  179. this.radius = data.readFloat();
  180. this.suspensionRestLength = data.readFloat();
  181. };
  182. SEA3D.CarController.prototype = Object.create( SEA3D.RigidBodyBase.prototype );
  183. SEA3D.CarController.prototype.constructor = SEA3D.CarController;
  184. SEA3D.CarController.prototype.type = "carc";
  185. //
  186. // Constraints
  187. //
  188. SEA3D.Constraints = function ( name, data, sea3d ) {
  189. this.name = name;
  190. this.data = data;
  191. this.sea3d = sea3d;
  192. this.attrib = data.readUShort();
  193. this.disableCollisionsBetweenBodies = this.attrib & 1 != 0;
  194. this.targetA = sea3d.getObject( data.readUInt() );
  195. this.pointA = data.readVector3();
  196. if ( this.attrib & 2 ) {
  197. this.targetB = sea3d.getObject( data.readUInt() );
  198. this.pointB = data.readVector3();
  199. }
  200. };
  201. //
  202. // P2P Constraint
  203. //
  204. SEA3D.P2PConstraint = function ( name, data, sea3d ) {
  205. this.name = name;
  206. this.data = data;
  207. this.sea3d = sea3d;
  208. SEA3D.Constraints.call( this, name, data, sea3d );
  209. };
  210. SEA3D.P2PConstraint.prototype = Object.create( SEA3D.Constraints.prototype );
  211. SEA3D.P2PConstraint.prototype.constructor = SEA3D.P2PConstraint;
  212. SEA3D.P2PConstraint.prototype.type = "p2pc";
  213. //
  214. // Hinge Constraint
  215. //
  216. SEA3D.HingeConstraint = function ( name, data, sea3d ) {
  217. SEA3D.Constraints.call( this, name, data, sea3d );
  218. this.axisA = data.readVector3();
  219. if ( this.attrib & 1 ) {
  220. this.axisB = data.readVector3();
  221. }
  222. if ( this.attrib & 4 ) {
  223. this.limit = {
  224. low: data.readFloat(),
  225. high: data.readFloat(),
  226. softness: data.readFloat(),
  227. biasFactor: data.readFloat(),
  228. relaxationFactor: data.readFloat()
  229. };
  230. }
  231. if ( this.attrib & 8 ) {
  232. this.angularMotor = {
  233. velocity: data.readFloat(),
  234. impulse: data.readFloat()
  235. };
  236. }
  237. };
  238. SEA3D.HingeConstraint.prototype = Object.create( SEA3D.Constraints.prototype );
  239. SEA3D.HingeConstraint.prototype.constructor = SEA3D.HingeConstraint;
  240. SEA3D.HingeConstraint.prototype.type = "hnec";
  241. //
  242. // Cone Twist Constraint
  243. //
  244. SEA3D.ConeTwistConstraint = function ( name, data, sea3d ) {
  245. SEA3D.Constraints.call( this, name, data, sea3d );
  246. this.axisA = data.readVector3();
  247. if ( this.attrib & 1 ) {
  248. this.axisB = data.readVector3();
  249. }
  250. if ( this.attrib & 4 ) {
  251. this.limit = {
  252. swingSpan1: data.readFloat(),
  253. swingSpan2: data.readFloat(),
  254. twistSpan: data.readFloat(),
  255. softness: data.readFloat(),
  256. biasFactor: data.readFloat(),
  257. relaxationFactor: data.readFloat()
  258. };
  259. }
  260. };
  261. SEA3D.ConeTwistConstraint.prototype = Object.create( SEA3D.Constraints.prototype );
  262. SEA3D.ConeTwistConstraint.prototype.constructor = SEA3D.ConeTwistConstraint;
  263. SEA3D.ConeTwistConstraint.prototype.type = "ctwc";
  264. //
  265. // Extension
  266. //
  267. SEA3D.File.setExtension( function () {
  268. // PHYSICS
  269. this.addClass( SEA3D.Sphere );
  270. this.addClass( SEA3D.Box );
  271. this.addClass( SEA3D.Cone );
  272. this.addClass( SEA3D.Capsule );
  273. this.addClass( SEA3D.Cylinder );
  274. this.addClass( SEA3D.ConvexGeometry );
  275. this.addClass( SEA3D.TriangleGeometry );
  276. this.addClass( SEA3D.Compound );
  277. this.addClass( SEA3D.RigidBody );
  278. this.addClass( SEA3D.P2PConstraint );
  279. this.addClass( SEA3D.HingeConstraint );
  280. this.addClass( SEA3D.ConeTwistConstraint );
  281. this.addClass( SEA3D.CarController );
  282. } );