joint.monkey2 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. Namespace mojo3d
  2. Private
  3. 'Global nullBody:btRigidBody=New btRigidBody( 0,Null,Null,Null )
  4. Public
  5. Class Joint Extends Component
  6. Const ERP:=0 'error reduction parameter - http://bulletphysics.org/mediawiki-1.5.8/index.php/Definitions
  7. Const STOP_ERP:=1
  8. Const CFM:=2
  9. Const STOP_CFM:=3 'constraint force mixing
  10. Const Type:=New ComponentType( "Joint",-20,ComponentTypeFlags.Singleton )
  11. Method New( entity:Entity )
  12. Super.New( entity,Type )
  13. End
  14. Method New( entity:Entity,joint:Joint )
  15. Super.New( entity,Type )
  16. End
  17. Method SetParam( param:Int,value:Float )
  18. If _btconstraint _btconstraint.setParam( param+1,value )
  19. _params[param]=value
  20. End
  21. Method GetParam:Float( param:Int )
  22. Return _params[param]
  23. End
  24. Protected
  25. Field _rvisible:Bool
  26. Field _params:=New Float[4]
  27. Field _btconstraint:btTypedConstraint
  28. Method OnCreate() Abstract
  29. Method OnBeginUpdate() Override
  30. Validate()
  31. End
  32. Method OnDestroy() Override
  33. If Not _btconstraint Return
  34. Entity.Scene.World.btWorld.removeConstraint( _btconstraint )
  35. _btconstraint.destroy()
  36. _btconstraint=Null
  37. End
  38. Method Validate()
  39. Local rvisible:=Entity.ReallyVisible
  40. If rvisible=_rvisible Return
  41. If rvisible
  42. If Not _btconstraint
  43. OnCreate()
  44. ' _btconstraint.setParam( ERP+1,_params[ERP] )
  45. ' _btconstraint.setParam( STOP_ERP+1,_params[STOP_ERP] )
  46. ' _btconstraint.setParam( CFM+1,_params[CFM] )
  47. ' _btconstraint.setParam( STOP_CFM+1,_params[STOP_CFM] )
  48. Endif
  49. Entity.Scene.World.btWorld.addConstraint( _btconstraint )
  50. Else
  51. If _btconstraint Entity.Scene.World.btWorld.removeConstraint( _btconstraint )
  52. Endif
  53. _rvisible=rvisible
  54. End
  55. End
  56. Class BallSocketJoint Extends Joint
  57. Method New( entity:Entity )
  58. Super.New( entity )
  59. AddInstance()
  60. End
  61. Method New( entity:Entity,joint:BallSocketJoint )
  62. Super.New( entity,joint )
  63. ConnectedBody=joint.ConnectedBody
  64. Pivot=joint.Pivot
  65. AddInstance( joint )
  66. End
  67. [jsonify=1]
  68. Property ConnectedBody:RigidBody()
  69. Return _connected
  70. Setter( body:RigidBody )
  71. _connected=body
  72. End
  73. [jsonify=1]
  74. Property Pivot:Vec3f()
  75. Return _pivot1
  76. Setter( pivot:Vec3f )
  77. _pivot1=pivot
  78. End
  79. Protected
  80. Field _connected:RigidBody
  81. Field _pivot1:Vec3f
  82. Method OnCreate() Override
  83. Local btBody1:=Entity.GetComponent<RigidBody>().btBody
  84. Assert( btBody1,"BallSocketJoint: No rigid body" ) 'todo: fail nicely
  85. If _connected
  86. Local btBody2:=_connected.btBody
  87. Assert( btBody2,"BallSocketJoint: No rigid body" ) 'todo: fail nicely
  88. Local tform:=_connected.Entity.InverseMatrix * Entity.Matrix
  89. Local pivot2:=tform * _pivot1
  90. _btconstraint=New btPoint2PointConstraint( btBody1,btBody2,_pivot1,pivot2 )
  91. Else
  92. _btconstraint=New btPoint2PointConstraint( btBody1,_pivot1 )
  93. End
  94. End
  95. End
  96. Class HingeJoint Extends Joint
  97. Method New( entity:Entity )
  98. Super.New( entity )
  99. Axis=New Vec3f( 0,1,0 )
  100. MinAngle=180
  101. MaxAngle=-180
  102. AddInstance()
  103. End
  104. Method New( entity:Entity,joint:HingeJoint )
  105. Super.New( entity,joint )
  106. ConnectedBody=joint.ConnectedBody
  107. Pivot=joint.Pivot
  108. Axis=joint.Axis
  109. MinAngle=joint.MinAngle
  110. MaxAngle=joint.MaxAngle
  111. AddInstance( joint )
  112. End
  113. [jsonify=1]
  114. Property ConnectedBody:RigidBody()
  115. Return _connected
  116. Setter( body:RigidBody )
  117. _connected=body
  118. End
  119. [jsonify=1]
  120. Property Pivot:Vec3f()
  121. Return _pivot1
  122. Setter( pivot:Vec3f )
  123. _pivot1=pivot
  124. End
  125. [jsonify=1]
  126. Property Axis:Vec3f()
  127. Return _axis1
  128. Setter( axis:Vec3f )
  129. _axis1=axis
  130. End
  131. Property MinAngle:Float()
  132. Return _minAngle
  133. Setter( angle:Float )
  134. _minAngle=angle
  135. SetLimits()
  136. End
  137. Property MaxAngle:Float()
  138. Return _maxAngle
  139. Setter( angle:Float )
  140. _maxAngle=angle
  141. SetLimits()
  142. End
  143. Protected
  144. Field _connected:RigidBody
  145. Field _pivot1:Vec3f
  146. Field _axis1:Vec3f
  147. Field _minAngle:Float
  148. Field _maxAngle:Float
  149. Method SetLimits()
  150. If Not _btconstraint Return
  151. Cast<btHingeConstraint>(_btconstraint).setLimit( _minAngle*Pi/180,_maxAngle*Pi/180 )
  152. End
  153. Method OnCreate() Override
  154. Local btBody1:=Entity.GetComponent<RigidBody>().btBody
  155. Assert( btBody1,"HingeJoint: No rigid body" ) 'todo: fail nicely
  156. If _connected
  157. Local btBody2:=_connected.btBody
  158. Assert( btBody2,"HingeJoint: No rigid body" ) 'todo: fail nicely
  159. Local tform:=_connected.Entity.InverseMatrix * Entity.Matrix
  160. Local pivot2:=tform * _pivot1
  161. Local axis2:=tform.m * _axis1
  162. _btconstraint=New btHingeConstraint( btBody1,btBody2,_pivot1,pivot2,_axis1,axis2 )
  163. Else
  164. _btconstraint=New btHingeConstraint( btBody1,_pivot1,_axis1 )
  165. End
  166. SetLimits()
  167. End
  168. End
  169. Class FixedJoint Extends Joint
  170. Method New( entity:Entity )
  171. Super.New( entity )
  172. AddInstance()
  173. End
  174. Method New( entity:Entity,joint:HingeJoint )
  175. Super.New( entity,joint )
  176. ConnectedBody=joint.ConnectedBody
  177. AddInstance( joint )
  178. End
  179. [jsonify=1]
  180. Property ConnectedBody:RigidBody()
  181. Return _connected
  182. Setter( body:RigidBody )
  183. _connected=body
  184. End
  185. Protected
  186. Field _connected:RigidBody
  187. Method OnCreate() Override
  188. Local btBody1:=Entity.GetComponent<RigidBody>()?.btBody
  189. Assert( btBody1,"FixedJoint: No rigid body" ) 'todo: fail nicely
  190. Local btBody2:=_connected?.btBody
  191. Assert( btBody2,"FixedJoint: No connected rigid body" ) 'todo: fail nicely
  192. Local tform:=_connected.Entity.InverseMatrix * Entity.Matrix
  193. _btconstraint=New btFixedConstraint( btBody1,btBody2,New AffineMat4f,tform )
  194. End
  195. End