CCPUForceField.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  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 FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "CCPUForceField.h"
  23. NS_CC_BEGIN
  24. const Vec3 PUForceFieldCalculationFactory::DEFAULT_WORLDSIZE(500.0f, 500.0f, 500.0f);
  25. //-----------------------------------------------------------------------
  26. unsigned short PUForceFieldCalculationFactory::getOctaves(void) const
  27. {
  28. return _octaves;
  29. }
  30. //-----------------------------------------------------------------------
  31. void PUForceFieldCalculationFactory::setOctaves(unsigned short octaves)
  32. {
  33. _octaves = octaves;
  34. generate(getForceFieldSize(), _octaves, _frequency, _amplitude, _persistence, _worldSize);
  35. }
  36. //-----------------------------------------------------------------------
  37. double PUForceFieldCalculationFactory::getFrequency(void) const
  38. {
  39. return _frequency;
  40. }
  41. //-----------------------------------------------------------------------
  42. void PUForceFieldCalculationFactory::setFrequency(double frequency)
  43. {
  44. _frequency = frequency;
  45. generate(getForceFieldSize(), _octaves, _frequency, _amplitude, _persistence, _worldSize);
  46. }
  47. //-----------------------------------------------------------------------
  48. double PUForceFieldCalculationFactory::getAmplitude(void) const
  49. {
  50. return _amplitude;
  51. }
  52. //-----------------------------------------------------------------------
  53. void PUForceFieldCalculationFactory::setAmplitude(double amplitude)
  54. {
  55. _amplitude = amplitude;
  56. generate(getForceFieldSize(), _octaves, _frequency, _amplitude, _persistence, _worldSize);
  57. }
  58. //-----------------------------------------------------------------------
  59. double PUForceFieldCalculationFactory::getPersistence(void) const
  60. {
  61. return _persistence;
  62. }
  63. //-----------------------------------------------------------------------
  64. void PUForceFieldCalculationFactory::setPersistence(double persistence)
  65. {
  66. _persistence = persistence;
  67. generate(getForceFieldSize(), _octaves, _frequency, _amplitude, _persistence, _worldSize);
  68. }
  69. //-----------------------------------------------------------------------
  70. unsigned int PUForceFieldCalculationFactory::getForceFieldSize(void) const
  71. {
  72. return 1; // Return default cubic size
  73. }
  74. //-----------------------------------------------------------------------
  75. void PUForceFieldCalculationFactory::setForceFieldSize(unsigned int forceFieldSize)
  76. {
  77. // The forcefield cannot be zero
  78. if (forceFieldSize == 0)
  79. return;
  80. generate(forceFieldSize, _octaves, _frequency, _amplitude, _persistence, _worldSize);
  81. }
  82. //-----------------------------------------------------------------------
  83. Vec3 PUForceFieldCalculationFactory::getWorldSize(void) const
  84. {
  85. return _worldSize;
  86. }
  87. //-----------------------------------------------------------------------
  88. void PUForceFieldCalculationFactory::setWorldSize(const Vec3& worldSize)
  89. {
  90. // The worldsize cannot be zero
  91. if (worldSize == Vec3::ZERO)
  92. return;
  93. _worldSize = worldSize;
  94. generate(getForceFieldSize(), _octaves, _frequency, _amplitude, _persistence, _worldSize);
  95. }
  96. //-----------------------------------------------------------------------
  97. //-----------------------------------------------------------------------
  98. //-----------------------------------------------------------------------
  99. void PURealTimeForceFieldCalculationFactory::generate(unsigned int /*forceFieldSize*/,
  100. unsigned short octaves,
  101. double frequency,
  102. double amplitude,
  103. double persistence,
  104. const Vec3& worldSize)
  105. {
  106. _octaves = octaves;
  107. _frequency = frequency;
  108. _amplitude = amplitude;
  109. _persistence = persistence;
  110. _worldSize = worldSize;
  111. _noise3D.initialise(octaves, frequency, amplitude, persistence);
  112. if (worldSize != Vec3::ZERO)
  113. {
  114. _mapScale.x = 1.0f / worldSize.x; // Remark: forceFieldSize is not used, because it is a unit cube
  115. _mapScale.y = 1.0f / worldSize.y;
  116. _mapScale.z = 1.0f / worldSize.z;
  117. }
  118. }
  119. //-----------------------------------------------------------------------
  120. void PURealTimeForceFieldCalculationFactory::determineForce(const Vec3& position, Vec3& force, float delta)
  121. {
  122. _mappedPosition.x = _mapScale.x * position.x;
  123. _mappedPosition.y = _mapScale.y * position.y;
  124. _mappedPosition.z = _mapScale.z * position.z;
  125. if (_mappedPosition.x < 0.0f || _mappedPosition.x > 1.0f ||
  126. _mappedPosition.y < 0.0f || _mappedPosition.y > 1.0f ||
  127. _mappedPosition.z < 0.0f || _mappedPosition.z > 1.0f)
  128. {
  129. // Position is outside the forcefield (outside the unit cube)
  130. return;
  131. }
  132. force.x = (float)(_noise3D.noise(_mappedPosition.x + delta, _mappedPosition.y, _mappedPosition.z) -
  133. _noise3D.noise(_mappedPosition.x - delta, _mappedPosition.y, _mappedPosition.z));
  134. force.y = (float)(_noise3D.noise(_mappedPosition.x, _mappedPosition.y + delta, _mappedPosition.z) -
  135. _noise3D.noise(_mappedPosition.x, _mappedPosition.y - delta, _mappedPosition.z));
  136. force.z = (float)(_noise3D.noise(_mappedPosition.x, _mappedPosition.y, _mappedPosition.z + delta) -
  137. _noise3D.noise(_mappedPosition.x, _mappedPosition.y, _mappedPosition.z - delta));
  138. }
  139. //-----------------------------------------------------------------------
  140. //-----------------------------------------------------------------------
  141. //-----------------------------------------------------------------------
  142. PUForceField::PUForceField(void) :
  143. _octaves(2),
  144. _frequency(1.0f),
  145. _amplitude(1.0f),
  146. _persistence(1.0f),
  147. _worldSize(PUForceFieldCalculationFactory::DEFAULT_WORLDSIZE),
  148. _forceFieldSize(64),
  149. _forceFieldCalculationFactory(0),
  150. _forceFieldType(FF_REALTIME_CALC)
  151. {
  152. }
  153. //-----------------------------------------------------------------------
  154. PUForceField::~PUForceField(void)
  155. {
  156. if (_forceFieldCalculationFactory)
  157. {
  158. delete _forceFieldCalculationFactory;
  159. }
  160. }
  161. //-----------------------------------------------------------------------
  162. void PUForceField::initialise(ForceFieldType type,
  163. const Vec3& position,
  164. unsigned int forceFieldSize,
  165. unsigned short octaves,
  166. double frequency,
  167. double amplitude,
  168. double persistence,
  169. const Vec3& worldSize)
  170. {
  171. // Initialise first
  172. initialise(type, forceFieldSize, octaves, frequency, amplitude, persistence, worldSize);
  173. // Store the base and max position of the forcefield.
  174. _forceFieldPositionBase = position;
  175. _forceFieldPositionBase.x -= 0.5f * worldSize.x;
  176. _forceFieldPositionBase.y -= 0.5f * worldSize.y;
  177. _forceFieldPositionBase.z -= 0.5f * worldSize.z;
  178. }
  179. //-----------------------------------------------------------------------
  180. void PUForceField::initialise(ForceFieldType type,
  181. unsigned int forceFieldSize,
  182. unsigned short octaves,
  183. double frequency,
  184. double amplitude,
  185. double persistence,
  186. const Vec3& worldSize)
  187. {
  188. // Create a factory
  189. _forceFieldCalculationFactory = createForceFieldCalculationFactory(type);
  190. // Create all necessary data for the force field
  191. _forceFieldCalculationFactory->generate(forceFieldSize, octaves, frequency, amplitude, persistence, worldSize);
  192. }
  193. //-----------------------------------------------------------------------
  194. const Vec3& PUForceField::getForceFieldPositionBase(void) const
  195. {
  196. return _forceFieldPositionBase;
  197. }
  198. //-----------------------------------------------------------------------
  199. void PUForceField::setForceFieldPositionBase(const Vec3& position)
  200. {
  201. _forceFieldPositionBase = position;
  202. }
  203. //-----------------------------------------------------------------------
  204. void PUForceField::determineForce(const Vec3& position, Vec3& force, float delta)
  205. {
  206. force.x = 0.0f;
  207. force.y = 0.0f;
  208. force.z = 0.0f;
  209. if (_forceFieldCalculationFactory)
  210. {
  211. _forceFieldCalculationFactory->determineForce(position - _forceFieldPositionBase, force, delta);
  212. }
  213. }
  214. //-----------------------------------------------------------------------
  215. PUForceFieldCalculationFactory* PUForceField::getForceFieldCalculationFactory() const
  216. {
  217. return _forceFieldCalculationFactory;
  218. }
  219. //-----------------------------------------------------------------------
  220. void PUForceField::setForceFieldCalculationFactory(PUForceFieldCalculationFactory* forceFieldCalculationFactory)
  221. {
  222. if (_forceFieldCalculationFactory)
  223. {
  224. delete _forceFieldCalculationFactory;
  225. }
  226. _forceFieldCalculationFactory = forceFieldCalculationFactory;
  227. }
  228. //-----------------------------------------------------------------------
  229. PUForceFieldCalculationFactory* PUForceField::createForceFieldCalculationFactory(ForceFieldType type)
  230. {
  231. _forceFieldType = type;
  232. if (type == FF_MATRIX_CALC)
  233. {
  234. // Use precreated matrix
  235. //setForceFieldCalculationFactory(new MatrixForceFieldCalculationFactory());
  236. return getForceFieldCalculationFactory();
  237. }
  238. else
  239. {
  240. // Use realtime calculation
  241. setForceFieldCalculationFactory(new (std::nothrow) PURealTimeForceFieldCalculationFactory());
  242. return getForceFieldCalculationFactory();
  243. }
  244. }
  245. //-----------------------------------------------------------------------
  246. PUForceField::ForceFieldType PUForceField::getForceFieldType() const
  247. {
  248. return _forceFieldType;
  249. }
  250. //-----------------------------------------------------------------------
  251. void PUForceField::setForceFieldType(const PUForceField::ForceFieldType forceFieldType)
  252. {
  253. _forceFieldType = forceFieldType;
  254. if (_forceFieldCalculationFactory)
  255. {
  256. initialise(_forceFieldType, _forceFieldSize, _octaves, _frequency, _amplitude, _persistence, _worldSize);
  257. }
  258. }
  259. //-----------------------------------------------------------------------
  260. unsigned short PUForceField::getOctaves(void) const
  261. {
  262. return _octaves;
  263. }
  264. //-----------------------------------------------------------------------
  265. void PUForceField::setOctaves(unsigned short octaves)
  266. {
  267. _octaves = octaves;
  268. if (_forceFieldCalculationFactory)
  269. {
  270. _forceFieldCalculationFactory->setOctaves(octaves);
  271. }
  272. }
  273. //-----------------------------------------------------------------------
  274. double PUForceField::getFrequency(void) const
  275. {
  276. return _frequency;
  277. }
  278. //-----------------------------------------------------------------------
  279. void PUForceField::setFrequency(double frequency)
  280. {
  281. _frequency = frequency;
  282. if (_forceFieldCalculationFactory)
  283. {
  284. _forceFieldCalculationFactory->setFrequency(frequency);
  285. }
  286. }
  287. //-----------------------------------------------------------------------
  288. double PUForceField::getAmplitude(void) const
  289. {
  290. return _amplitude;
  291. }
  292. //-----------------------------------------------------------------------
  293. void PUForceField::setAmplitude(double amplitude)
  294. {
  295. _amplitude = amplitude;
  296. if (_forceFieldCalculationFactory)
  297. {
  298. _forceFieldCalculationFactory->setAmplitude(amplitude);
  299. }
  300. }
  301. //-----------------------------------------------------------------------
  302. double PUForceField::getPersistence(void) const
  303. {
  304. return _persistence;
  305. }
  306. //-----------------------------------------------------------------------
  307. void PUForceField::setPersistence(double persistence)
  308. {
  309. _persistence = persistence;
  310. if (_forceFieldCalculationFactory)
  311. {
  312. _forceFieldCalculationFactory->setPersistence(persistence);
  313. }
  314. }
  315. //-----------------------------------------------------------------------
  316. unsigned int PUForceField::getForceFieldSize(void) const
  317. {
  318. return _forceFieldSize;
  319. }
  320. //-----------------------------------------------------------------------
  321. void PUForceField::setForceFieldSize(unsigned int forceFieldSize)
  322. {
  323. _forceFieldSize = forceFieldSize;
  324. if (_forceFieldCalculationFactory)
  325. {
  326. _forceFieldCalculationFactory->setForceFieldSize(forceFieldSize);
  327. }
  328. }
  329. //-----------------------------------------------------------------------
  330. Vec3 PUForceField::getWorldSize(void) const
  331. {
  332. return _worldSize;
  333. }
  334. //-----------------------------------------------------------------------
  335. void PUForceField::setWorldSize(const Vec3& worldSize)
  336. {
  337. _worldSize = worldSize;
  338. if (_forceFieldCalculationFactory)
  339. {
  340. _forceFieldCalculationFactory->setWorldSize(worldSize);
  341. }
  342. }
  343. NS_CC_END