playerControllerComponent.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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/playerControllerComponent.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 "collision/collision.h"
  37. #include "T3D/physics/physicsPlayer.h"
  38. #include "T3D/physics/physicsPlugin.h"
  39. #include "T3D/components/collision/collisionInterfaces.h"
  40. #include "T3D/trigger.h"
  41. #include "T3D/components/collision/collisionTrigger.h"
  42. // Movement constants
  43. static F32 sVerticalStepDot = 0.173f; // 80
  44. static F32 sMinFaceDistance = 0.01f;
  45. static F32 sTractionDistance = 0.04f;
  46. static F32 sNormalElasticity = 0.01f;
  47. static U32 sMoveRetryCount = 5;
  48. static F32 sMaxImpulseVelocity = 200.0f;
  49. //////////////////////////////////////////////////////////////////////////
  50. // Callbacks
  51. IMPLEMENT_CALLBACK(PlayerControllerComponent, updateMove, void, (PlayerControllerComponent* obj), (obj),
  52. "Called when the player updates it's movement, only called if object is set to callback in script(doUpdateMove).\n"
  53. "@param obj the Player object\n");
  54. //////////////////////////////////////////////////////////////////////////
  55. // Constructor/Destructor
  56. //////////////////////////////////////////////////////////////////////////
  57. PlayerControllerComponent::PlayerControllerComponent() : Component()
  58. {
  59. addComponentField("isStatic", "If enabled, object will not simulate physics", "bool", "0", "");
  60. addComponentField("gravity", "The direction of gravity affecting this object, as a vector", "vector", "0 0 -9", "");
  61. addComponentField("drag", "The drag coefficient that constantly affects the object", "float", "0.7", "");
  62. addComponentField("mass", "The mass of the object", "float", "1", "");
  63. mBuoyancy = 0.f;
  64. mFriction = 0.3f;
  65. mElasticity = 0.4f;
  66. mMaxVelocity = 3000.f;
  67. mVelocity = VectorF::Zero;
  68. mContactTimer = 0;
  69. mSticky = false;
  70. mFalling = false;
  71. mSwimming = false;
  72. mInWater = false;
  73. mDelta.pos = mDelta.posVec = Point3F::Zero;
  74. mDelta.warpTicks = mDelta.warpCount = 0;
  75. mDelta.rot[0].identity();
  76. mDelta.rot[1].identity();
  77. mDelta.dt = 1;
  78. mUseDirectMoveInput = false;
  79. mFriendlyName = "Player Controller";
  80. mComponentType = "Physics";
  81. mDescription = getDescriptionText("A general-purpose physics player controller.");
  82. //mNetFlags.set(Ghostable | ScopeAlways);
  83. mMass = 9.0f; // from ShapeBase
  84. mDrag = 1.0f; // from ShapeBase
  85. maxStepHeight = 1.0f;
  86. moveSurfaceAngle = 60.0f;
  87. contactSurfaceAngle = 85.0f;
  88. fallingSpeedThreshold = -10.0f;
  89. horizMaxSpeed = 80.0f;
  90. horizMaxAccel = 100.0f;
  91. horizResistSpeed = 38.0f;
  92. horizResistFactor = 1.0f;
  93. upMaxSpeed = 80.0f;
  94. upMaxAccel = 100.0f;
  95. upResistSpeed = 38.0f;
  96. upResistFactor = 1.0f;
  97. // Air control
  98. airControl = 0.0f;
  99. //Grav mod
  100. mGravityMod = 1;
  101. mInputVelocity = Point3F(0, 0, 0);
  102. mPhysicsRep = NULL;
  103. mPhysicsWorld = NULL;
  104. }
  105. PlayerControllerComponent::~PlayerControllerComponent()
  106. {
  107. for (S32 i = 0; i < mFields.size(); ++i)
  108. {
  109. ComponentField &field = mFields[i];
  110. SAFE_DELETE_ARRAY(field.mFieldDescription);
  111. }
  112. SAFE_DELETE_ARRAY(mDescription);
  113. }
  114. IMPLEMENT_CO_NETOBJECT_V1(PlayerControllerComponent);
  115. //////////////////////////////////////////////////////////////////////////
  116. bool PlayerControllerComponent::onAdd()
  117. {
  118. if (!Parent::onAdd())
  119. return false;
  120. return true;
  121. }
  122. void PlayerControllerComponent::onRemove()
  123. {
  124. Parent::onRemove();
  125. SAFE_DELETE(mPhysicsRep);
  126. }
  127. void PlayerControllerComponent::onComponentAdd()
  128. {
  129. Parent::onComponentAdd();
  130. updatePhysics();
  131. }
  132. void PlayerControllerComponent::componentAddedToOwner(Component *comp)
  133. {
  134. if (comp->getId() == getId())
  135. return;
  136. //test if this is a shape component!
  137. CollisionInterface *collisionInterface = dynamic_cast<CollisionInterface*>(comp);
  138. if (collisionInterface)
  139. {
  140. collisionInterface->onCollisionChanged.notify(this, &PlayerControllerComponent::updatePhysics);
  141. mOwnerCollisionInterface = collisionInterface;
  142. updatePhysics();
  143. }
  144. }
  145. void PlayerControllerComponent::componentRemovedFromOwner(Component *comp)
  146. {
  147. if (comp->getId() == getId()) //?????????
  148. return;
  149. //test if this is a shape component!
  150. CollisionInterface *collisionInterface = dynamic_cast<CollisionInterface*>(comp);
  151. if (collisionInterface)
  152. {
  153. collisionInterface->onCollisionChanged.remove(this, &PlayerControllerComponent::updatePhysics);
  154. mOwnerCollisionInterface = NULL;
  155. updatePhysics();
  156. }
  157. }
  158. void PlayerControllerComponent::updatePhysics(PhysicsCollision *collision)
  159. {
  160. if (!PHYSICSMGR)
  161. return;
  162. mPhysicsWorld = PHYSICSMGR->getWorld(isServerObject() ? "server" : "client");
  163. //first, clear the old physRep
  164. SAFE_DELETE(mPhysicsRep);
  165. mPhysicsRep = PHYSICSMGR->createPlayer();
  166. F32 runSurfaceCos = mCos(mDegToRad(moveSurfaceAngle));
  167. Point3F ownerBounds = mOwner->getObjBox().getExtents() * mOwner->getScale();
  168. mPhysicsRep->init("", ownerBounds, runSurfaceCos, maxStepHeight, mOwner, mPhysicsWorld);
  169. mPhysicsRep->setTransform(mOwner->getTransform());
  170. }
  171. void PlayerControllerComponent::initPersistFields()
  172. {
  173. Parent::initPersistFields();
  174. addField("inputVelocity", TypePoint3F, Offset(mInputVelocity, PlayerControllerComponent), "");
  175. addField("useDirectMoveInput", TypePoint3F, Offset(mUseDirectMoveInput, PlayerControllerComponent), "");
  176. }
  177. U32 PlayerControllerComponent::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
  178. {
  179. U32 retMask = Parent::packUpdate(con, mask, stream);
  180. return retMask;
  181. }
  182. void PlayerControllerComponent::unpackUpdate(NetConnection *con, BitStream *stream)
  183. {
  184. Parent::unpackUpdate(con, stream);
  185. }
  186. //
  187. void PlayerControllerComponent::processTick()
  188. {
  189. Parent::processTick();
  190. if (!isServerObject() || !isActive())
  191. return;
  192. // Warp to catch up to server
  193. if (mDelta.warpCount < mDelta.warpTicks)
  194. {
  195. mDelta.warpCount++;
  196. // Set new pos.
  197. mDelta.pos = mOwner->getPosition();
  198. mDelta.pos += mDelta.warpOffset;
  199. mDelta.rot[0] = mDelta.rot[1];
  200. mDelta.rot[1].interpolate(mDelta.warpRot[0], mDelta.warpRot[1], F32(mDelta.warpCount) / mDelta.warpTicks);
  201. MatrixF trans;
  202. mDelta.rot[1].setMatrix(&trans);
  203. trans.setPosition(mDelta.pos);
  204. mOwner->setTransform(trans);
  205. // Pos backstepping
  206. mDelta.posVec.x = -mDelta.warpOffset.x;
  207. mDelta.posVec.y = -mDelta.warpOffset.y;
  208. mDelta.posVec.z = -mDelta.warpOffset.z;
  209. }
  210. else
  211. {
  212. // Save current rigid state interpolation
  213. mDelta.posVec = mOwner->getPosition();
  214. mDelta.rot[0] = mOwner->getTransform();
  215. updateMove();
  216. updatePos(TickSec);
  217. // Wrap up interpolation info
  218. mDelta.pos = mOwner->getPosition();
  219. mDelta.posVec -= mOwner->getPosition();
  220. mDelta.rot[1] = mOwner->getTransform();
  221. // Update container database
  222. setTransform(mOwner->getTransform());
  223. setMaskBits(VelocityMask);
  224. setMaskBits(PositionMask);
  225. }
  226. }
  227. void PlayerControllerComponent::interpolateTick(F32 dt)
  228. {
  229. }
  230. void PlayerControllerComponent::ownerTransformSet(MatrixF *mat)
  231. {
  232. if (mPhysicsRep)
  233. mPhysicsRep->setTransform(mOwner->getTransform());
  234. }
  235. void PlayerControllerComponent::setTransform(const MatrixF& mat)
  236. {
  237. mOwner->setTransform(mat);
  238. setMaskBits(UpdateMask);
  239. }
  240. //
  241. void PlayerControllerComponent::updateMove()
  242. {
  243. if (!PHYSICSMGR)
  244. return;
  245. Move *move = &mOwner->lastMove;
  246. //If we're not set to use mUseDirectMoveInput, then we allow for an override in the form of mInputVelocity
  247. if (!mUseDirectMoveInput)
  248. {
  249. move->x = mInputVelocity.x;
  250. move->y = mInputVelocity.y;
  251. move->z = mInputVelocity.z;
  252. }
  253. // Is waterCoverage high enough to be 'swimming'?
  254. {
  255. bool swimming = mOwner->getContainerInfo().waterCoverage > 0.65f/* && canSwim()*/;
  256. if (swimming != mSwimming)
  257. {
  258. mSwimming = swimming;
  259. }
  260. }
  261. // Update current orientation
  262. bool doStandardMove = true;
  263. GameConnection* con = mOwner->getControllingClient();
  264. MatrixF zRot;
  265. zRot.set(EulerF(0.0f, 0.0f, mOwner->getRotation().asEulerF().z));
  266. // Desired move direction & speed
  267. VectorF moveVec;
  268. F32 moveSpeed = mInputVelocity.len();
  269. zRot.getColumn(0, &moveVec);
  270. moveVec *= move->x;
  271. VectorF tv;
  272. zRot.getColumn(1, &tv);
  273. moveVec += tv * move->y;
  274. // Acceleration due to gravity
  275. VectorF acc(mPhysicsWorld->getGravity() * mGravityMod * TickSec);
  276. // Determine ground contact normal. Only look for contacts if
  277. // we can move and aren't mounted.
  278. mContactInfo.contactNormal = VectorF::Zero;
  279. mContactInfo.jump = false;
  280. mContactInfo.run = false;
  281. bool jumpSurface = false, runSurface = false;
  282. if (!mOwner->isMounted())
  283. findContact(&mContactInfo.run, &mContactInfo.jump, &mContactInfo.contactNormal);
  284. if (mContactInfo.jump)
  285. mJumpSurfaceNormal = mContactInfo.contactNormal;
  286. // If we don't have a runSurface but we do have a contactNormal,
  287. // then we are standing on something that is too steep.
  288. // Deflect the force of gravity by the normal so we slide.
  289. // We could also try aligning it to the runSurface instead,
  290. // but this seems to work well.
  291. if (!mContactInfo.run && !mContactInfo.contactNormal.isZero())
  292. acc = (acc - 2 * mContactInfo.contactNormal * mDot(acc, mContactInfo.contactNormal));
  293. // Acceleration on run surface
  294. if (mContactInfo.run && !mSwimming)
  295. {
  296. mContactTimer = 0;
  297. VectorF pv = moveVec;
  298. // Adjust the player's requested dir. to be parallel
  299. // to the contact surface.
  300. F32 pvl = pv.len();
  301. // Convert to acceleration
  302. if (pvl)
  303. pv *= moveSpeed / pvl;
  304. VectorF runAcc = pv - (mVelocity + acc);
  305. F32 runSpeed = runAcc.len();
  306. // Clamp acceleration, player also accelerates faster when
  307. // in his hard landing recover state.
  308. F32 maxAcc;
  309. maxAcc = (horizMaxAccel / mMass) * TickSec;
  310. if (runSpeed > maxAcc)
  311. runAcc *= maxAcc / runSpeed;
  312. acc += runAcc;
  313. }
  314. else if (!mSwimming && airControl > 0.0f)
  315. {
  316. VectorF pv;
  317. pv = moveVec;
  318. F32 pvl = pv.len();
  319. if (pvl)
  320. pv *= moveSpeed / pvl;
  321. VectorF runAcc = pv - (mVelocity + acc);
  322. runAcc.z = 0;
  323. runAcc.x = runAcc.x * airControl;
  324. runAcc.y = runAcc.y * airControl;
  325. F32 runSpeed = runAcc.len();
  326. // We don't test for sprinting when performing air control
  327. F32 maxAcc = (horizMaxAccel / mMass) * TickSec * 0.3f;
  328. if (runSpeed > maxAcc)
  329. runAcc *= maxAcc / runSpeed;
  330. acc += runAcc;
  331. // There are no special air control animations
  332. // so... increment this unless you really want to
  333. // play the run anims in the air.
  334. mContactTimer++;
  335. }
  336. else if (mSwimming)
  337. {
  338. // Remove acc into contact surface (should only be gravity)
  339. // Clear out floating point acc errors, this will allow
  340. // the player to "rest" on the ground.
  341. F32 vd = -mDot(acc, mContactInfo.contactNormal);
  342. if (vd > 0.0f)
  343. {
  344. VectorF dv = mContactInfo.contactNormal * (vd + 0.002f);
  345. acc += dv;
  346. if (acc.len() < 0.0001f)
  347. acc.set(0.0f, 0.0f, 0.0f);
  348. }
  349. // get the head pitch and add it to the moveVec
  350. // This more accurate swim vector calc comes from Matt Fairfax
  351. MatrixF xRot, zRot;
  352. xRot.set(EulerF(mOwner->getRotation().asEulerF().x, 0, 0));
  353. zRot.set(EulerF(0, 0, mOwner->getRotation().asEulerF().z));
  354. MatrixF rot;
  355. rot.mul(zRot, xRot);
  356. rot.getColumn(0, &moveVec);
  357. moveVec *= move->x;
  358. VectorF tv;
  359. rot.getColumn(1, &tv);
  360. moveVec += tv * move->y;
  361. rot.getColumn(2, &tv);
  362. moveVec += tv * move->z;
  363. // Force a 0 move if there is no energy, and only drain
  364. // move energy if we're moving.
  365. VectorF swimVec = moveVec;
  366. // If we are swimming but close enough to the shore/ground
  367. // we can still have a surface-normal. In this case align the
  368. // velocity to the normal to make getting out of water easier.
  369. moveVec.normalize();
  370. F32 isSwimUp = mDot(moveVec, mContactInfo.contactNormal);
  371. if (!mContactInfo.contactNormal.isZero() && isSwimUp < 0.1f)
  372. {
  373. F32 pvl = swimVec.len();
  374. if (pvl)
  375. {
  376. VectorF nn;
  377. mCross(swimVec, VectorF(0.0f, 0.0f, 1.0f), &nn);
  378. nn *= 1.0f / pvl;
  379. VectorF cv = mContactInfo.contactNormal;
  380. cv -= nn * mDot(nn, cv);
  381. swimVec -= cv * mDot(swimVec, cv);
  382. }
  383. }
  384. F32 swimVecLen = swimVec.len();
  385. // Convert to acceleration.
  386. if (swimVecLen)
  387. swimVec *= moveSpeed / swimVecLen;
  388. VectorF swimAcc = swimVec - (mVelocity + acc);
  389. F32 swimSpeed = swimAcc.len();
  390. // Clamp acceleration.
  391. F32 maxAcc = (horizMaxAccel / mMass) * TickSec;
  392. if (swimSpeed > maxAcc)
  393. swimAcc *= maxAcc / swimSpeed;
  394. acc += swimAcc;
  395. mContactTimer++;
  396. }
  397. else
  398. mContactTimer++;
  399. // Add in force from physical zones...
  400. acc += (mOwner->getContainerInfo().appliedForce / mMass) * TickSec;
  401. // Adjust velocity with all the move & gravity acceleration
  402. // TG: I forgot why doesn't the TickSec multiply happen here...
  403. mVelocity += acc;
  404. // apply horizontal air resistance
  405. F32 hvel = mSqrt(mVelocity.x * mVelocity.x + mVelocity.y * mVelocity.y);
  406. if (hvel > horizResistSpeed)
  407. {
  408. F32 speedCap = hvel;
  409. if (speedCap > horizMaxSpeed)
  410. speedCap = horizMaxSpeed;
  411. speedCap -= horizResistFactor * TickSec * (speedCap - horizResistSpeed);
  412. F32 scale = speedCap / hvel;
  413. mVelocity.x *= scale;
  414. mVelocity.y *= scale;
  415. }
  416. if (mVelocity.z > upResistSpeed)
  417. {
  418. if (mVelocity.z > upMaxSpeed)
  419. mVelocity.z = upMaxSpeed;
  420. mVelocity.z -= upResistFactor * TickSec * (mVelocity.z - upResistSpeed);
  421. }
  422. // Apply drag
  423. mVelocity -= mVelocity * mDrag * TickSec;
  424. // Clamp very small velocity to zero
  425. if (mVelocity.isZero())
  426. mVelocity = Point3F::Zero;
  427. // If we are not touching anything and have sufficient -z vel,
  428. // we are falling.
  429. if (mContactInfo.run)
  430. {
  431. mFalling = false;
  432. }
  433. else
  434. {
  435. VectorF vel;
  436. mOwner->getWorldToObj().mulV(mVelocity, &vel);
  437. mFalling = vel.z < fallingSpeedThreshold;
  438. }
  439. // Enter/Leave Liquid
  440. if (!mInWater && mOwner->getContainerInfo().waterCoverage > 0.0f)
  441. {
  442. mInWater = true;
  443. }
  444. else if (mInWater && mOwner->getContainerInfo().waterCoverage <= 0.0f)
  445. {
  446. mInWater = false;
  447. }
  448. }
  449. void PlayerControllerComponent::updatePos(const F32 travelTime)
  450. {
  451. if (!PHYSICSMGR)
  452. return;
  453. PROFILE_SCOPE(PlayerControllerComponent_UpdatePos);
  454. Point3F newPos;
  455. Collision col;
  456. dMemset(&col, 0, sizeof(col));
  457. static CollisionList collisionList;
  458. collisionList.clear();
  459. newPos = mPhysicsRep->move(mVelocity * travelTime, collisionList);
  460. bool haveCollisions = false;
  461. bool wasFalling = mFalling;
  462. if (collisionList.getCount() > 0)
  463. {
  464. mFalling = false;
  465. haveCollisions = true;
  466. //TODO: clean this up so the phys component doesn't have to tell the col interface to do this
  467. CollisionInterface* colInterface = mOwner->getComponent<CollisionInterface>();
  468. if (colInterface)
  469. {
  470. colInterface->handleCollisionList(collisionList, mVelocity);
  471. }
  472. }
  473. if (haveCollisions)
  474. {
  475. // Pick the collision that most closely matches our direction
  476. VectorF velNormal = mVelocity;
  477. velNormal.normalizeSafe();
  478. const Collision *collision = &collisionList[0];
  479. F32 collisionDot = mDot(velNormal, collision->normal);
  480. const Collision *cp = collision + 1;
  481. const Collision *ep = collision + collisionList.getCount();
  482. for (; cp != ep; cp++)
  483. {
  484. F32 dp = mDot(velNormal, cp->normal);
  485. if (dp < collisionDot)
  486. {
  487. collisionDot = dp;
  488. collision = cp;
  489. }
  490. }
  491. // Modify our velocity based on collisions
  492. for (U32 i = 0; i<collisionList.getCount(); ++i)
  493. {
  494. F32 bd = -mDot(mVelocity, collisionList[i].normal);
  495. VectorF dv = collisionList[i].normal * (bd + sNormalElasticity);
  496. mVelocity += dv;
  497. }
  498. // Store the last collision for use later on. The handle collision
  499. // code only expects a single collision object.
  500. if (collisionList.getCount() > 0)
  501. col = collisionList[collisionList.getCount() - 1];
  502. // We'll handle any player-to-player collision, and the last collision
  503. // with other obejct types.
  504. for (U32 i = 0; i<collisionList.getCount(); ++i)
  505. {
  506. Collision& colCheck = collisionList[i];
  507. if (colCheck.object)
  508. {
  509. col = colCheck;
  510. }
  511. }
  512. }
  513. MatrixF newMat;
  514. newMat.setPosition(newPos);
  515. mPhysicsRep->setTransform(newMat);
  516. mOwner->setPosition(newPos);
  517. }
  518. //
  519. void PlayerControllerComponent::setVelocity(const VectorF& vel)
  520. {
  521. mVelocity = vel;
  522. // Clamp against the maximum velocity.
  523. if (mMaxVelocity > 0)
  524. {
  525. F32 len = mVelocity.magnitudeSafe();
  526. if (len > mMaxVelocity)
  527. {
  528. Point3F excess = mVelocity * (1.0f - (mMaxVelocity / len));
  529. mVelocity -= excess;
  530. }
  531. }
  532. setMaskBits(VelocityMask);
  533. }
  534. void PlayerControllerComponent::findContact(bool *run, bool *jump, VectorF *contactNormal)
  535. {
  536. SceneObject *contactObject = NULL;
  537. Vector<SceneObject*> overlapObjects;
  538. mPhysicsRep->findContact(&contactObject, contactNormal, &overlapObjects);
  539. F32 vd = (*contactNormal).z;
  540. *run = vd > mCos(mDegToRad(moveSurfaceAngle));
  541. *jump = vd > mCos(mDegToRad(contactSurfaceAngle));
  542. // Check for triggers
  543. for (U32 i = 0; i < overlapObjects.size(); i++)
  544. {
  545. SceneObject *obj = overlapObjects[i];
  546. U32 objectMask = obj->getTypeMask();
  547. // Check: triggers, corpses and items...
  548. //
  549. if (objectMask & TriggerObjectType)
  550. {
  551. if (Trigger* pTrigger = dynamic_cast<Trigger*>(obj))
  552. {
  553. pTrigger->potentialEnterObject(mOwner);
  554. }
  555. else if (CollisionTrigger* pTriggerEx = dynamic_cast<CollisionTrigger*>(obj))
  556. {
  557. if (pTriggerEx)
  558. pTriggerEx->potentialEnterObject(mOwner);
  559. }
  560. //Add any other custom classes and the sort here that should be filtered against
  561. /*else if (TriggerExample* pTriggerEx = dynamic_cast<TriggerExample*>(obj))
  562. {
  563. if (pTriggerEx)
  564. pTriggerEx->potentialEnterObject(mOwner);
  565. }*/
  566. }
  567. }
  568. mContactInfo.contacted = contactObject != NULL;
  569. mContactInfo.contactObject = contactObject;
  570. if (mContactInfo.contacted)
  571. mContactInfo.contactNormal = *contactNormal;
  572. }
  573. void PlayerControllerComponent::applyImpulse(const Point3F &pos, const VectorF &vec)
  574. {
  575. AssertFatal(!mIsNaN(vec), "Player::applyImpulse() - The vector is NaN!");
  576. // Players ignore angular velocity
  577. VectorF vel;
  578. vel.x = vec.x / getMass();
  579. vel.y = vec.y / getMass();
  580. vel.z = vec.z / getMass();
  581. // Make sure the impulse isn't too bigg
  582. F32 len = vel.magnitudeSafe();
  583. if (len > sMaxImpulseVelocity)
  584. {
  585. Point3F excess = vel * (1.0f - (sMaxImpulseVelocity / len));
  586. vel -= excess;
  587. }
  588. setVelocity(mVelocity + vel);
  589. }
  590. DefineEngineMethod(PlayerControllerComponent, applyImpulse, bool, (Point3F pos, VectorF vel), ,
  591. "@brief Apply an impulse to this object as defined by a world position and velocity vector.\n\n"
  592. "@param pos impulse world position\n"
  593. "@param vel impulse velocity (impulse force F = m * v)\n"
  594. "@return Always true\n"
  595. "@note Not all objects that derrive from GameBase have this defined.\n")
  596. {
  597. object->applyImpulse(pos, vel);
  598. return true;
  599. }
  600. DefineEngineMethod(PlayerControllerComponent, getContactNormal, Point3F, (), ,
  601. "@brief Apply an impulse to this object as defined by a world position and velocity vector.\n\n"
  602. "@param pos impulse world position\n"
  603. "@param vel impulse velocity (impulse force F = m * v)\n"
  604. "@return Always true\n"
  605. "@note Not all objects that derrive from GameBase have this defined.\n")
  606. {
  607. return object->getContactNormal();
  608. }
  609. DefineEngineMethod(PlayerControllerComponent, getContactObject, SceneObject*, (), ,
  610. "@brief Apply an impulse to this object as defined by a world position and velocity vector.\n\n"
  611. "@param pos impulse world position\n"
  612. "@param vel impulse velocity (impulse force F = m * v)\n"
  613. "@return Always true\n"
  614. "@note Not all objects that derrive from GameBase have this defined.\n")
  615. {
  616. return object->getContactObject();
  617. }
  618. DefineEngineMethod(PlayerControllerComponent, isContacted, bool, (), ,
  619. "@brief Apply an impulse to this object as defined by a world position and velocity vector.\n\n"
  620. "@param pos impulse world position\n"
  621. "@param vel impulse velocity (impulse force F = m * v)\n"
  622. "@return Always true\n"
  623. "@note Not all objects that derrive from GameBase have this defined.\n")
  624. {
  625. return object->isContacted();
  626. }