constraint.monkey2 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. Namespace mojo3d
  2. Class Constraint Extends Component
  3. Const ERP:=0 'error reduction parameter - http://bulletphysics.org/mediawiki-1.5.8/index.php/Definitions
  4. Const STOP_ERP:=1
  5. Const CFM:=2
  6. Const STOP_CFM:=3 'constraint force mixing
  7. Const Type:=New ComponentType( "Constraint",-20,ComponentTypeFlags.Singleton )
  8. Method New( entity:Entity )
  9. Super.New( entity,Type )
  10. End
  11. Method New( entity:Entity,constraint:Constraint )
  12. Super.New( entity,Type )
  13. End
  14. Method SetParam( param:Int,value:Float )
  15. If _btconstraint _btconstraint.setParam( param+1,value )
  16. _params[param]=value
  17. End
  18. Method GetParam:Float( param:Int )
  19. Return _params[param]
  20. End
  21. Protected
  22. Field _rvisible:Bool
  23. Field _params:=New Float[4]
  24. Field _btconstraint:btTypedConstraint
  25. Method OnCreate() Abstract
  26. Method OnBeginUpdate() Override
  27. Validate()
  28. End
  29. Method OnDestroy() Override
  30. If Not _btconstraint Return
  31. Entity.Scene.World.btWorld.removeConstraint( _btconstraint )
  32. _btconstraint.destroy()
  33. _btconstraint=Null
  34. End
  35. Method Validate()
  36. Local rvisible:=Entity.ReallyVisible
  37. If rvisible=_rvisible Return
  38. If rvisible
  39. If Not _btconstraint
  40. OnCreate()
  41. ' _btconstraint.setParam( ERP+1,_params[ERP] )
  42. ' _btconstraint.setParam( STOP_ERP+1,_params[STOP_ERP] )
  43. ' _btconstraint.setParam( CFM+1,_params[CFM] )
  44. ' _btconstraint.setParam( STOP_CFM+1,_params[STOP_CFM] )
  45. Endif
  46. Entity.Scene.World.btWorld.addConstraint( _btconstraint )
  47. Else
  48. If _btconstraint Entity.Scene.World.btWorld.removeConstraint( _btconstraint )
  49. Endif
  50. _rvisible=rvisible
  51. End
  52. End
  53. Class PointToPointConstraint Extends Constraint
  54. Method New( entity:Entity )
  55. Super.New( entity )
  56. AddInstance()
  57. End
  58. Method New( entity:Entity,constraint:PointToPointConstraint )
  59. Super.New( entity,constraint )
  60. Pivot=constraint.Pivot
  61. ConnectedBody=constraint.ConnectedBody
  62. ConnectedPivot=constraint.ConnectedPivot
  63. AddInstance( constraint )
  64. End
  65. [jsonify=1]
  66. Property Pivot:Vec3f()
  67. Return _pivot1
  68. Setter( pivot:Vec3f )
  69. _pivot1=pivot
  70. End
  71. [jsonify=1]
  72. Property ConnectedBody:RigidBody()
  73. Return _connected
  74. Setter( body:RigidBody )
  75. _connected=body
  76. End
  77. [jsonify=1]
  78. Property ConnectedPivot:Vec3f()
  79. Return _pivot2
  80. Setter( pivot:Vec3f )
  81. _pivot2=pivot
  82. End
  83. Protected
  84. Field _connected:RigidBody
  85. Field _pivot1:Vec3f
  86. Field _pivot2:Vec3f
  87. Method OnCreate() Override
  88. Local btBody1:=Entity.GetComponent<RigidBody>().btBody
  89. Assert( btBody1,"PointToPointConstraint: No rigid body" )
  90. If _connected
  91. Local btBody2:=_connected.btBody
  92. Assert( btBody2,"PointToPointConstraint: No rigid body" )
  93. _btconstraint=New btPoint2PointConstraint( btBody1,btBody2,_pivot1,_pivot2 )
  94. Else
  95. _btconstraint=New btPoint2PointConstraint( btBody1,_pivot1 )
  96. End
  97. End
  98. End