CCActionPageTurn3D.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /****************************************************************************
  2. Copyright (c) 2009 Sindesso Pty Ltd http://www.sindesso.com/
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2017 Chukong Technologies Inc.
  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 "2d/CCActionPageTurn3D.h"
  23. #include "2d/CCGrid.h"
  24. #include "2d/CCNodeGrid.h"
  25. NS_CC_BEGIN
  26. PageTurn3D* PageTurn3D::create(float duration, const Size& gridSize)
  27. {
  28. PageTurn3D *action = new (std::nothrow) PageTurn3D();
  29. if (action && action->initWithDuration(duration, gridSize))
  30. {
  31. action->autorelease();
  32. return action;
  33. }
  34. delete action;
  35. return nullptr;
  36. }
  37. PageTurn3D *PageTurn3D::clone() const
  38. {
  39. // no copy constructor
  40. return PageTurn3D::create(_duration, _gridSize);
  41. }
  42. GridBase* PageTurn3D::getGrid()
  43. {
  44. auto result = Grid3D::create(_gridSize, _gridNodeTarget->getGridRect());
  45. if (result)
  46. {
  47. result->setNeedDepthTestForBlit(true);
  48. }
  49. return result;
  50. }
  51. /*
  52. * Update each tick
  53. * Time is the percentage of the way through the duration
  54. */
  55. void PageTurn3D::update(float time)
  56. {
  57. float tt = MAX(0, time - 0.25f);
  58. float deltaAy = (tt * tt * 500);
  59. float ay = -100 - deltaAy;
  60. float deltaTheta = sqrtf(time);
  61. float theta = deltaTheta > 0.5f ? (float)M_PI_2*deltaTheta : (float)M_PI_2*(1-deltaTheta);
  62. float rotateByYAxis = (2-time)* M_PI;
  63. float sinTheta = sinf(theta);
  64. float cosTheta = cosf(theta);
  65. for (int i = 0; i <= _gridSize.width; ++i)
  66. {
  67. for (int j = 0; j <= _gridSize.height; ++j)
  68. {
  69. // Get original vertex
  70. Vec3 p = getOriginalVertex(Vec2(i ,j));
  71. p.x -= getGridRect().origin.x;
  72. float R = sqrtf((p.x * p.x) + ((p.y - ay) * (p.y - ay)));
  73. float r = R * sinTheta;
  74. float alpha = asinf( p.x / R );
  75. float beta = alpha / sinTheta;
  76. float cosBeta = cosf( beta );
  77. // If beta > PI then we've wrapped around the cone
  78. // Reduce the radius to stop these points interfering with others
  79. if (beta <= M_PI)
  80. {
  81. p.x = ( r * sinf(beta));
  82. }
  83. else
  84. {
  85. // Force X = 0 to stop wrapped
  86. // points
  87. p.x = 0;
  88. }
  89. p.y = ( R + ay - ( r * (1 - cosBeta) * sinTheta));
  90. // We scale z here to avoid the animation being
  91. // too much bigger than the screen due to perspective transform
  92. p.z = (r * ( 1 - cosBeta ) * cosTheta);// "100" didn't work for
  93. p.x = p.z * sinf(rotateByYAxis) + p.x * cosf(rotateByYAxis);
  94. p.z = p.z * cosf(rotateByYAxis) - p.x * sinf(rotateByYAxis);
  95. p.z/=7;
  96. // Stop z coord from dropping beneath underlying page in a transition
  97. // issue #751
  98. if( p.z < 0.5f )
  99. {
  100. p.z = 0.5f;
  101. }
  102. // Set new coords
  103. p.x += getGridRect().origin.x;
  104. setVertex(Vec2(i, j), p);
  105. }
  106. }
  107. }
  108. NS_CC_END