CCActionPageTurn3D.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /****************************************************************************
  2. Copyright (c) 2009 Sindesso Pty Ltd http://www.sindesso.com/
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2016 Chukong Technologies Inc.
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "2d/CCActionPageTurn3D.h"
  24. #include "2d/CCGrid.h"
  25. #include "2d/CCNodeGrid.h"
  26. NS_CC_BEGIN
  27. PageTurn3D* PageTurn3D::create(float duration, const Size& gridSize)
  28. {
  29. PageTurn3D *action = new (std::nothrow) PageTurn3D();
  30. if (action && action->initWithDuration(duration, gridSize))
  31. {
  32. action->autorelease();
  33. return action;
  34. }
  35. delete action;
  36. return nullptr;
  37. }
  38. PageTurn3D *PageTurn3D::clone() const
  39. {
  40. // no copy constructor
  41. return PageTurn3D::create(_duration, _gridSize);
  42. }
  43. GridBase* PageTurn3D::getGrid()
  44. {
  45. auto result = Grid3D::create(_gridSize, _gridNodeTarget->getGridRect());
  46. if (result)
  47. {
  48. result->setNeedDepthTestForBlit(true);
  49. }
  50. return result;
  51. }
  52. /*
  53. * Update each tick
  54. * Time is the percentage of the way through the duration
  55. */
  56. void PageTurn3D::update(float time)
  57. {
  58. float tt = MAX(0, time - 0.25f);
  59. float deltaAy = (tt * tt * 500);
  60. float ay = -100 - deltaAy;
  61. float deltaTheta = sqrtf(time);
  62. float theta = deltaTheta > 0.5f ? (float)M_PI_2*deltaTheta : (float)M_PI_2*(1-deltaTheta);
  63. float rotateByYAxis = (2-time)* M_PI;
  64. float sinTheta = sinf(theta);
  65. float cosTheta = cosf(theta);
  66. for (int i = 0; i <= _gridSize.width; ++i)
  67. {
  68. for (int j = 0; j <= _gridSize.height; ++j)
  69. {
  70. // Get original vertex
  71. Vec3 p = getOriginalVertex(Vec2(i ,j));
  72. p.x -= getGridRect().origin.x;
  73. float R = sqrtf((p.x * p.x) + ((p.y - ay) * (p.y - ay)));
  74. float r = R * sinTheta;
  75. float alpha = asinf( p.x / R );
  76. float beta = alpha / sinTheta;
  77. float cosBeta = cosf( beta );
  78. // If beta > PI then we've wrapped around the cone
  79. // Reduce the radius to stop these points interfering with others
  80. if (beta <= M_PI)
  81. {
  82. p.x = ( r * sinf(beta));
  83. }
  84. else
  85. {
  86. // Force X = 0 to stop wrapped
  87. // points
  88. p.x = 0;
  89. }
  90. p.y = ( R + ay - ( r * (1 - cosBeta) * sinTheta));
  91. // We scale z here to avoid the animation being
  92. // too much bigger than the screen due to perspective transform
  93. p.z = (r * ( 1 - cosBeta ) * cosTheta);// "100" didn't work for
  94. p.x = p.z * sinf(rotateByYAxis) + p.x * cosf(rotateByYAxis);
  95. p.z = p.z * cosf(rotateByYAxis) - p.x * sinf(rotateByYAxis);
  96. p.z/=7;
  97. // Stop z coord from dropping beneath underlying page in a transition
  98. // issue #751
  99. if( p.z < 0.5f )
  100. {
  101. p.z = 0.5f;
  102. }
  103. // Set new coords
  104. p.x += getGridRect().origin.x;
  105. setVertex(Vec2(i, j), p);
  106. }
  107. }
  108. }
  109. NS_CC_END