collider.monkey2 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. Namespace mojo3d
  2. #Import "native/internaledges.cpp"
  3. #Import "native/internaledges.h"
  4. Extern Private
  5. Function CreateInternalEdgeInfo( mesh:btBvhTriangleMeshShape )="bbBullet::createInternalEdgeInfo"
  6. Public
  7. Class Entity Extension
  8. Property Collider:Collider()
  9. Return Cast<Collider>( GetComponent( Collider.Type ) )
  10. End
  11. End
  12. Class Collider Extends Component
  13. Const Type:=New ComponentType( "Collider",10,ComponentTypeFlags.Singleton )
  14. Method New( entity:Entity )
  15. Super.New( entity,Type )
  16. End
  17. Property Margin:Float()
  18. Return Validate().getMargin()
  19. Setter( margin:Float )
  20. Validate().setMargin( margin )
  21. End
  22. Method CalculateLocalInertia:Vec3f( mass:Float )
  23. Return Validate().calculateLocalInertia( mass )
  24. End
  25. Method Validate:btCollisionShape()
  26. If Not _btshape _btshape=OnCreate()
  27. Return _btshape
  28. End
  29. Protected
  30. Method OnCreate:btCollisionShape() Abstract
  31. Method Invalidate()
  32. If Not _btshape Return
  33. _btshape.destroy()
  34. _btshape=Null
  35. Entity.RigidBody?.ColliderInvalidated()
  36. End
  37. function SetOrigin:btCollisionShape( shape:btCollisionShape,origin:Vec3f )
  38. If origin=Null Return shape
  39. Local tshape:=New btCompoundShape( False,1 )
  40. tshape.addChildShape( AffineMat4f.Translation( origin ),shape )
  41. Return tshape
  42. End
  43. Private
  44. Field _btshape:btCollisionShape
  45. End
  46. Class ConvexCollider Extends Collider
  47. Method New( Entity:Entity )
  48. Super.New( Entity )
  49. End
  50. End
  51. Class BoxCollider Extends ConvexCollider
  52. Method New( Entity:Entity )
  53. Super.New( Entity )
  54. Box=New Boxf( -1,1 )
  55. End
  56. Property Box:Boxf()
  57. Return _box
  58. Setter( box:Boxf )
  59. _box=box
  60. Invalidate()
  61. End
  62. Protected
  63. Method OnCopy:BoxCollider( entity:Entity ) Override
  64. Local collider:=New BoxCollider( entity )
  65. collider.Box=Box
  66. Return collider
  67. End
  68. Method OnCreate:btCollisionShape() Override
  69. Local shape:=New btBoxShape( _box.Size/2 )
  70. Return SetOrigin( shape,_box.Center )
  71. End
  72. Private
  73. Field _box:=New Boxf( -1,1 )
  74. End
  75. Class SphereCollider Extends ConvexCollider
  76. Method New( Entity:Entity )
  77. Super.New( Entity )
  78. End
  79. Property Radius:Float()
  80. Return _radius
  81. Setter( radius:Float )
  82. _radius=radius
  83. Invalidate()
  84. End
  85. Property Origin:Vec3f()
  86. Return _origin
  87. Setter( origin:Vec3f )
  88. _origin=origin
  89. Invalidate()
  90. End
  91. Protected
  92. Method OnCopy:SphereCollider( entity:Entity ) Override
  93. Local collider:=New SphereCollider( entity )
  94. collider.Radius=Radius
  95. collider.Origin=Origin
  96. Return collider
  97. End
  98. Method OnCreate:btCollisionShape() Override
  99. Local shape:=New btSphereShape( _radius )
  100. return SetOrigin( shape,_origin )
  101. End
  102. Private
  103. Field _radius:Float=1
  104. Field _origin:Vec3f
  105. End
  106. Class CylinderCollider Extends ConvexCollider
  107. Method New( entity:Entity )
  108. Super.New( entity )
  109. End
  110. Property Radius:Float()
  111. Return _radius
  112. Setter( radius:Float )
  113. _radius=radius
  114. Invalidate()
  115. End
  116. Property Length:Float()
  117. Return _length
  118. Setter( length:Float )
  119. _length=length
  120. Invalidate()
  121. End
  122. Property Axis:Axis()
  123. Return _axis
  124. Setter( axis:Axis )
  125. _axis=axis
  126. Invalidate()
  127. End
  128. Property Origin:Vec3f()
  129. Return _origin
  130. Setter( origin:Vec3f )
  131. _origin=origin
  132. Invalidate()
  133. End
  134. Protected
  135. Method OnCopy:CylinderCollider( entity:Entity ) Override
  136. Local collider:=New CylinderCollider( entity )
  137. collider.Radius=Radius
  138. collider.Length=Length
  139. collider.Axis=Axis
  140. collider.Origin=Origin
  141. Return collider
  142. End
  143. Method OnCreate:btCollisionShape() Override
  144. Local shape:btCollisionShape
  145. Select _axis
  146. case Axis.X
  147. shape=New btCylinderShapeX( New btVector3( _length/2,_radius,_radius ) )
  148. Case Axis.Y
  149. shape=New btCylinderShape ( New btVector3( _radius,_length/2,_radius ) )
  150. case Axis.Z
  151. shape=New btCylinderShapeZ( New btVector3( _radius,_radius,_length/2 ) )
  152. Default
  153. RuntimeError( "Invalid Cylinder Axis" )
  154. End
  155. Return SetOrigin( shape,_origin )
  156. End
  157. Private
  158. Field _radius:Float=0.5
  159. Field _length:Float=1.0
  160. Field _axis:Axis=geom.Axis.Y
  161. Field _origin:Vec3f
  162. End
  163. Class CapsuleCollider Extends ConvexCollider
  164. Method New( entity:Entity )
  165. Super.New( entity )
  166. End
  167. Property Radius:Float()
  168. Return _radius
  169. Setter( radius:Float )
  170. _radius=radius
  171. Invalidate()
  172. End
  173. Property Length:Float()
  174. Return _length
  175. Setter( length:Float )
  176. _length=length
  177. Invalidate()
  178. End
  179. Property Axis:Axis()
  180. Return _axis
  181. Setter( axis:Axis )
  182. _axis=axis
  183. Invalidate()
  184. End
  185. Property Origin:Vec3f()
  186. Return _origin
  187. Setter( origin:Vec3f )
  188. _origin=origin
  189. Invalidate()
  190. End
  191. Protected
  192. Method OnCopy:CapsuleCollider( entity:Entity ) Override
  193. Local collider:=New CapsuleCollider( entity )
  194. collider.Radius=Radius
  195. collider.Length=Length
  196. collider.Axis=Axis
  197. collider.Origin=Origin
  198. Return collider
  199. End
  200. Method OnCreate:btCollisionShape() Override
  201. Local shape:btCollisionShape
  202. Select _axis
  203. Case Axis.X
  204. shape=New btCapsuleShapeX( _radius,_length )
  205. Case Axis.Y
  206. shape=New btCapsuleShape ( _radius,_length )
  207. Case Axis.Z
  208. shape=New btCapsuleShapeZ( _radius,_length )
  209. Default
  210. RuntimeError( "Invalid Capsule Axis" )
  211. End
  212. Return SetOrigin( shape,_origin )
  213. End
  214. Private
  215. Field _radius:Float=0.5
  216. Field _length:Float=1.0
  217. Field _axis:Axis=geom.Axis.Y
  218. Field _origin:Vec3f
  219. End
  220. Class ConeCollider Extends ConvexCollider
  221. Method New( entity:Entity )
  222. Super.New( entity )
  223. End
  224. Property Radius:Float()
  225. Return _radius
  226. Setter( radius:Float )
  227. _radius=radius
  228. Invalidate()
  229. End
  230. Property Length:Float()
  231. Return _length
  232. Setter( length:Float )
  233. _length=length
  234. Invalidate()
  235. End
  236. Property Axis:Axis()
  237. Return _axis
  238. Setter( axis:Axis )
  239. _axis=axis
  240. Invalidate()
  241. End
  242. Property Origin:Vec3f()
  243. Return _origin
  244. Setter( origin:Vec3f )
  245. _origin=origin
  246. Invalidate()
  247. End
  248. Protected
  249. Method OnCopy:ConeCollider( entity:Entity ) Override
  250. Local collider:=New ConeCollider( entity )
  251. collider.Radius=Radius
  252. collider.Length=Length
  253. collider.Axis=Axis
  254. collider.Origin=Origin
  255. Return collider
  256. End
  257. Method OnCreate:btCollisionShape() Override
  258. Local shape:btCollisionShape
  259. Select _axis
  260. Case Axis.X
  261. shape=New btConeShapeX( _radius,_length )
  262. Case Axis.Y
  263. shape=New btConeShape ( _radius,_length )
  264. Case Axis.Z
  265. shape=New btConeShapeZ( _radius,_length )
  266. Default
  267. RuntimeError( "Invalid Cone Axis" )
  268. End
  269. Return SetOrigin( shape,_origin )
  270. End
  271. Private
  272. Field _radius:Float=0.5
  273. Field _length:Float=1.0
  274. Field _axis:Axis=geom.Axis.Y
  275. Field _origin:Vec3f
  276. End
  277. Class ConcaveCollider Extends Collider
  278. Method New( entity:Entity )
  279. Super.New( entity )
  280. End
  281. End
  282. Class MeshCollider Extends ConcaveCollider
  283. Method New( entity:Entity )
  284. Super.New( entity )
  285. End
  286. Property Mesh:Mesh()
  287. Return _mesh
  288. Setter( mesh:Mesh )
  289. _mesh=mesh
  290. Invalidate()
  291. End
  292. Protected
  293. Method OnCopy:MeshCollider( entity:Entity ) Override
  294. Local collider:=New MeshCollider( entity )
  295. collider.Mesh=Mesh
  296. Return collider
  297. End
  298. Method OnCreate:btCollisionShape() Override
  299. Local vertices:=_mesh.GetVertices()
  300. _vertices=New btScalar[vertices.Length*3]
  301. For Local i:=0 Until vertices.Length
  302. _vertices[i*3]=vertices[i].position.x
  303. _vertices[i*3+1]=vertices[i].position.y
  304. _vertices[i*3+2]=vertices[i].position.z
  305. Next
  306. Local indices:=_mesh.GetAllIndices()
  307. _indices=New Int[indices.Length]
  308. For Local i:=0 Until indices.Length Step 3
  309. _indices[i+0]=indices[1]
  310. _indices[i+1]=indices[i+1]
  311. _indices[i+2]=indices[i+2]
  312. Next
  313. _btmesh=New btTriangleIndexVertexArray( _indices.Length/3,_indices.Data,12,_vertices.Length,_vertices.Data,12 )
  314. Local shape:=New btBvhTriangleMeshShape( _btmesh,True,True )
  315. ' CreateInternalEdgeInfo( shape )
  316. Return shape
  317. End
  318. Private
  319. Field _mesh:Mesh
  320. Field _vertices:btScalar[]
  321. 'Field _vertices:btVector3[]
  322. Field _indices:Int[]
  323. Field _btmesh:btTriangleIndexVertexArray
  324. End
  325. #rem
  326. Class TerrainCollider Extends ConcaveCollider
  327. Method New( box:Boxf,data:Pixmap )
  328. Local shape:=New btHeightfieldTerrainShape( data.Width,data.Height,data.Data,1.0/255.0,0.0,1.0,1,PHY_UCHAR,False )
  329. shape.setUseDiamondSubdivision( True )
  330. _btshape=shape
  331. _btshape.setLocalScaling( New Vec3f( box.Width/data.Width,box.Height,box.Depth/data.Height ) )
  332. SetOrigin( box.Center )
  333. End
  334. End
  335. #end