CCPUForceField.cpp 14 KB

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