physicsComponent.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "T3D/components/physics/physicsComponent.h"
  23. #include "platform/platform.h"
  24. #include "console/consoleTypes.h"
  25. #include "core/util/safeDelete.h"
  26. #include "core/resourceManager.h"
  27. #include "core/stream/fileStream.h"
  28. #include "console/consoleTypes.h"
  29. #include "console/consoleObject.h"
  30. #include "ts/tsShapeInstance.h"
  31. #include "core/stream/bitStream.h"
  32. #include "gfx/gfxTransformSaver.h"
  33. #include "console/engineAPI.h"
  34. #include "lighting/lightQuery.h"
  35. #include "T3D/gameBase/gameConnection.h"
  36. #include "T3D/containerQuery.h"
  37. #include "math/mathIO.h"
  38. #include "T3D/physics/physicsPlugin.h"
  39. //////////////////////////////////////////////////////////////////////////
  40. // Constructor/Destructor
  41. //////////////////////////////////////////////////////////////////////////
  42. PhysicsComponent::PhysicsComponent() : Component()
  43. {
  44. addComponentField("isStatic", "If enabled, object will not simulate physics", "bool", "0", "");
  45. addComponentField("gravity", "The direction of gravity affecting this object, as a vector", "vector", "0 0 -9", "");
  46. addComponentField("drag", "The drag coefficient that constantly affects the object", "float", "0.7", "");
  47. addComponentField("mass", "The mass of the object", "float", "1", "");
  48. mFriendlyName = "Physics Component";
  49. mComponentType = "Physics";
  50. mDescription = getDescriptionText("A stub component class that physics components should inherit from.");
  51. mStatic = false;
  52. mAtRest = false;
  53. mAtRestCounter = 0;
  54. mGravity = VectorF(0, 0, 0);
  55. mVelocity = VectorF(0, 0, 0);
  56. mDrag = 0.7f;
  57. mMass = 1.f;
  58. mGravityMod = 1.f;
  59. csmAtRestTimer = 64;
  60. sAtRestVelocity = 0.15f;
  61. mDelta.pos = Point3F(0, 0, 0);
  62. mDelta.posVec = Point3F(0, 0, 0);
  63. mDelta.warpTicks = mDelta.warpCount = 0;
  64. mDelta.dt = 1;
  65. mDelta.move = NullMove;
  66. mPredictionCount = 0;
  67. }
  68. PhysicsComponent::~PhysicsComponent()
  69. {
  70. for(S32 i = 0;i < mFields.size();++i)
  71. {
  72. ComponentField &field = mFields[i];
  73. SAFE_DELETE_ARRAY(field.mFieldDescription);
  74. }
  75. SAFE_DELETE_ARRAY(mDescription);
  76. }
  77. IMPLEMENT_CONOBJECT(PhysicsComponent);
  78. void PhysicsComponent::onComponentAdd()
  79. {
  80. Parent::onComponentAdd();
  81. // Initialize interpolation vars.
  82. mDelta.rot[1] = mDelta.rot[0] = QuatF(mOwner->getTransform());
  83. mDelta.pos = mOwner->getPosition();
  84. mDelta.posVec = Point3F(0,0,0);
  85. }
  86. void PhysicsComponent::onComponentRemove()
  87. {
  88. Parent::onComponentRemove();
  89. }
  90. void PhysicsComponent::initPersistFields()
  91. {
  92. Parent::initPersistFields();
  93. addField("gravity", TypePoint3F, Offset(mGravity, PhysicsComponent));
  94. addField("velocity", TypePoint3F, Offset(mVelocity, PhysicsComponent));
  95. addField("isStatic", TypeBool, Offset(mStatic, PhysicsComponent));
  96. }
  97. //Networking
  98. U32 PhysicsComponent::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
  99. {
  100. U32 retMask = Parent::packUpdate(con, mask, stream);
  101. if(stream->writeFlag(mask & VelocityMask))
  102. mathWrite( *stream, mVelocity );
  103. if(stream->writeFlag(mask & UpdateMask))
  104. {
  105. stream->writeFlag(mStatic);
  106. stream->writeFlag(mAtRest);
  107. stream->writeInt(mAtRestCounter,8);
  108. mathWrite( *stream, mGravity );
  109. stream->writeFloat(mDrag, 12);
  110. //stream->writeFloat(mMass, 12);
  111. stream->writeFloat(mGravityMod, 12);
  112. }
  113. return retMask;
  114. }
  115. void PhysicsComponent::unpackUpdate(NetConnection *con, BitStream *stream)
  116. {
  117. Parent::unpackUpdate(con, stream);
  118. if(stream->readFlag())
  119. mathRead( *stream, &mVelocity );
  120. if(stream->readFlag())
  121. {
  122. mStatic = stream->readFlag();
  123. mAtRest = stream->readFlag();
  124. mAtRestCounter = stream->readInt(8);
  125. mathRead( *stream, &mGravity );
  126. mDrag = stream->readFloat(12);
  127. //mMass = stream->readFloat(12);
  128. mGravityMod = stream->readFloat(12);
  129. }
  130. }
  131. //Setup
  132. void PhysicsComponent::prepCollision()
  133. {
  134. if (!mOwner)
  135. return;
  136. if (mConvexList != NULL)
  137. mConvexList->nukeList();
  138. mOwner->enableCollision();
  139. _updatePhysics();
  140. }
  141. void PhysicsComponent::_updatePhysics()
  142. {
  143. SAFE_DELETE( mPhysicsRep );
  144. if ( !PHYSICSMGR )
  145. return;
  146. return;
  147. }
  148. void PhysicsComponent::buildConvex(const Box3F& box, Convex* convex)
  149. {
  150. convex = nullptr;
  151. }
  152. //Updates
  153. void PhysicsComponent::interpolateTick(F32 dt)
  154. {
  155. Point3F pos = mDelta.pos + mDelta.posVec * dt;
  156. //Point3F rot = mDelta.rot + mDelta.rotVec * dt;
  157. setRenderPosition(pos,dt);
  158. }
  159. void PhysicsComponent::updatePos(const F32 travelTime)
  160. {
  161. return;
  162. }
  163. void PhysicsComponent::updateForces()
  164. {
  165. return;
  166. }
  167. void PhysicsComponent::updateContainer()
  168. {
  169. PROFILE_SCOPE(PhysicsBehaviorInstance_updateContainer);
  170. // Update container drag and buoyancy properties
  171. // Set default values.
  172. //mDrag = mDataBlock->drag;
  173. //mBuoyancy = 0.0f;
  174. //mGravityMod = 1.0;
  175. //mAppliedForce.set(0,0,0);
  176. mLastContainerInfo = ContainerQueryInfo();
  177. mLastContainerInfo.box = mOwner->getWorldBox();
  178. mLastContainerInfo.mass = mMass;
  179. mOwner->getContainer()->findObjects(mLastContainerInfo.box, WaterObjectType | PhysicalZoneObjectType, findRouter, &mLastContainerInfo);
  180. //mWaterCoverage = info.waterCoverage;
  181. //mLiquidType = info.liquidType;
  182. //mLiquidHeight = info.waterHeight;
  183. //setCurrentWaterObject( info.waterObject );
  184. // This value might be useful as a datablock value,
  185. // This is what allows the player to stand in shallow water (below this coverage)
  186. // without jiggling from buoyancy
  187. if (mLastContainerInfo.waterCoverage >= 0.25f)
  188. {
  189. // water viscosity is used as drag for in water.
  190. // ShapeBaseData drag is used for drag outside of water.
  191. // Combine these two components to calculate this ShapeBase object's
  192. // current drag.
  193. mDrag = (mLastContainerInfo.waterCoverage * mLastContainerInfo.waterViscosity) +
  194. (1.0f - mLastContainerInfo.waterCoverage) * mDrag;
  195. //mBuoyancy = (info.waterDensity / mDataBlock->density) * info.waterCoverage;
  196. }
  197. //mAppliedForce = info.appliedForce;
  198. mGravityMod = mLastContainerInfo.gravityScale;
  199. }
  200. //Events
  201. void PhysicsComponent::updateVelocity(const F32 dt)
  202. {
  203. }
  204. void PhysicsComponent::applyImpulse(const Point3F&, const VectorF& vec)
  205. {
  206. // Items ignore angular velocity
  207. VectorF vel;
  208. vel.x = vec.x / mMass;
  209. vel.y = vec.y / mMass;
  210. vel.z = vec.z / mMass;
  211. setVelocity(mVelocity + vel);
  212. }
  213. //Setters
  214. void PhysicsComponent::setTransform(const MatrixF& mat)
  215. {
  216. mOwner->setTransform(mat);
  217. if (!mStatic)
  218. {
  219. mAtRest = false;
  220. mAtRestCounter = 0;
  221. }
  222. if (getPhysicsRep())
  223. getPhysicsRep()->setTransform(mOwner->getTransform());
  224. setMaskBits(UpdateMask);
  225. }
  226. void PhysicsComponent::setPosition(const Point3F& pos)
  227. {
  228. MatrixF mat = mOwner->getTransform();
  229. if (mOwner->isMounted()) {
  230. // Use transform from mounted object
  231. //mOwner->getObjectMount()->getMountTransform( mOwner->getMountNode(), mMount.xfm, &mat );
  232. return;
  233. }
  234. else {
  235. mat.setColumn(3, pos);
  236. }
  237. mOwner->setTransform(mat);
  238. if (getPhysicsRep())
  239. getPhysicsRep()->setTransform(mat);
  240. }
  241. void PhysicsComponent::setRenderPosition(const Point3F& pos, F32 dt)
  242. {
  243. MatrixF mat = mOwner->getRenderTransform();
  244. if (mOwner->isMounted()) {
  245. // Use transform from mounted object
  246. //mOwner->getObjectMount()->getMountRenderTransform( dt, mOwner->getMountNode(), mMount.xfm, &mat );
  247. return;
  248. }
  249. else {
  250. mat.setColumn(3, pos);
  251. }
  252. mOwner->setRenderTransform(mat);
  253. }
  254. void PhysicsComponent::setVelocity(const VectorF& vel)
  255. {
  256. mVelocity = vel;
  257. mAtRest = false;
  258. mAtRestCounter = 0;
  259. setMaskBits(VelocityMask);
  260. }
  261. //Getters
  262. PhysicsBody *PhysicsComponent::getPhysicsRep()
  263. {
  264. /*if(mOwner)
  265. {
  266. Entity* ac = dynamic_cast<Entity*>(mOwner);
  267. if(ac)
  268. return ac->mPhysicsRep;
  269. }*/
  270. return NULL;
  271. }
  272. void PhysicsComponent::getVelocity(const Point3F& r, Point3F* v)
  273. {
  274. *v = mVelocity;
  275. }
  276. void PhysicsComponent::getOriginVector(const Point3F &p,Point3F* r)
  277. {
  278. *r = p - mOwner->getObjBox().getCenter();
  279. }
  280. F32 PhysicsComponent::getZeroImpulse(const Point3F& r,const Point3F& normal)
  281. {
  282. Point3F a,b,c;
  283. //set up our inverse matrix
  284. MatrixF iv,qmat;
  285. MatrixF inverse = MatrixF::Identity;
  286. qmat = mOwner->getTransform();
  287. iv.mul(qmat,inverse);
  288. qmat.transpose();
  289. inverse.mul(iv,qmat);
  290. mCross(r, normal, &a);
  291. inverse.mulV(a, &b);
  292. mCross(b, r, &c);
  293. return 1 / ((1/mMass) + mDot(c, normal));
  294. }
  295. DefineEngineMethod( PhysicsComponent, applyImpulse, bool, ( Point3F pos, VectorF vel ),,
  296. "@brief Apply an impulse to this object as defined by a world position and velocity vector.\n\n"
  297. "@param pos impulse world position\n"
  298. "@param vel impulse velocity (impulse force F = m * v)\n"
  299. "@return Always true\n"
  300. "@note Not all objects that derrive from GameBase have this defined.\n")
  301. {
  302. object->applyImpulse(pos,vel);
  303. return true;
  304. }