collider.monkey2 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. Namespace mojo3d
  2. #Import "native/internaledges.cpp"
  3. #Import "native/internaledges.h"
  4. Extern Private
  5. Function CreateInternalEdgeInfo( mesh:btBvhTriangleMeshShape )="bbBullet::createInternalEdgeInfo"
  6. Private
  7. Global emptyShape:=New btEmptyShape
  8. Public
  9. Class Collider Extends Component
  10. Const Type:=New ComponentType( "Collider",10,ComponentTypeFlags.Singleton )
  11. Method New( entity:Entity )
  12. Super.New( entity,Type )
  13. End
  14. Method New( entity:Entity,collider:Collider )
  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 _btshape Return _btshape
  27. _btshape=OnCreate()
  28. Return _btshape
  29. End
  30. Protected
  31. Method OnCreate:btCollisionShape() Abstract
  32. Method Invalidate()
  33. If Not _btshape Return
  34. _btshape.destroy()
  35. _btshape=Null
  36. Entity.RigidBody?.ColliderInvalidated()
  37. End
  38. function SetOrigin:btCollisionShape( shape:btCollisionShape,origin:Vec3f )
  39. If origin=Null Return shape
  40. Local tshape:=New btCompoundShape( False,1 )
  41. tshape.addChildShape( AffineMat4f.Translation( origin ),shape )
  42. Return tshape
  43. End
  44. Private
  45. Field _btshape:btCollisionShape
  46. End
  47. Class ConvexCollider Extends Collider
  48. Method New( Entity:Entity )
  49. Super.New( Entity )
  50. End
  51. Method New( entity:Entity,collider:ConvexCollider )
  52. Super.New( entity,collider )
  53. End
  54. End
  55. Class BoxCollider Extends ConvexCollider
  56. Method New( entity:Entity )
  57. Super.New( entity )
  58. Box=New Boxf( -1,1 )
  59. AddInstance()
  60. End
  61. Method New( entity:Entity,collider:BoxCollider )
  62. Super.New( entity,collider )
  63. Box=collider.Box
  64. AddInstance( collider )
  65. End
  66. [jsonify=1]
  67. Property Box:Boxf()
  68. Return _box
  69. Setter( box:Boxf )
  70. _box=box
  71. Invalidate()
  72. End
  73. Protected
  74. Method OnCopy:BoxCollider( entity:Entity ) Override
  75. local collider:=New BoxCollider( entity,Self )
  76. Return collider
  77. End
  78. Method OnCreate:btCollisionShape() Override
  79. Local shape:=New btBoxShape( _box.Size/2 )
  80. Return SetOrigin( shape,_box.Center )
  81. End
  82. Private
  83. Field _box:=New Boxf( -1,1 )
  84. End
  85. Class SphereCollider Extends ConvexCollider
  86. Method New( entity:Entity )
  87. Super.New( entity )
  88. AddInstance()
  89. End
  90. Method New( entity:Entity,collider:SphereCollider )
  91. Super.New( entity,collider )
  92. Radius=collider.Radius
  93. Origin=collider.Origin
  94. AddInstance( collider )
  95. End
  96. [jsonify=1]
  97. Property Radius:Float()
  98. Return _radius
  99. Setter( radius:Float )
  100. _radius=radius
  101. Invalidate()
  102. End
  103. [jsonify=1]
  104. Property Origin:Vec3f()
  105. Return _origin
  106. Setter( origin:Vec3f )
  107. _origin=origin
  108. Invalidate()
  109. End
  110. Protected
  111. Method OnCopy:SphereCollider( entity:Entity ) Override
  112. Local collider:=New SphereCollider( entity,Self )
  113. Return collider
  114. End
  115. Method OnCreate:btCollisionShape() Override
  116. Local shape:=New btSphereShape( _radius )
  117. return SetOrigin( shape,_origin )
  118. End
  119. Private
  120. Field _radius:Float=1
  121. Field _origin:Vec3f
  122. End
  123. Class CylinderCollider Extends ConvexCollider
  124. Method New( entity:Entity )
  125. Super.New( entity )
  126. AddInstance()
  127. End
  128. Method New( entity:Entity,collider:CylinderCollider )
  129. Super.New( entity,collider )
  130. Radius=collider.Radius
  131. Length=collider.Length
  132. Axis=collider.Axis
  133. Origin=collider.Origin
  134. AddInstance( collider )
  135. End
  136. [jsonify=1]
  137. Property Radius:Float()
  138. Return _radius
  139. Setter( radius:Float )
  140. _radius=radius
  141. Invalidate()
  142. End
  143. [jsonify=1]
  144. Property Length:Float()
  145. Return _length
  146. Setter( length:Float )
  147. _length=length
  148. Invalidate()
  149. End
  150. [jsonify=1]
  151. Property Axis:Axis()
  152. Return _axis
  153. Setter( axis:Axis )
  154. _axis=axis
  155. Invalidate()
  156. End
  157. [jsonify=1]
  158. Property Origin:Vec3f()
  159. Return _origin
  160. Setter( origin:Vec3f )
  161. _origin=origin
  162. Invalidate()
  163. End
  164. Protected
  165. Method OnCopy:CylinderCollider( entity:Entity ) Override
  166. Local collider:=New CylinderCollider( entity )
  167. collider.Radius=Radius
  168. collider.Length=Length
  169. collider.Axis=Axis
  170. collider.Origin=Origin
  171. Return collider
  172. End
  173. Method OnCreate:btCollisionShape() Override
  174. Local shape:btCollisionShape
  175. Select _axis
  176. case Axis.X
  177. shape=New btCylinderShapeX( New btVector3( _length/2,_radius,_radius ) )
  178. Case Axis.Y
  179. shape=New btCylinderShape ( New btVector3( _radius,_length/2,_radius ) )
  180. case Axis.Z
  181. shape=New btCylinderShapeZ( New btVector3( _radius,_radius,_length/2 ) )
  182. Default
  183. RuntimeError( "Invalid Cylinder Axis" )
  184. End
  185. Return SetOrigin( shape,_origin )
  186. End
  187. Private
  188. Field _radius:Float=0.5
  189. Field _length:Float=1.0
  190. Field _axis:Axis=geom.Axis.Y
  191. Field _origin:Vec3f
  192. End
  193. Class CapsuleCollider Extends ConvexCollider
  194. Method New( entity:Entity )
  195. Super.New( entity )
  196. AddInstance()
  197. End
  198. Method New( entity:Entity,collider:CapsuleCollider )
  199. Super.New( entity,collider )
  200. Radius=collider.Radius
  201. Length=collider.Length
  202. Axis=collider.Axis
  203. Origin=collider.Origin
  204. AddInstance( collider )
  205. End
  206. [jsonify=1]
  207. Property Radius:Float()
  208. Return _radius
  209. Setter( radius:Float )
  210. _radius=radius
  211. Invalidate()
  212. End
  213. [jsonify=1]
  214. Property Length:Float()
  215. Return _length
  216. Setter( length:Float )
  217. _length=length
  218. Invalidate()
  219. End
  220. [jsonify=1]
  221. Property Axis:Axis()
  222. Return _axis
  223. Setter( axis:Axis )
  224. _axis=axis
  225. Invalidate()
  226. End
  227. [jsonify=1]
  228. Property Origin:Vec3f()
  229. Return _origin
  230. Setter( origin:Vec3f )
  231. _origin=origin
  232. Invalidate()
  233. End
  234. Protected
  235. Method OnCopy:CapsuleCollider( entity:Entity ) Override
  236. Local collider:=New CapsuleCollider( entity,Self )
  237. Return collider
  238. End
  239. Method OnCreate:btCollisionShape() Override
  240. Local shape:btCollisionShape
  241. Select _axis
  242. Case Axis.X
  243. shape=New btCapsuleShapeX( _radius,_length )
  244. Case Axis.Y
  245. shape=New btCapsuleShape ( _radius,_length )
  246. Case Axis.Z
  247. shape=New btCapsuleShapeZ( _radius,_length )
  248. Default
  249. RuntimeError( "Invalid Capsule Axis" )
  250. End
  251. Return SetOrigin( shape,_origin )
  252. End
  253. Private
  254. Field _radius:Float=0.5
  255. Field _length:Float=1.0
  256. Field _axis:Axis=geom.Axis.Y
  257. Field _origin:Vec3f
  258. End
  259. Class ConeCollider Extends ConvexCollider
  260. Method New( entity:Entity )
  261. Super.New( entity )
  262. AddInstance()
  263. End
  264. Method New( entity:Entity,collider:ConeCollider )
  265. Super.New( entity,collider )
  266. Radius=collider.Radius
  267. Length=collider.Length
  268. Axis=collider.Axis
  269. Origin=collider.Origin
  270. AddInstance( collider )
  271. End
  272. [jsonify=1]
  273. Property Radius:Float()
  274. Return _radius
  275. Setter( radius:Float )
  276. _radius=radius
  277. Invalidate()
  278. End
  279. [jsonify=1]
  280. Property Length:Float()
  281. Return _length
  282. Setter( length:Float )
  283. _length=length
  284. Invalidate()
  285. End
  286. [jsonify=1]
  287. Property Axis:Axis()
  288. Return _axis
  289. Setter( axis:Axis )
  290. _axis=axis
  291. Invalidate()
  292. End
  293. [jsonify=1]
  294. Property Origin:Vec3f()
  295. Return _origin
  296. Setter( origin:Vec3f )
  297. _origin=origin
  298. Invalidate()
  299. End
  300. Protected
  301. Method OnCopy:ConeCollider( entity:Entity ) Override
  302. Local collider:=New ConeCollider( entity,Self )
  303. Return collider
  304. End
  305. Method OnCreate:btCollisionShape() Override
  306. Local shape:btCollisionShape
  307. Select _axis
  308. Case Axis.X
  309. shape=New btConeShapeX( _radius,_length )
  310. Case Axis.Y
  311. shape=New btConeShape ( _radius,_length )
  312. Case Axis.Z
  313. shape=New btConeShapeZ( _radius,_length )
  314. Default
  315. RuntimeError( "Invalid Cone Axis" )
  316. End
  317. Return SetOrigin( shape,_origin )
  318. End
  319. Private
  320. Field _radius:Float=0.5
  321. Field _length:Float=1.0
  322. Field _axis:Axis=geom.Axis.Y
  323. Field _origin:Vec3f
  324. End
  325. Class ConvexHullCollider Extends ConvexCollider
  326. Method New( entity:Entity )
  327. Super.New( entity )
  328. AddInstance()
  329. End
  330. Method New( entity:Entity,collider:ConvexHullCollider )
  331. Super.New( entity,collider )
  332. Mesh=collider.Mesh
  333. AddInstance( collider )
  334. End
  335. [jsonify=1]
  336. Property Mesh:Mesh()
  337. Return _mesh
  338. Setter( mesh:Mesh )
  339. If mesh=_mesh Return
  340. _mesh=mesh
  341. Invalidate()
  342. End
  343. Private
  344. Field _mesh:Mesh
  345. Method OnCreate:btCollisionShape() Override
  346. If Not _mesh Return emptyShape
  347. Local vertices:=_mesh.GetVertices()
  348. Local points:=New btScalar[vertices.Length*3]
  349. For Local i:=0 Until vertices.Length
  350. libc.memcpy( points.Data+i*3,Varptr( vertices[i].position ),12 )
  351. Next
  352. Local shape:=New btConvexHullShape( points.Data,points.Length/3,12 )
  353. Return shape
  354. End
  355. End
  356. Class ConcaveCollider Extends Collider
  357. Method New( entity:Entity )
  358. Super.New( entity )
  359. End
  360. Method New( entity:Entity,collider:ConcaveCollider )
  361. Super.New( entity,collider )
  362. End
  363. End
  364. Class MeshCollider Extends ConcaveCollider
  365. Method New( entity:Entity )
  366. Super.New( entity )
  367. AddInstance()
  368. End
  369. Method New( entity:Entity,collider:MeshCollider )
  370. Super.New( entity,collider )
  371. UseInternalEdgeInfo=collider.UseInternalEdgeInfo
  372. Mesh=collider.Mesh
  373. AddInstance( collider )
  374. End
  375. [jsonify=1]
  376. Property UseInternalEdgeInfo:Bool()
  377. Return _internalEdgeInfo
  378. Setter( internalEdgeInfo:Bool )
  379. If internalEdgeInfo=_internalEdgeInfo Return
  380. _internalEdgeInfo=internalEdgeInfo
  381. Invalidate()
  382. End
  383. [jsonify=1]
  384. Property Mesh:Mesh()
  385. Return _mesh
  386. Setter( mesh:Mesh )
  387. If mesh=_mesh Return
  388. _mesh=mesh
  389. Invalidate()
  390. End
  391. Protected
  392. Method OnCopy:MeshCollider( entity:Entity ) Override
  393. Local collider:=New MeshCollider( entity,Self )
  394. Return collider
  395. End
  396. Method OnCreate:btCollisionShape() Override
  397. If Not _mesh Return emptyShape
  398. Local vertices:=_mesh.GetVertices()
  399. _vertices=New btScalar[vertices.Length*3]
  400. For Local i:=0 Until vertices.Length
  401. libc.memcpy( _vertices.Data+i*3,Varptr( vertices[i].position ),12 )
  402. Next
  403. Local indices:=_mesh.GetAllIndices()
  404. _indices=New Int[indices.Length]
  405. libc.memcpy( _indices.Data,indices.Data,_indices.Length*4 )
  406. _btmesh=New btTriangleIndexVertexArray( _indices.Length/3,_indices.Data,12,_vertices.Length,_vertices.Data,12 )
  407. Local shape:=New btBvhTriangleMeshShape( _btmesh,True,True )
  408. If _internalEdgeInfo CreateInternalEdgeInfo( shape )
  409. Return shape
  410. End
  411. Private
  412. Field _mesh:Mesh
  413. Field _vertices:btScalar[]
  414. Field _indices:Int[]
  415. Field _btmesh:btTriangleIndexVertexArray
  416. Field _internalEdgeInfo:Bool
  417. End
  418. Class TerrainCollider Extends ConcaveCollider
  419. Method New( entity:Entity )
  420. Super.New( entity )
  421. Bounds=New Boxf( -1,1 )
  422. AddInstance()
  423. End
  424. Method New( entity:Entity,collider:TerrainCollider )
  425. Super.New( entity,collider )
  426. Heightmap=collider.Heightmap
  427. Bounds=collider.Bounds
  428. UseDiamondSubdivision=collider.UseDiamondSubdivision
  429. UseZigzagSubdivision=collider.UseZigzagSubdivision
  430. AddInstance( collider )
  431. End
  432. Property Heightmap:Pixmap()
  433. Return _heightmap
  434. Setter( heightmap:Pixmap )
  435. If heightmap=_heightmap Return
  436. Assert( heightmap.Format=PixelFormat.I8,"Heightmap must be in I8 format" )
  437. _heightmap=heightmap
  438. Invalidate()
  439. _shape=Null
  440. End
  441. Property Bounds:Boxf()
  442. Return _bounds
  443. Setter( bounds:Boxf )
  444. _bounds=bounds
  445. Invalidate()
  446. End
  447. Property UseDiamondSubdivision:Bool()
  448. Return _diamondSubdiv
  449. Setter( diamondSubdiv:Bool )
  450. _diamondSubdiv=diamondSubdiv
  451. If _shape _shape.setUseDiamondSubdivision( _diamondSubdiv )
  452. End
  453. Property UseZigzagSubdivision:Bool()
  454. Return _zigzagSubdiv
  455. Setter( zigzagSubdiv:Bool )
  456. _zigzagSubdiv=zigzagSubdiv
  457. If _shape _shape.setUseZigzagSubdivision( _zigzagSubdiv )
  458. End
  459. Protected
  460. Method OnCreate:btCollisionShape() Override
  461. If Not _shape
  462. _shape=New btHeightfieldTerrainShape( _heightmap.Width,_heightmap.Height,_heightmap.Data,1.0/255.0,0.0,1.0,1,PHY_UCHAR,False )
  463. _shape.setUseDiamondSubdivision( _diamondSubdiv )
  464. _shape.setUseZigzagSubdivision( _zigzagSubdiv )
  465. Endif
  466. _shape.setLocalScaling( New Vec3f( _bounds.Width/_heightmap.Width,_bounds.Height,_bounds.Depth/_heightmap.Height ) )
  467. Return SetOrigin( _shape,_bounds.Center )
  468. End
  469. Private
  470. Field _heightmap:Pixmap
  471. Field _bounds:Boxf
  472. Field _diamondSubdiv:Bool
  473. Field _zigzagSubdiv:Bool
  474. Field _shape:btHeightfieldTerrainShape
  475. End