CCPUBaseCollider.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "CCPUBaseCollider.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. NS_CC_BEGIN
  25. // Constants
  26. const float PUBaseCollider::DEFAULT_BOUNCYNESS = 1.0f;
  27. const float PUBaseCollider::DEFAULT_FRICTION = 0.0f;
  28. const PUBaseCollider::IntersectionType PUBaseCollider::DEFAULT_INTERSECTION_TYPE = PUBaseCollider::IT_POINT;
  29. const PUBaseCollider::CollisionType PUBaseCollider::DEFAULT_COLLISION_TYPE = PUBaseCollider::CT_BOUNCE;
  30. //-----------------------------------------------------------------------
  31. PUBaseCollider::PUBaseCollider()
  32. : PUAffector()
  33. , _friction(DEFAULT_FRICTION)
  34. , _bouncyness(DEFAULT_BOUNCYNESS)
  35. , _intersectionType(DEFAULT_INTERSECTION_TYPE)
  36. , _collisionType(DEFAULT_COLLISION_TYPE)
  37. , _velocityScale(1.0f)
  38. {
  39. }
  40. PUBaseCollider::~PUBaseCollider()
  41. {
  42. }
  43. PUBaseCollider::IntersectionType PUBaseCollider::getIntersectionType() const
  44. {
  45. return _intersectionType;
  46. }
  47. void PUBaseCollider::setIntersectionType( const IntersectionType& intersectionType )
  48. {
  49. _intersectionType = intersectionType;
  50. }
  51. PUBaseCollider::CollisionType PUBaseCollider::getCollisionType() const
  52. {
  53. return _collisionType;
  54. }
  55. void PUBaseCollider::setCollisionType( const CollisionType& collisionType )
  56. {
  57. _collisionType = collisionType;
  58. }
  59. float PUBaseCollider::getFriction() const
  60. {
  61. return _friction;
  62. }
  63. void PUBaseCollider::setFriction( const float friction )
  64. {
  65. _friction = friction;
  66. }
  67. float PUBaseCollider::getBouncyness() const
  68. {
  69. return _bouncyness;
  70. }
  71. void PUBaseCollider::setBouncyness( const float bouncyness )
  72. {
  73. _bouncyness = bouncyness;
  74. }
  75. void PUBaseCollider::populateAlignedBox( AABB& box, const Vec3& position, const float width, const float height, const float depth )
  76. {
  77. float halfWidth = 0.5f * width;
  78. float halfHeight = 0.5f * height;
  79. float halfDepth = 0.5f * depth;
  80. box.set(Vec3(position.x - halfWidth,
  81. position.y - halfHeight,
  82. position.z - halfDepth),
  83. Vec3(position.x + halfWidth,
  84. position.y + halfHeight,
  85. position.z + halfDepth));
  86. }
  87. void PUBaseCollider::calculateRotationSpeedAfterCollision( PUParticle3D* particle )
  88. {
  89. if (particle->particleType != PUParticle3D::PT_VISUAL)
  90. return;
  91. float signedFriction = CCRANDOM_0_1() > 0.5f ? -(_friction - 1) : (_friction - 1);
  92. particle->rotationSpeed *= signedFriction;
  93. particle->zRotationSpeed *= signedFriction;
  94. }
  95. void PUBaseCollider::preUpdateAffector( float deltaTime )
  96. {
  97. // Take scaled velocity into account
  98. _velocityScale = deltaTime * (static_cast<PUParticleSystem3D *>(_particleSystem))->getParticleSystemScaleVelocity();
  99. }
  100. void PUBaseCollider::copyAttributesTo( PUAffector* affector )
  101. {
  102. PUAffector::copyAttributesTo(affector);
  103. PUBaseCollider* baseCollider = static_cast<PUBaseCollider*>(affector);
  104. baseCollider->_bouncyness = _bouncyness;
  105. baseCollider->_friction = _friction;
  106. baseCollider->_intersectionType = _intersectionType;
  107. baseCollider->_collisionType = _collisionType;
  108. }
  109. NS_CC_END