RigidBody.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. ----------------------------------------------------------------------------------------------------
  2. --
  3. -- Copyright (c) Contributors to the Open 3D Engine Project.
  4. -- For complete copyright and license terms please see the LICENSE at the root of this distribution.
  5. --
  6. -- SPDX-License-Identifier: Apache-2.0 OR MIT
  7. --
  8. --
  9. --
  10. ----------------------------------------------------------------------------------------------------
  11. RigidBody = {
  12. type = "RigidBody",
  13. MapVisMask = 0,
  14. Properties = {
  15. soclasses_SmartObjectClass = "",
  16. bAutoGenAIHidePts = 0,
  17. objModel = "Objects/box.cgf",
  18. Density = 5000,
  19. Mass = 1,
  20. bResting = 1, -- If rigid body is originally in resting state.
  21. bVisible = 1, -- If rigid body is originally visible.
  22. bRigidBodyActive = 1, -- If rigid body is originally created OR will be created only on OnActivate.
  23. bActivateOnRocketDamage = 0, -- Activate when a rocket hit the entity.
  24. Impulse = {X=0,Y=0,Z=0}, -- Impulse to apply at event.
  25. max_time_step = 0.01,
  26. sleep_speed = 0.04,
  27. damping = 0,
  28. water_damping = 1.5,
  29. water_resistance = 0,
  30. },
  31. temp_vec={x=0,y=0,z=0},
  32. PhysParams = { mass=0,density=0 },
  33. updateTime = 500,
  34. gravityUpdate = 0,
  35. Editor={
  36. Icon = "physicsobject.bmp",
  37. IconOnTop=1,
  38. },
  39. }
  40. -------------------------------------------------------
  41. function RigidBody:OnInit()
  42. self.ModelName = "";
  43. self.Mass = 0;
  44. self:OnReset();
  45. -- System.Log( "here1" );
  46. end
  47. -------------------------------------------------------
  48. function RigidBody:OnReset()
  49. --self:NetPresent(nil);
  50. if (self.ModelName ~= self.Properties.objModel or self.Mass ~= self.Properties.Mass) then
  51. self.Mass = self.Properties.Mass;
  52. self.ModelName = self.Properties.objModel;
  53. self:LoadObject( 0,self.ModelName );
  54. end
  55. local Properties = self.Properties;
  56. if (Properties.bVisible == 0) then
  57. self:DrawSlot( 0,0 );
  58. else
  59. self:DrawSlot( 0,1 );
  60. end
  61. local physType;
  62. if (self.Properties.bRigidBodyActive == 1) then
  63. physType = PE_RIGID;
  64. self.PhysParams.density = Properties.Density;
  65. self.PhysParams.mass = Properties.Mass;
  66. else
  67. physType = PE_STATIC;
  68. end
  69. self:Physicalize( 0,physType,self.PhysParams );
  70. self:SetPhysicParams(PHYSICPARAM_SIMULATION, self.Properties );
  71. self:SetPhysicParams(PHYSICPARAM_BUOYANCY, self.Properties );
  72. if (self.Properties.bResting == 0) then
  73. self:AwakePhysics(1);
  74. else
  75. self:AwakePhysics(0);
  76. end
  77. -- Mark AI hideable flag.
  78. if (self.Properties.bAutoGenAIHidePts == 1) then
  79. self:SetFlags(ENTITY_FLAG_AI_HIDEABLE, 0); -- set
  80. else
  81. self:SetFlags(ENTITY_FLAG_AI_HIDEABLE, 2); -- remove
  82. end
  83. end
  84. -------------------------------------------------------
  85. function RigidBody:OnPropertyChange()
  86. self:OnReset();
  87. end
  88. -------------------------------------------------------
  89. function RigidBody:OnContact( player )
  90. self:Event_OnTouch( player );
  91. end
  92. -------------------------------------------------------
  93. function RigidBody:OnDamage( hit )
  94. --System.LogToConsole( "On Damage" );
  95. if( hit.ipart ) then
  96. self:AddImpulse( hit.ipart, hit.pos, hit.dir, hit.impact_force_mul );
  97. -- else
  98. -- self:AddImpulse( -1, hit.pos, hit.dir, hit.impact_force_mul );
  99. end
  100. if(self.Properties.bActivateOnRocketDamage)then
  101. if(hit.explosion)then
  102. self:AwakePhysics(1);
  103. end
  104. end
  105. end
  106. -------------------------------------------------------
  107. function RigidBody:OnShutDown()
  108. end
  109. -------------------------------------------------------
  110. function RigidBody:OnTimer()
  111. -- System.Log("RigidBody OnTimer");
  112. -- self:SetTimer( 0,1000 );
  113. end
  114. function RigidBody:OnUpdate()
  115. --FIXME: all this timing stuff will be replaced by OnTimer once it will work again
  116. self.gravityUpdate = self.gravityUpdate + _frametime;
  117. if (self.gravityUpdate < 0.5) then
  118. return;
  119. end
  120. self.gravityUpdate = 0.0;
  121. EntityUpdateGravity(self);
  122. end
  123. -------------------------------------------------------
  124. -- Input events
  125. -------------------------------------------------------
  126. function RigidBody:Event_AddImpulse(sender)
  127. self.temp_vec.x=self.Properties.Impulse.X;
  128. self.temp_vec.y=self.Properties.Impulse.Y;
  129. self.temp_vec.z=self.Properties.Impulse.Z;
  130. self:AddImpulse(0,nil,self.temp_vec,1);
  131. end
  132. -------------------------------------------------------
  133. function RigidBody:Event_Activate(sender)
  134. --create rigid body
  135. self:CreateRigidBody( self.Properties.Density,self.Properties.Mass,0 );
  136. self:Activate(1);
  137. self:AwakePhysics(1);
  138. end
  139. -------------------------------------------------------
  140. function RigidBody:Event_Show(sender)
  141. self:DrawSlot( 0,1 );
  142. end
  143. -------------------------------------------------------
  144. function RigidBody:Event_Hide(sender)
  145. self:DrawSlot( 0,0 );
  146. end
  147. -------------------------------------------------------
  148. -- Output events
  149. -------------------------------------------------------
  150. function RigidBody:Event_OnTouch(sender)
  151. BroadcastEvent( self,"OnTouch" );
  152. end
  153. RigidBody.FlowEvents =
  154. {
  155. Inputs =
  156. {
  157. Activate = { RigidBody.Event_Activate, "bool" },
  158. AddImpulse = { RigidBody.Event_AddImpulse, "bool" },
  159. Hide = { RigidBody.Event_Hide, "bool" },
  160. Show = { RigidBody.Event_Show, "bool" },
  161. OnTouch = { RigidBody.Event_OnTouch, "bool" },
  162. },
  163. Outputs =
  164. {
  165. Activate = "bool",
  166. AddImpulse = "bool",
  167. Hide = "bool",
  168. Show = "bool",
  169. OnTouch = "bool",
  170. },
  171. }