axmol/extensions/Particle3D/PU/CCPUBoxCollider.cpp

284 lines
9.8 KiB
C++
Raw Normal View History

2015-02-11 18:14:22 +08:00
/****************************************************************************
2015-03-02 16:05:26 +08:00
Copyright (C) 2013 Henry van Merode. All rights reserved.
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
http://www.cocos2d-x.org
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2015-03-05 13:24:06 +08:00
#include "CCPUBoxCollider.h"
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
2015-02-11 18:14:22 +08:00
NS_CC_BEGIN
// Constants
2021-12-25 10:04:45 +08:00
const float PUBoxCollider::DEFAULT_WIDTH = 100.0f;
2015-03-05 13:24:06 +08:00
const float PUBoxCollider::DEFAULT_HEIGHT = 100.0f;
2021-12-25 10:04:45 +08:00
const float PUBoxCollider::DEFAULT_DEPTH = 100.0f;
2015-02-11 18:14:22 +08:00
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUBoxCollider::PUBoxCollider()
: PUBaseCollider()
, _width(DEFAULT_WIDTH)
, _height(DEFAULT_HEIGHT)
, _depth(DEFAULT_DEPTH)
, _xmin(0.0f)
, _xmax(0.0f)
, _ymin(0.0f)
, _ymax(0.0f)
, _zmin(0.0f)
, _zmax(0.0f)
, _innerCollision(false)
{}
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
PUBoxCollider::~PUBoxCollider() {}
2015-02-11 18:14:22 +08:00
//-----------------------------------------------------------------------
float PUBoxCollider::getWidth() const
2015-02-11 18:14:22 +08:00
{
return _width;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PUBoxCollider::setWidth(const float width)
2015-02-11 18:14:22 +08:00
{
_width = width;
}
//-----------------------------------------------------------------------
float PUBoxCollider::getHeight() const
2015-02-11 18:14:22 +08:00
{
return _height;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PUBoxCollider::setHeight(const float height)
2015-02-11 18:14:22 +08:00
{
_height = height;
}
//-----------------------------------------------------------------------
float PUBoxCollider::getDepth() const
2015-02-11 18:14:22 +08:00
{
return _depth;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PUBoxCollider::setDepth(const float depth)
2015-02-11 18:14:22 +08:00
{
_depth = depth;
}
//-----------------------------------------------------------------------
bool PUBoxCollider::isInnerCollision() const
2015-02-11 18:14:22 +08:00
{
return _innerCollision;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PUBoxCollider::setInnerCollision(bool innerCollision)
2015-02-11 18:14:22 +08:00
{
_innerCollision = innerCollision;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PUBoxCollider::calculateDirectionAfterCollision(PUParticle3D* particle)
2015-02-11 18:14:22 +08:00
{
switch (_collisionType)
{
2021-12-25 10:04:45 +08:00
case PUBaseCollider::CT_BOUNCE:
{
// Determine the nearest side and reverse the direction
if (isSmallestValue(particle->position.x - _xmin, particle->position))
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
particle->direction.x *= -1;
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
else if (isSmallestValue(_xmax - particle->position.x, particle->position))
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
particle->direction.x *= -1;
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
else if (isSmallestValue(particle->position.y - _ymin, particle->position))
{
particle->direction.y *= -1;
}
else if (isSmallestValue(_ymax - particle->position.y, particle->position))
{
particle->direction.y *= -1;
}
else if (isSmallestValue(particle->position.z - _zmin, particle->position))
{
particle->direction.z *= -1;
}
else if (isSmallestValue(_zmax - particle->position.z, particle->position))
{
particle->direction.z *= -1;
}
particle->direction *= _bouncyness;
}
break;
case PUBaseCollider::CT_FLOW:
{
if (isSmallestValue(particle->position.x - _xmin, particle->position))
{
particle->direction.x = 0;
}
else if (isSmallestValue(_xmax - particle->position.x, particle->position))
{
particle->direction.x = 0;
}
else if (isSmallestValue(particle->position.y - _ymin, particle->position))
{
particle->direction.y = 0;
}
else if (isSmallestValue(_ymax - particle->position.y, particle->position))
{
particle->direction.y = 0;
}
else if (isSmallestValue(particle->position.z - _zmin, particle->position))
{
particle->direction.z = 0;
}
else if (isSmallestValue(_zmax - particle->position.z, particle->position))
{
particle->direction.z = 0;
}
particle->direction *= -_friction;
}
break;
default:
2015-02-11 18:14:22 +08:00
break;
}
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUBoxCollider::calculateBounds()
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
float scaledWidth = _affectorScale.x * _width;
2015-02-11 18:14:22 +08:00
float scaledHeight = _affectorScale.y * _height;
2021-12-25 10:04:45 +08:00
float scaledDepth = _affectorScale.z * _depth;
2015-02-11 18:14:22 +08:00
_xmin = _derivedPosition.x - 0.5f * scaledWidth;
_xmax = _derivedPosition.x + 0.5f * scaledWidth;
_ymin = _derivedPosition.y - 0.5f * scaledHeight;
_ymax = _derivedPosition.y + 0.5f * scaledHeight;
_zmin = _derivedPosition.z - 0.5f * scaledDepth;
_zmax = _derivedPosition.z + 0.5f * scaledDepth;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
bool PUBoxCollider::isSmallestValue(float value, const Vec3& particlePosition)
2015-02-11 18:14:22 +08:00
{
float value1 = particlePosition.x - _xmin;
float value2 = _xmax - particlePosition.x;
float value3 = particlePosition.y - _ymin;
float value4 = _ymax - particlePosition.y;
float value5 = particlePosition.z - _zmin;
float value6 = _zmax - particlePosition.z;
2021-12-25 10:04:45 +08:00
return (value <= value1 && value <= value2 && value <= value3 && value <= value4 && value <= value5 &&
value <= value6);
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
void PUBoxCollider::updatePUAffector(PUParticle3D* particle, float /*deltaTime*/)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
// for (auto iter : _particleSystem->getParticles())
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
// PUParticle3D *particle = iter;
2015-02-11 18:14:22 +08:00
_predictedPosition = particle->position + _velocityScale * particle->direction;
2021-12-25 10:04:45 +08:00
bool collision = false;
2015-02-11 18:14:22 +08:00
/** Collision detection is a two-step. First, we determine whether the particle is now colliding.
If it is, the particle is re-positioned. However, a timeElapsed value is used, which is not the same
2021-12-25 10:04:45 +08:00
as the one that was used at the moment before the particle was colliding. Therefore, we rather
2015-02-11 18:14:22 +08:00
want to predict particle collision in front. This probably isn't the fastest solution.
The same approach was used for the other colliders.
*/
2021-12-25 10:04:45 +08:00
switch (_intersectionType)
{
case PUBaseCollider::IT_POINT:
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
// Validate for a point-box intersection
if (_innerCollision != _box.containPoint(particle->position))
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
// Collision detected (re-position the particle)
particle->position -= _velocityScale * particle->direction;
collision = true;
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
else if (_innerCollision != _box.containPoint(_predictedPosition))
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
// Collision detected
collision = true;
}
}
break;
case PUBaseCollider::IT_BOX:
{
AABB box;
populateAlignedBox(box, particle->position, particle->width, particle->height, particle->depth);
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
if (_innerCollision != box.intersects(_box))
{
// Collision detected (re-position the particle)
particle->position -= _velocityScale * particle->direction;
collision = true;
}
else
{
populateAlignedBox(box, _predictedPosition, particle->width, particle->height, particle->depth);
2015-02-11 18:14:22 +08:00
if (_innerCollision != box.intersects(_box))
{
2021-12-25 10:04:45 +08:00
// Collision detected
2015-02-11 18:14:22 +08:00
collision = true;
}
}
2021-12-25 10:04:45 +08:00
}
break;
2015-02-11 18:14:22 +08:00
}
if (collision)
{
calculateDirectionAfterCollision(particle);
calculateRotationSpeedAfterCollision(particle);
particle->addEventFlags(PUParticle3D::PEF_COLLIDED);
}
}
}
2021-12-25 10:04:45 +08:00
void PUBoxCollider::preUpdateAffector(float deltaTime)
2015-02-11 18:14:22 +08:00
{
2015-03-05 13:24:06 +08:00
PUBaseCollider::preUpdateAffector(deltaTime);
2015-02-11 18:14:22 +08:00
// Calculate the affectors' center position in worldspace, set the box and calculate the bounds
// Applied scaling in V 1.3.1.
2021-12-25 10:04:45 +08:00
populateAlignedBox(_box, getDerivedPosition(), _affectorScale.x * _width, _affectorScale.y * _height,
_affectorScale.z * _depth);
2015-02-11 18:14:22 +08:00
calculateBounds();
}
2015-03-05 13:24:06 +08:00
PUBoxCollider* PUBoxCollider::create()
2015-02-11 18:14:22 +08:00
{
2021-12-08 00:11:53 +08:00
auto pbc = new PUBoxCollider();
2015-02-11 18:14:22 +08:00
pbc->autorelease();
return pbc;
}
2021-12-25 10:04:45 +08:00
void PUBoxCollider::copyAttributesTo(PUAffector* affector)
2015-03-02 13:07:32 +08:00
{
2015-03-05 13:24:06 +08:00
PUBaseCollider::copyAttributesTo(affector);
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
PUBoxCollider* boxCollider = static_cast<PUBoxCollider*>(affector);
boxCollider->_width = _width;
boxCollider->_height = _height;
boxCollider->_depth = _depth;
2015-03-02 13:07:32 +08:00
boxCollider->_innerCollision = _innerCollision;
}
2015-03-03 15:13:47 +08:00
NS_CC_END