enphysics.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /**
  2. * \defgroup Physics Physics system
  3. * @{
  4. */
  5. typedef int[] dGeom;
  6. typedef int[] dJoint;
  7. typedef int[] dBlock;
  8. proto native int dGetNumDynamicBodies(notnull IEntity worldEnt);
  9. proto native IEntity dGetDynamicBody(notnull IEntity worldEnt, int index);
  10. proto native void dSetInteractionLayer(notnull IEntity worldEntity, int mask1, int mask2, bool enable);
  11. proto native bool dGetInteractionLayer(notnull IEntity worldEntity, int mask1, int mask2);
  12. //!Gets global gravity
  13. proto native vector dGetGravity(notnull IEntity worldEntity);
  14. //!Changes global gravity
  15. proto native void dSetGravity(notnull IEntity worldEntity, vector g);
  16. //!Changes fixed time-slice. Default is 1/40, thus simulation runs on 40fps. With smaller values, there is more precise simulation
  17. proto native void dSetTimeSlice(notnull IEntity worldEntity, float timeSlice);
  18. /**
  19. * \defgroup RigidBody RigidBody API
  20. * @{
  21. */
  22. //proto native int dMaterialClone(string target, string source, int material_index)
  23. //proto native int dMaterialGetType(string source)
  24. //proto native int dMaterialSetType(string source, int material_index)
  25. class PhysicsGeomDef: Managed
  26. {
  27. string Name;
  28. dGeom Geometry;
  29. vector Frame[4] = {Vector(1, 0, 0), Vector(0, 1, 0), Vector(0, 0, 1), Vector(0, 0, 0)};
  30. int ParentNode = -1;
  31. string MaterialName;
  32. int LayerMask; //<Bit mask of layers we are in
  33. void PhysicsGeomDef(string name, dGeom geom, string materialName, int layerMask)
  34. {
  35. Name = name;
  36. Geometry = geom;
  37. MaterialName = materialName;
  38. LayerMask = layerMask;
  39. }
  40. };
  41. /*!
  42. Creates RigidBody from custom made geometries. The geometries are deleted when rigid body is destroyed
  43. \param geoms array of custom made geometries
  44. @code
  45. autoptr PhysicsGeomDef geoms[] = {PhysicsGeomDef("", dGeomCreateBox(size), "material/default", 0xffffffff)};
  46. dBodyCreateStaticEx(this, geoms);
  47. @endcode
  48. */
  49. proto bool dBodyCreateStaticEx(notnull IEntity ent, PhysicsGeomDef geoms[]);
  50. proto bool dBodyCreateGhostEx(notnull IEntity ent, PhysicsGeomDef geoms[]);
  51. /*!
  52. Creates RigidBody from custom made geometries. The geometries are deleted when rigid body is destroyed
  53. \param centerOfMass Offset from object-pivot to center of mass
  54. \param mass Body mass
  55. \param geoms array of custom made geometries
  56. @code
  57. autoptr PhysicsGeomDef geoms[] = {PhysicsGeomDef("", dGeomCreateBox(size), "material/default", 0xffffffff)};
  58. dBodyCreateDynamicEx(this, center, 1.0, geoms);
  59. @endcode
  60. */
  61. proto bool dBodyCreateDynamicEx(notnull IEntity ent, vector centerOfMass, float mass, PhysicsGeomDef geoms[]);
  62. //!Destroys attached physics body
  63. proto native void dBodyDestroy(notnull IEntity ent);
  64. //!Has the entity attached physics body?
  65. proto native bool dBodyIsSet(notnull IEntity ent);
  66. proto native void dBodySetInteractionLayer(notnull IEntity ent, int mask);
  67. proto native int dBodyGetInteractionLayer(notnull IEntity ent);
  68. proto native void dBodySetGeomInteractionLayer(notnull IEntity ent, int index, int mask);
  69. proto native int dBodyGetGeomInteractionLayer(notnull IEntity ent, int index);
  70. //! state of a rigidbody
  71. enum ActiveState
  72. {
  73. ACTIVE,
  74. INACTIVE,
  75. ALWAYS_ACTIVE
  76. };
  77. proto native void dBodyActive(notnull IEntity ent, ActiveState activeState);
  78. proto native void dBodyDynamic(notnull IEntity ent, bool dynamic);
  79. proto native bool dBodyIsDynamic(notnull IEntity ent);
  80. proto native bool dBodyIsActive(notnull IEntity ent);
  81. proto native bool dBodyEnableGravity(notnull IEntity ent, bool enable);
  82. proto native void dBodySetDamping(notnull IEntity ent, float linearDamping, float angularDamping);
  83. proto native void dBodySetSleepingTreshold(notnull IEntity body, float linearTreshold, float angularTreshold);
  84. proto native bool dBodyIsSolid(notnull IEntity ent);
  85. proto native void dBodySetSolid(notnull IEntity ent, bool solid);
  86. /*!
  87. If both maxMotion and shapeCastRadius is >=0 then the continuous collision detection is enabled.
  88. If you want to disable it, use -1
  89. \param maxMotion max motion threshold when sphere-cast is performed, to find time of impact. It should be
  90. little bit less than size of the geometry to catch the situation when tunelling can happen
  91. \param sphereCastRadius The radius of the largest possible sphere, that is completelly inside the body geometry.
  92. */
  93. proto native void dBodyEnableCCD(notnull IEntity body, float maxMotion, float sphereCastRadius);
  94. /*!
  95. Sets scale factor for all impulses/velocities/forces. Default is <1,1,1>. Zero any axis if you want to do 2D physics
  96. */
  97. proto native void dBodySetLinearFactor(notnull IEntity body, vector linearFactor);
  98. //!returns center of mass offset
  99. proto native vector dBodyGetCenterOfMass(notnull IEntity body);
  100. /**
  101. \brief Returns linear velocity
  102. \param ent \p IEntity entity which origin will be set
  103. \param mat \p vector[4] matrix to be set
  104. \return \p vector linear velocity
  105. @code
  106. Man player = g_Game.GetPlayer();
  107. Print( GetVelocity(player) );
  108. >> <0,0,0>
  109. @endcode
  110. */
  111. proto native vector GetVelocity(notnull IEntity ent);
  112. /**
  113. \brief Sets linear velocity (for Rigid bodies)
  114. \param ent \p entity which velocity will be set
  115. \param vel \p velocity vector to be set
  116. */
  117. proto native void SetVelocity(notnull IEntity ent, vector vel);
  118. /**
  119. \brief Disables collisions between two entities
  120. */
  121. proto native dBlock dBodyCollisionBlock(notnull IEntity ent1, notnull IEntity ent2);
  122. proto native void dBodyRemoveBlock(notnull IEntity worldEntity, dBlock block);
  123. proto native void dBodySetInertiaTensorV(notnull IEntity body, vector v);
  124. proto native void dBodySetInertiaTensorM(notnull IEntity body, vector m[3]);
  125. proto native float dBodyGetMass(notnull IEntity ent);
  126. proto native void dBodySetMass(notnull IEntity body, float mass);
  127. proto native void dBodyApplyTorqueImpulse(notnull IEntity ent, vector torqueImpulse);
  128. proto native vector dBodyGetInvInertiaDiagLocal(notnull IEntity ent);
  129. proto native float dBodyComputeImpulseDenominator(notnull IEntity ent, vector position, vector normal);
  130. proto native float dBodyComputeAngularImpulseDenominator(notnull IEntity ent, vector axis);
  131. proto native vector dBodyGetLocalInertia(notnull IEntity ent);
  132. proto void dBodyGetInvInertiaTensorWorld(notnull IEntity body, out vector inertiaTensorWS[3]);
  133. /**
  134. \brief Applies impuls on a pos position in world coordinates
  135. */
  136. proto void dBodyApplyImpulseAt(notnull IEntity body, vector impulse, vector pos);
  137. /**
  138. \brief Applies impuls on a rigidbody (origin)
  139. */
  140. proto void dBodyApplyImpulse(notnull IEntity body, vector impulse);
  141. /**
  142. \brief Applies constant force on a rigidbody (origin)
  143. */
  144. proto void dBodyApplyForce(notnull IEntity body, vector force);
  145. /**
  146. \brief Applies constant force on a position
  147. */
  148. proto void dBodyApplyForceAt(notnull IEntity body, vector pos, vector force);
  149. proto native void dBodyApplyTorque(notnull IEntity body, vector torque);
  150. /**
  151. \brief Gets angular velocity for a rigidbody
  152. */
  153. proto vector dBodyGetAngularVelocity(notnull IEntity body);
  154. /**
  155. \brief Changed an angular velocity
  156. \param body \p Rigid body
  157. \param angvel \p Angular velocity, rotation around x, y and z axis (not yaw/pitch/roll)
  158. */
  159. proto void dBodySetAngularVelocity(notnull IEntity body, vector angvel);
  160. /**
  161. \brief Sets target transformation. If timeslice == dt (simulation step delta time), it will happen in next step, otherwise in time = timeslice
  162. */
  163. proto native void dBodySetTargetMatrix(notnull IEntity body, vector matrix[4], float timeslice);
  164. proto native void dBodyGetWorldTransform(notnull IEntity body, out vector matrix[4]);
  165. proto native void dBodyGetDirectWorldTransform(notnull IEntity body, out vector matrix[4]);
  166. proto native float dBodyGetKineticEnergy(notnull IEntity body);
  167. proto native vector dBodyGetVelocityAt(notnull IEntity body, vector globalpos);
  168. //@}
  169. /**
  170. * \defgroup Geometry Geometry API definition
  171. * @{
  172. */
  173. //! Creates box geometry
  174. proto native dGeom dGeomCreateBox(vector size);
  175. //! Creates sphere geometry
  176. proto native dGeom dGeomCreateSphere(float radius);
  177. //! Creates capsule geometry
  178. proto native dGeom dGeomCreateCapsule(float radius, vector extent);
  179. //! Creates cylinder geometry
  180. proto native dGeom dGeomCreateCylinder(float radius, vector extent);
  181. //! Destroys geometry
  182. proto native void dGeomDestroy(dGeom geom);
  183. //proto native int dBodyAddGeom(notnull IEntity body, dGeom geom, vector frame[4], string material, int interactionLayer);
  184. // find a geometry by its name and returns its index or -1 if the geometry wasn't found
  185. proto native int dBodyGetGeom(notnull IEntity ent, string name);
  186. // returns number of geometries of the entity
  187. proto native int dBodyGetNumGeoms(notnull IEntity ent);
  188. //@}
  189. /**
  190. * \defgroup Constraints Constraints API definition
  191. * @{
  192. */
  193. proto native dJoint dJointCreateHinge(notnull IEntity ent1, notnull IEntity ent2, vector point1, vector axis1, vector point2, vector axis2, bool block, float breakThreshold);
  194. proto native dJoint dJointCreateHinge2(notnull IEntity ent1, notnull IEntity ent2, vector matrix1[4], vector matrix2[4], bool block, float breakThreshold);
  195. proto native dJoint dJointCreateSlider(notnull IEntity ent1, notnull IEntity ent2, vector matrix1[4], vector matrix2[4], bool block, float breakThreshold);
  196. proto native dJoint dJointCreateBallSocket(notnull IEntity ent1, notnull IEntity ent2, vector point1, vector point2, bool block, float breakThreshold);
  197. proto native dJoint dJointCreateFixed(notnull IEntity ent1, notnull IEntity ent2, vector point1, vector point2, bool block, float breakThreshold);
  198. proto native dJoint dJointCreateConeTwist(notnull IEntity ent1, notnull IEntity ent2, vector matrix1[4], vector matrix2[4], bool block, float breakThreshold);
  199. proto native dJoint dJointCreate6DOF(notnull IEntity ent1, notnull IEntity ent2, vector matrix1[4], vector matrix2[4], bool block, float breakThreshold);
  200. proto native dJoint dJointCreate6DOFSpring(notnull IEntity ent1, notnull IEntity ent2, vector matrix1[4], vector matrix2[4], bool block, float breakThreshold);
  201. proto native void dJointDestroy(dJoint joint);
  202. //only hinge joint
  203. proto native void dJointHingeSetLimits(dJoint joint, float low, float high, float softness, float biasFactor, float relaxationFactor);
  204. proto native void dJointHingeSetAxis(dJoint joint, vector axis);
  205. proto native void dJointHingeSetMotorTargetAngle(dJoint joint, float angle, float dt, float maxImpulse);
  206. //only cone-twist joint
  207. proto native void dJointConeTwistSetAngularOnly(dJoint joint, bool angularOnly);
  208. // setLimit(), a few notes:
  209. // _softness:
  210. // 0->1, recommend ~0.8->1.
  211. // describes % of limits where movement is free.
  212. // beyond this softness %, the limit is gradually enforced until the "hard" (1.0) limit is reached.
  213. // _biasFactor:
  214. // 0->1?, recommend 0.3 +/-0.3 or so.
  215. // strength with which constraint resists zeroth order (angular, not angular velocity) limit violation.
  216. // __relaxationFactor:
  217. // 0->1, recommend to stay near 1.
  218. // the lower the value, the less the constraint will fight velocities which violate the angular limits.
  219. proto native void dJointConeTwistSetLimit(dJoint joint, int limitIndex, float limitValue);
  220. proto native void dJointConeTwistSetLimits(dJoint joint, float _swingSpan1, float _swingSpan2, float _twistSpan, float _softness, float _biasFactor, float _relaxationFactor);
  221. //only 6DOF & 6DOFSpring.
  222. /*!
  223. - free means upper < lower,
  224. - locked means upper == lower
  225. - limited means upper > lower
  226. - axis: first 3 are linear, next 3 are angular
  227. */
  228. proto native void dJoint6DOFSetLinearLimits(dJoint joint, vector linearLower, vector linearUpper);
  229. proto native void dJoint6DOFSetAngularLimits(dJoint joint, vector angularLower, vector angularUpper);
  230. proto native void dJoint6DOFSetLimit(dJoint joint, int axis, float lo, float hi);
  231. //when stiffness == -1 && damping == -1, spring is disabled
  232. proto native void dJoint6DOFSpringSetSpring(dJoint joint, int axis, float stiffness, float damping);
  233. //only slider joint
  234. proto native void dJointSliderSetLinearLimits(dJoint joint, float lowerLimit, float upperLimit);
  235. proto native void dJointSliderSetAngularLimits(dJoint joint, float lowerLimit, float upperLimit);
  236. proto native void dJointSliderSetDirLinear(dJoint joint, float softness, float restitution, float damping);
  237. proto native void dJointSliderSetDirAngular(dJoint joint, float softness, float restitution, float damping);
  238. proto native void dJointSliderSetLimLinear(dJoint joint, float softness, float restitution, float damping);
  239. proto native void dJointSliderSetLimAngular(dJoint joint, float softness, float restitution, float damping);
  240. proto native void dJointSliderSetOrthoLinear(dJoint joint, float softness, float restitution, float damping);
  241. proto native void dJointSliderSetOrthoAngular(dJoint joint, float softness, float restitution, float damping);
  242. //if force == 0, motor is off
  243. proto native void dJointSliderSetLinearMotor(dJoint joint, float velocity, float force);
  244. proto native void dJointSliderSetAngularMotor(dJoint joint, float velocity, float force);
  245. proto native float dJointSliderGetLinearPos(dJoint joint);
  246. proto native float dJointSliderGetAngularPos(dJoint joint);
  247. //@}
  248. //-----------------------------------------------------------------
  249. typedef int[] dMaterial;
  250. class Contact
  251. {
  252. private void Contact() {}
  253. private void ~Contact() {}
  254. dMaterial Material1;
  255. dMaterial Material2;
  256. int MaterialIndex1;
  257. int MaterialIndex2;
  258. int Index1;
  259. int Index2;
  260. float PenetrationDepth;
  261. float Impulse;
  262. float RelativeNormalVelocityBefore;
  263. float RelativeNormalVelocityAfter;
  264. vector Normal;
  265. vector Position;
  266. vector RelativeVelocityBefore;
  267. vector RelativeVelocityAfter;
  268. proto native vector GetNormalImpulse();
  269. proto native float GetRelativeVelocityBefore(vector vel);
  270. proto native float GetRelativeVelocityAfter(vector vel);
  271. };
  272. //@}