pathShape.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine
  3. // Copyright (C) GarageGames.com, Inc.
  4. // @author Stefan "Beffy" Moises
  5. // this is a modified version of PathCamera that allows to move shapes along paths
  6. //-----------------------------------------------------------------------------
  7. #include "platform/platform.h"
  8. #include "math/mMath.h"
  9. #include "math/mathIO.h"
  10. #include "console/simBase.h"
  11. #include "console/console.h"
  12. #include "console/consoleTypes.h"
  13. #include "core/stream/bitStream.h"
  14. #include "core/dnet.h"
  15. #include "scene/pathManager.h"
  16. #include "T3D/gameFunctions.h"
  17. #include "T3D/gameBase/gameConnection.h"
  18. #include "gui/worldEditor/editor.h"
  19. #include "console/engineAPI.h"
  20. #include "math/mTransform.h"
  21. #include "T3D/pathShape.h"
  22. //----------------------------------------------------------------------------
  23. IMPLEMENT_CO_DATABLOCK_V1(PathShapeData);
  24. void PathShapeData::consoleInit()
  25. {
  26. }
  27. bool PathShapeData::preload(bool server, String &errorStr)
  28. {
  29. if(!Parent::preload(server, errorStr))
  30. return false;
  31. return true;
  32. }
  33. void PathShapeData::initPersistFields()
  34. {
  35. Parent::initPersistFields();
  36. }
  37. void PathShapeData::packData(BitStream* stream)
  38. {
  39. Parent::packData(stream);
  40. }
  41. void PathShapeData::unpackData(BitStream* stream)
  42. {
  43. Parent::unpackData(stream);
  44. }
  45. //----------------------------------------------------------------------------
  46. IMPLEMENT_CO_NETOBJECT_V1(PathShape);
  47. PathShape::PathShape()
  48. {
  49. mNetFlags.set(Ghostable|ScopeAlways);
  50. mTypeMask |= PathShapeObjectType | StaticShapeObjectType;
  51. delta.time = 0;
  52. delta.timeVec = 0;
  53. mDataBlock = NULL;
  54. mState = Forward;
  55. mNodeBase = 0;
  56. mNodeCount = 0;
  57. mPosition = 0;
  58. mTarget = 0;
  59. mTargetSet = false;
  60. MatrixF mat = MatrixF::Identity;
  61. mLastXform = MatrixF::Identity;
  62. Parent::setTransform(mat);
  63. for (U32 i = 0; i < 4; i++)
  64. {
  65. mControl[i] = StringTable->insert("");
  66. }
  67. }
  68. PathShape::~PathShape()
  69. {
  70. }
  71. //----------------------------------------------------------------------------
  72. bool PathShape::onAdd()
  73. {
  74. if(!Parent::onAdd() && !mDataBlock)
  75. return false;
  76. mTypeMask |= PathShapeObjectType | StaticShapeObjectType;
  77. // Initialize from the current transform.
  78. if (!mNodeCount) {
  79. QuatF rot(getTransform());
  80. Point3F pos = getPosition();
  81. mSpline.removeAll();
  82. mSpline.push_back(new CameraSpline::Knot(pos,rot,1,
  83. CameraSpline::Knot::NORMAL, CameraSpline::Knot::SPLINE));
  84. mNodeCount = 1;
  85. }
  86. if (isServerObject()) scriptOnAdd();
  87. return true;
  88. }
  89. void PathShape::onRemove()
  90. {
  91. scriptOnRemove();
  92. removeFromScene();
  93. unmount();
  94. Parent::onRemove();
  95. if (isGhost())
  96. for (S32 i = 0; i < MaxSoundThreads; i++)
  97. stopAudio(i);
  98. }
  99. bool PathShape::onNewDataBlock(GameBaseData* dptr, bool reload)
  100. {
  101. mDataBlock = dynamic_cast<PathShapeData*>(dptr);
  102. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  103. return false;
  104. scriptOnNewDataBlock();
  105. return true;
  106. }
  107. PathShapeData::PathShapeData()
  108. {
  109. }
  110. //----------------------------------------------------------------------------
  111. void PathShape::initPersistFields()
  112. {
  113. addField( "Path", TYPEID< SimObjectRef<SimPath::Path> >(), Offset( mSimPath, PathShape ),
  114. "@brief Name of a Path to follow." );
  115. addField("Controler", TypeString, Offset(mControl, PathShape), 4, "controlers");
  116. Parent::initPersistFields();
  117. }
  118. void PathShape::consoleInit()
  119. {
  120. }
  121. //----------------------------------------------------------------------------
  122. void PathShape::processTick(const Move* move)
  123. {
  124. // client and server
  125. Parent::processTick(move);
  126. // Move to new time
  127. advancePosition(TickMs);
  128. MatrixF mat;
  129. interpolateMat(mPosition,&mat);
  130. Parent::setTransform(mat);
  131. updateContainer();
  132. }
  133. void PathShape::interpolateTick(F32 dt)
  134. {
  135. Parent::interpolateTick(dt);
  136. MatrixF mat;
  137. interpolateMat(delta.time + (delta.timeVec * dt),&mat);
  138. Parent::setRenderTransform(mat);
  139. }
  140. void PathShape::interpolateMat(F32 pos,MatrixF* mat)
  141. {
  142. CameraSpline::Knot knot;
  143. mSpline.value(pos - mNodeBase,&knot);
  144. knot.mRotation.setMatrix(mat);
  145. mat->setPosition(knot.mPosition);
  146. }
  147. void PathShape::advancePosition(S32 ms)
  148. {
  149. delta.timeVec = mPosition;
  150. // Advance according to current speed
  151. if (mState == Forward) {
  152. mPosition = mSpline.advanceTime(mPosition - mNodeBase,ms);
  153. if (mPosition > F32(mNodeCount - 1))
  154. mPosition = F32(mNodeCount - 1);
  155. mPosition += (F32)mNodeBase;
  156. if (mTargetSet && mPosition >= mTarget) {
  157. mTargetSet = false;
  158. mPosition = mTarget;
  159. mState = Stop;
  160. }
  161. }
  162. else
  163. if (mState == Backward) {
  164. mPosition = mSpline.advanceTime(mPosition - mNodeBase,-ms);
  165. if (mPosition < 0)
  166. mPosition = 0;
  167. mPosition += mNodeBase;
  168. if (mTargetSet && mPosition <= mTarget) {
  169. mTargetSet = false;
  170. mPosition = mTarget;
  171. mState = Stop;
  172. }
  173. }
  174. // Script callbacks
  175. if (int(mPosition) != int(delta.timeVec))
  176. onNode(int(mPosition));
  177. // Set frame interpolation
  178. delta.time = mPosition;
  179. delta.timeVec -= mPosition;
  180. }
  181. //----------------------------------------------------------------------------
  182. void PathShape::setPosition(F32 pos)
  183. {
  184. mPosition = mClampF(pos,mNodeBase,mNodeBase + mNodeCount - 1);
  185. MatrixF mat;
  186. interpolateMat(mPosition,&mat);
  187. Parent::setTransform(mat);
  188. setMaskBits(PositionMask);
  189. }
  190. void PathShape::setTarget(F32 pos)
  191. {
  192. mTarget = pos;
  193. mTargetSet = true;
  194. if (mTarget > mPosition)
  195. mState = Forward;
  196. else
  197. if (mTarget < mPosition)
  198. mState = Backward;
  199. else {
  200. mTargetSet = false;
  201. mState = Stop;
  202. }
  203. setMaskBits(TargetMask | StateMask);
  204. }
  205. void PathShape::setState(State s)
  206. {
  207. mState = s;
  208. setMaskBits(StateMask);
  209. }
  210. S32 PathShape::getState()
  211. {
  212. return mState;
  213. }
  214. //-----------------------------------------------------------------------------
  215. void PathShape::reset(F32 speed)
  216. {
  217. CameraSpline::Knot *knot = new CameraSpline::Knot;
  218. mSpline.value(mPosition - mNodeBase,knot);
  219. if (speed)
  220. knot->mSpeed = speed;
  221. mSpline.removeAll();
  222. mSpline.push_back(knot);
  223. mNodeBase = 0;
  224. mNodeCount = 1;
  225. mPosition = 0;
  226. mTargetSet = false;
  227. mState = Forward;
  228. setMaskBits(StateMask | PositionMask | WindowMask | TargetMask);
  229. }
  230. void PathShape::pushBack(CameraSpline::Knot *knot)
  231. {
  232. // Make room at the end
  233. if (mNodeCount == NodeWindow) {
  234. delete mSpline.remove(mSpline.getKnot(0));
  235. mNodeBase++;
  236. }
  237. else
  238. mNodeCount++;
  239. // Fill in the new node
  240. mSpline.push_back(knot);
  241. setMaskBits(WindowMask);
  242. // Make sure the position doesn't fall off
  243. if (mPosition < mNodeBase) {
  244. mPosition = mNodeBase;
  245. setMaskBits(PositionMask);
  246. }
  247. }
  248. void PathShape::pushFront(CameraSpline::Knot *knot)
  249. {
  250. // Make room at the front
  251. if (mNodeCount == NodeWindow)
  252. delete mSpline.remove(mSpline.getKnot(mNodeCount));
  253. else
  254. mNodeCount++;
  255. mNodeBase--;
  256. // Fill in the new node
  257. mSpline.push_front(knot);
  258. setMaskBits(WindowMask);
  259. // Make sure the position doesn't fall off
  260. if (mPosition > mNodeBase + (NodeWindow - 1)) {
  261. mPosition = mNodeBase + (NodeWindow - 1);
  262. setMaskBits(PositionMask);
  263. }
  264. }
  265. void PathShape::popFront()
  266. {
  267. if (mNodeCount < 2)
  268. return;
  269. // Remove the first node. Node base and position are unaffected.
  270. mNodeCount--;
  271. delete mSpline.remove(mSpline.getKnot(0));
  272. }
  273. //----------------------------------------------------------------------------
  274. void PathShape::onNode(S32 node)
  275. {
  276. if (!isGhost())
  277. {
  278. Con::executef(mDataBlock, "onNode", getIdString(), Con::getIntArg(node));
  279. Con::executef(mDataBlock, mSpline.getKnot(node)->mHitCommand.c_str(), getIdString());
  280. }
  281. }
  282. //----------------------------------------------------------------------------
  283. //----------------------------------------------------------------------------
  284. U32 PathShape::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
  285. {
  286. Parent::packUpdate(con,mask,stream);
  287. if (stream->writeFlag(mask & StateMask))
  288. stream->writeInt(mState,StateBits);
  289. if (stream->writeFlag(mask & PositionMask))
  290. stream->write(mPosition);
  291. if (stream->writeFlag(mask & TargetMask))
  292. if (stream->writeFlag(mTargetSet))
  293. stream->write(mTarget);
  294. if (stream->writeFlag(mask & WindowMask)) {
  295. stream->write(mNodeBase);
  296. stream->write(mNodeCount);
  297. for (S32 i = 0; i < mNodeCount; i++) {
  298. CameraSpline::Knot *knot = mSpline.getKnot(i);
  299. mathWrite(*stream, knot->mPosition);
  300. mathWrite(*stream, knot->mRotation);
  301. stream->write(knot->mSpeed);
  302. stream->writeInt(knot->mType, CameraSpline::Knot::NUM_TYPE_BITS);
  303. stream->writeInt(knot->mPath, CameraSpline::Knot::NUM_PATH_BITS);
  304. }
  305. }
  306. // The rest of the data is part of the control object packet update.
  307. // If we're controlled by this client, we don't need to send it.
  308. if(stream->writeFlag(getControllingClient() == con && !(mask & InitialUpdateMask)))
  309. return 0;
  310. return 0;
  311. }
  312. void PathShape::unpackUpdate(NetConnection *con, BitStream *stream)
  313. {
  314. Parent::unpackUpdate(con,stream);
  315. // StateMask
  316. if (stream->readFlag())
  317. mState = stream->readInt(StateBits);
  318. // PositionMask
  319. if (stream->readFlag()) {
  320. stream->read(&mPosition);
  321. delta.time = mPosition;
  322. delta.timeVec = 0;
  323. }
  324. // TargetMask
  325. if (stream->readFlag()) {
  326. mTargetSet = stream->readFlag();
  327. if (mTargetSet) {
  328. stream->read(&mTarget);
  329. }
  330. }
  331. // WindowMask
  332. if (stream->readFlag()) {
  333. mSpline.removeAll();
  334. stream->read(&mNodeBase);
  335. stream->read(&mNodeCount);
  336. for (S32 i = 0; i < mNodeCount; i++) {
  337. CameraSpline::Knot *knot = new CameraSpline::Knot();
  338. mathRead(*stream, &knot->mPosition);
  339. mathRead(*stream, &knot->mRotation);
  340. stream->read(&knot->mSpeed);
  341. knot->mType = (CameraSpline::Knot::Type)stream->readInt(CameraSpline::Knot::NUM_TYPE_BITS);
  342. knot->mPath = (CameraSpline::Knot::Path)stream->readInt(CameraSpline::Knot::NUM_PATH_BITS);
  343. mSpline.push_back(knot);
  344. }
  345. }
  346. // Controlled by the client?
  347. if (stream->readFlag()) return;
  348. }
  349. //-----------------------------------------------------------------------------
  350. // Console access methods
  351. //-----------------------------------------------------------------------------
  352. DefineEngineMethod(PathShape, setPosition, void, (F32 position), (0.0f), "Set the current position of the camera along the path.\n"
  353. "@param position Position along the path, from 0.0 (path start) - 1.0 (path end), to place the camera.\n"
  354. "@tsexample\n"
  355. "// Set the camera on a position along its path from 0.0 - 1.0.\n"
  356. "%position = \"0.35\";\n\n"
  357. "// Force the pathCamera to its new position along the path.\n"
  358. "%pathCamera.setPosition(%position);\n"
  359. "@endtsexample\n")
  360. {
  361. object->setPosition(position);
  362. }
  363. DefineEngineMethod(PathShape, setTarget, void, (F32 position), (1.0f), "@brief Set the movement target for this camera along its path.\n\n"
  364. "The camera will attempt to move along the path to the given target in the direction provided "
  365. "by setState() (the default is forwards). Once the camera moves past this target it will come "
  366. "to a stop, and the target state will be cleared.\n"
  367. "@param position Target position, between 0.0 (path start) and 1.0 (path end), for the camera to move to along its path.\n"
  368. "@tsexample\n"
  369. "// Set the position target, between 0.0 (path start) and 1.0 (path end), for this camera to move to.\n"
  370. "%position = \"0.50\";\n\n"
  371. "// Inform the pathCamera of the new target position it will move to.\n"
  372. "%pathCamera.setTarget(%position);\n"
  373. "@endtsexample\n")
  374. {
  375. object->setTarget(position);
  376. }
  377. DefineEngineMethod(PathShape, setState, void, (const char* newState), ("forward"), "Set the movement state for this path camera.\n"
  378. "@param newState New movement state type for this camera. Forward, Backward or Stop.\n"
  379. "@tsexample\n"
  380. "// Set the state type (forward, backward, stop).\n"
  381. "// In this example, the camera will travel from the first node\n"
  382. "// to the last node (or target if given with setTarget())\n"
  383. "%state = \"forward\";\n\n"
  384. "// Inform the pathCamera to change its movement state to the defined value.\n"
  385. "%pathCamera.setState(%state);\n"
  386. "@endtsexample\n")
  387. {
  388. if (!dStricmp(newState, "forward"))
  389. object->setState(PathShape::Forward);
  390. else
  391. if (!dStricmp(newState, "backward"))
  392. object->setState(PathShape::Backward);
  393. else
  394. object->setState(PathShape::Stop);
  395. }
  396. DefineEngineMethod(PathShape, reset, void, (F32 speed), (1.0f), "@brief Clear the camera's path and set the camera's current transform as the start of the new path.\n\n"
  397. "What specifically occurs is a new knot is created from the camera's current transform. Then the current path "
  398. "is cleared and the new knot is pushed onto the path. Any previous target is cleared and the camera's movement "
  399. "state is set to Forward. The camera is now ready for a new path to be defined.\n"
  400. "@param speed Speed for the camera to move along its path after being reset.\n"
  401. "@tsexample\n"
  402. "//Determine the new movement speed of this camera. If not set, the speed will default to 1.0.\n"
  403. "%speed = \"0.50\";\n\n"
  404. "// Inform the path camera to start a new path at"
  405. "// the camera's current position, and set the new "
  406. "// path's speed value.\n"
  407. "%pathCamera.reset(%speed);\n"
  408. "@endtsexample\n")
  409. {
  410. object->reset(speed);
  411. }
  412. static CameraSpline::Knot::Type resolveKnotType(const char *arg)
  413. {
  414. if (dStricmp(arg, "Position Only") == 0)
  415. return CameraSpline::Knot::POSITION_ONLY;
  416. if (dStricmp(arg, "Kink") == 0)
  417. return CameraSpline::Knot::KINK;
  418. return CameraSpline::Knot::NORMAL;
  419. }
  420. static CameraSpline::Knot::Path resolveKnotPath(const char *arg)
  421. {
  422. if (!dStricmp(arg, "Linear"))
  423. return CameraSpline::Knot::LINEAR;
  424. return CameraSpline::Knot::SPLINE;
  425. }
  426. DefineEngineMethod(PathShape, pushBack, void, (TransformF transform, F32 speed, const char* type, const char* path, const char *hitCommand),
  427. (TransformF::Identity, 1.0f, "Normal", "Linear",""),
  428. "@brief Adds a new knot to the back of a path camera's path.\n"
  429. "@param transform Transform for the new knot. In the form of \"x y z ax ay az aa\" such as returned by SceneObject::getTransform()\n"
  430. "@param speed Speed setting for this knot.\n"
  431. "@param type Knot type (Normal, Position Only, Kink).\n"
  432. "@param path %Path type (Linear, Spline).\n"
  433. "@tsexample\n"
  434. "// Transform vector for new knot. (Pos_X Pos_Y Pos_Z Rot_X Rot_Y Rot_Z Angle)\n"
  435. "%transform = \"15.0 5.0 5.0 1.4 1.0 0.2 1.0\"\n\n"
  436. "// Speed setting for knot.\n"
  437. "%speed = \"1.0\"\n\n"
  438. "// Knot type. (Normal, Position Only, Kink)\n"
  439. "%type = \"Normal\";\n\n"
  440. "// Path Type. (Linear, Spline)\n"
  441. "%path = \"Linear\";\n\n"
  442. "// Inform the path camera to add a new knot to the back of its path\n"
  443. "%pathCamera.pushBack(%transform,%speed,%type,%path);\n"
  444. "@endtsexample\n")
  445. {
  446. QuatF rot(transform.getOrientation());
  447. object->pushBack(new CameraSpline::Knot(transform.getPosition(), rot, speed, resolveKnotType(type), resolveKnotPath(path), String(hitCommand)));
  448. }
  449. DefineEngineMethod(PathShape, pushFront, void, (TransformF transform, F32 speed, const char* type, const char* path),
  450. (1.0f, "Normal", "Linear"),
  451. "@brief Adds a new knot to the front of a path camera's path.\n"
  452. "@param transform Transform for the new knot. In the form of \"x y z ax ay az aa\" such as returned by SceneObject::getTransform()\n"
  453. "@param speed Speed setting for this knot.\n"
  454. "@param type Knot type (Normal, Position Only, Kink).\n"
  455. "@param path %Path type (Linear, Spline).\n"
  456. "@tsexample\n"
  457. "// Transform vector for new knot. (Pos_X,Pos_Y,Pos_Z,Rot_X,Rot_Y,Rot_Z,Angle)\n"
  458. "%transform = \"15.0 5.0 5.0 1.4 1.0 0.2 1.0\"\n\n"
  459. "// Speed setting for knot.\n"
  460. "%speed = \"1.0\";\n\n"
  461. "// Knot type. (Normal, Position Only, Kink)\n"
  462. "%type = \"Normal\";\n\n"
  463. "// Path Type. (Linear, Spline)\n"
  464. "%path = \"Linear\";\n\n"
  465. "// Inform the path camera to add a new knot to the front of its path\n"
  466. "%pathCamera.pushFront(%transform, %speed, %type, %path);\n"
  467. "@endtsexample\n")
  468. {
  469. QuatF rot(transform.getOrientation());
  470. object->pushFront(new CameraSpline::Knot(transform.getPosition(), rot, speed, resolveKnotType(type), resolveKnotPath(path)));
  471. }
  472. DefineEngineMethod(PathShape, popFront, void, (), , "Removes the knot at the front of the camera's path.\n"
  473. "@tsexample\n"
  474. "// Remove the first knot in the camera's path.\n"
  475. "%pathCamera.popFront();\n"
  476. "@endtsexample\n")
  477. {
  478. object->popFront();
  479. }
  480. DefineEngineMethod(PathShape, getState, S32, (), , "PathShape.getState()")
  481. {
  482. return object->getState();
  483. }