axmol/cocos/navmesh/CCNavMeshAgent.cpp

428 lines
11 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
http://www.cocos2d-x.org
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +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
2019-11-23 20:27:39 +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
2019-11-23 20:27:39 +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.
****************************************************************************/
#include "navmesh/CCNavMeshAgent.h"
#if CC_USE_NAVMESH
2021-12-25 10:04:45 +08:00
# include "navmesh/CCNavMesh.h"
# include "recast/DetourCrowd.h"
# include "2d/CCNode.h"
# include "2d/CCScene.h"
# include <algorithm>
2019-11-23 20:27:39 +08:00
NS_CC_BEGIN
NavMeshAgentParam::NavMeshAgentParam()
2021-12-25 10:04:45 +08:00
: radius(0.6f)
, height(2.0f)
, maxAcceleration(8.0f)
, maxSpeed(3.5f)
, collisionQueryRange(radius * 12.0f)
, pathOptimizationRange(radius * 30.0f)
, separationWeight(2.0f)
, updateFlags(DT_CROWD_ANTICIPATE_TURNS | DT_CROWD_OPTIMIZE_VIS | DT_CROWD_OPTIMIZE_TOPO |
DT_CROWD_OBSTACLE_AVOIDANCE)
, obstacleAvoidanceType(3)
, queryFilterType(0)
{}
NavMeshAgent* NavMeshAgent::create(const NavMeshAgentParam& param)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
auto ref = new NavMeshAgent();
if (ref->initWith(param))
2019-11-23 20:27:39 +08:00
{
ref->autorelease();
return ref;
}
CC_SAFE_DELETE(ref);
return nullptr;
}
2021-12-26 23:26:34 +08:00
std::string_view NavMeshAgent::getNavMeshAgentComponentName()
2019-11-23 20:27:39 +08:00
{
static std::string comName = "___NavMeshAgentComponent___";
return comName;
}
cocos2d::NavMeshAgent::NavMeshAgent()
: _syncFlag(NODE_AND_NODE)
, _rotRefAxes(Vec3::UNIT_Z)
, _state(DT_CROWDAGENT_STATE_WALKING)
, _needAutoOrientation(true)
, _agentID(-1)
, _needUpdateAgent(true)
, _needMove(false)
, _totalTimeAfterMove(0.0f)
, _userData(nullptr)
, _crowd(nullptr)
, _navMeshQuery(nullptr)
2021-12-25 10:04:45 +08:00
{}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
cocos2d::NavMeshAgent::~NavMeshAgent() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
bool NavMeshAgent::initWith(const NavMeshAgentParam& param)
2019-11-23 20:27:39 +08:00
{
_param = param;
setName(getNavMeshAgentComponentName());
return true;
}
2021-12-25 10:04:45 +08:00
void cocos2d::NavMeshAgent::setNavMeshQuery(dtNavMeshQuery* query)
2019-11-23 20:27:39 +08:00
{
_navMeshQuery = query;
}
2021-12-25 10:04:45 +08:00
void cocos2d::NavMeshAgent::removeFrom(dtCrowd* crowed)
2019-11-23 20:27:39 +08:00
{
crowed->removeAgent(_agentID);
2021-12-25 10:04:45 +08:00
_crowd = nullptr;
2019-11-23 20:27:39 +08:00
_agentID = -1;
}
2021-12-25 10:04:45 +08:00
void cocos2d::NavMeshAgent::addTo(dtCrowd* crowed)
2019-11-23 20:27:39 +08:00
{
_crowd = crowed;
dtCrowdAgentParams ap;
convertTodtAgentParam(_param, ap);
Mat4 mat = _owner->getNodeToWorldTransform();
_agentID = _crowd->addAgent(&mat.m[12], &ap);
}
2021-12-25 10:04:45 +08:00
void cocos2d::NavMeshAgent::convertTodtAgentParam(const NavMeshAgentParam& inParam, dtCrowdAgentParams& outParam)
2019-11-23 20:27:39 +08:00
{
memset(&outParam, 0, sizeof(outParam));
2021-12-25 10:04:45 +08:00
outParam.collisionQueryRange = inParam.collisionQueryRange;
outParam.height = inParam.height;
outParam.maxAcceleration = inParam.maxAcceleration;
outParam.maxSpeed = inParam.maxSpeed;
2019-11-23 20:27:39 +08:00
outParam.obstacleAvoidanceType = inParam.obstacleAvoidanceType;
outParam.pathOptimizationRange = inParam.pathOptimizationRange;
2021-12-25 10:04:45 +08:00
outParam.queryFilterType = inParam.queryFilterType;
outParam.radius = inParam.radius;
outParam.separationWeight = inParam.separationWeight;
outParam.updateFlags = inParam.updateFlags;
2019-11-23 20:27:39 +08:00
}
void cocos2d::NavMeshAgent::onExit()
{
2021-12-25 10:04:45 +08:00
if (_agentID == -1)
return;
2019-11-23 20:27:39 +08:00
Component::onExit();
auto scene = _owner->getScene();
2021-12-25 10:04:45 +08:00
if (scene && scene->getNavMesh())
{
2019-11-23 20:27:39 +08:00
scene->getNavMesh()->removeNavMeshAgent(this);
}
}
void cocos2d::NavMeshAgent::onEnter()
{
2021-12-25 10:04:45 +08:00
if (_agentID != -1)
return;
2019-11-23 20:27:39 +08:00
Component::onEnter();
auto scene = _owner->getScene();
2021-12-25 10:04:45 +08:00
if (scene && scene->getNavMesh())
{
2019-11-23 20:27:39 +08:00
scene->getNavMesh()->addNavMeshAgent(this);
}
}
float NavMeshAgent::getMaxSpeed() const
{
return _param.maxSpeed;
}
void NavMeshAgent::setSeparationWeight(float weight)
{
_param.separationWeight = weight;
2021-12-25 10:04:45 +08:00
_needUpdateAgent = true;
2019-11-23 20:27:39 +08:00
}
float NavMeshAgent::getSeparationWeight() const
{
return _param.separationWeight;
}
void cocos2d::NavMeshAgent::setObstacleAvoidanceType(unsigned char type)
{
_param.obstacleAvoidanceType = type;
2021-12-25 10:04:45 +08:00
_needUpdateAgent = true;
2019-11-23 20:27:39 +08:00
}
unsigned char NavMeshAgent::getObstacleAvoidanceType() const
{
return _param.obstacleAvoidanceType;
}
Vec3 NavMeshAgent::getCurrentVelocity() const
{
2021-12-25 10:04:45 +08:00
if (_crowd)
{
2019-11-23 20:27:39 +08:00
auto agent = _crowd->getAgent(_agentID);
2021-12-25 10:04:45 +08:00
if (agent)
{
2019-11-23 20:27:39 +08:00
return Vec3(agent->vel[0], agent->vel[1], agent->vel[2]);
}
}
return Vec3::ZERO;
}
void NavMeshAgent::setMaxSpeed(float maxSpeed)
{
2021-12-25 10:04:45 +08:00
_param.maxSpeed = maxSpeed;
2019-11-23 20:27:39 +08:00
_needUpdateAgent = true;
}
float NavMeshAgent::getMaxAcceleration() const
{
return _param.maxAcceleration;
}
void NavMeshAgent::setMaxAcceleration(float maxAcceleration)
{
_param.maxAcceleration = maxAcceleration;
2021-12-25 10:04:45 +08:00
_needUpdateAgent = true;
2019-11-23 20:27:39 +08:00
}
float NavMeshAgent::getHeight() const
{
return _param.height;
}
void NavMeshAgent::setHeight(float height)
{
2021-12-25 10:04:45 +08:00
_param.height = height;
2019-11-23 20:27:39 +08:00
_needUpdateAgent = true;
}
float NavMeshAgent::getRadius() const
{
return _param.radius;
}
void NavMeshAgent::setRadius(float radius)
{
2021-12-25 10:04:45 +08:00
_param.radius = radius;
2019-11-23 20:27:39 +08:00
_needUpdateAgent = true;
}
2021-12-25 10:04:45 +08:00
void NavMeshAgent::move(const Vec3& destination, const MoveCallback& callback)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_destination = destination;
_moveCallback = callback;
_needMove = true;
2019-11-23 20:27:39 +08:00
_needUpdateAgent = true;
}
OffMeshLinkData NavMeshAgent::getCurrentOffMeshLinkData()
{
OffMeshLinkData data;
2021-12-25 10:04:45 +08:00
if (_crowd && isOnOffMeshLink())
{
2019-11-23 20:27:39 +08:00
auto agentAnim = _crowd->getEditableAgentAnim(_agentID);
2021-12-25 10:04:45 +08:00
if (agentAnim)
{
2019-11-23 20:27:39 +08:00
Mat4 mat;
if (_owner && _owner->getParent())
mat = _owner->getParent()->getWorldToNodeTransform();
mat.transformPoint(agentAnim->startPos, &data.startPosition);
mat.transformPoint(agentAnim->endPos, &data.endPosition);
}
}
return data;
}
bool NavMeshAgent::isOnOffMeshLink()
{
return _state == DT_CROWDAGENT_STATE_OFFMESH;
}
void cocos2d::NavMeshAgent::completeOffMeshLink()
{
2021-12-25 10:04:45 +08:00
if (_crowd && isOnOffMeshLink())
{
_state = DT_CROWDAGENT_STATE_WALKING;
2019-11-23 20:27:39 +08:00
_needUpdateAgent = true;
}
}
void NavMeshAgent::setAutoTraverseOffMeshLink(bool isAuto)
{
2021-12-25 10:04:45 +08:00
if (_crowd && isOnOffMeshLink())
{
2019-11-23 20:27:39 +08:00
auto agentAnim = _crowd->getEditableAgentAnim(_agentID);
2021-12-25 10:04:45 +08:00
if (agentAnim)
{
2019-11-23 20:27:39 +08:00
agentAnim->active = isAuto;
}
}
}
void NavMeshAgent::stop()
{
2021-12-25 10:04:45 +08:00
if (_state != DT_CROWDAGENT_STATE_INVALID)
return;
_state = DT_CROWDAGENT_STATE_INVALID;
2019-11-23 20:27:39 +08:00
_needUpdateAgent = true;
}
2021-12-25 10:04:45 +08:00
void NavMeshAgent::setOrientationRefAxes(const Vec3& rotRefAxes)
2019-11-23 20:27:39 +08:00
{
_rotRefAxes = rotRefAxes;
}
void cocos2d::NavMeshAgent::setAutoOrientation(bool isAuto)
{
_needAutoOrientation = isAuto;
}
void NavMeshAgent::resume()
{
2021-12-25 10:04:45 +08:00
if (_state != DT_CROWDAGENT_STATE_INVALID)
return;
_state = DT_CROWDAGENT_STATE_WALKING;
2019-11-23 20:27:39 +08:00
_needUpdateAgent = true;
}
void NavMeshAgent::pause()
{
2021-12-25 10:04:45 +08:00
if (_state == DT_CROWDAGENT_STATE_INVALID)
return;
_state = DT_CROWDAGENT_STATE_INVALID;
2019-11-23 20:27:39 +08:00
_needUpdateAgent = true;
}
void NavMeshAgent::preUpdate(float delta)
{
if (_state != DT_CROWDAGENT_STATE_INVALID)
_totalTimeAfterMove += delta;
if (_moveCallback && _state != DT_CROWDAGENT_STATE_INVALID)
_moveCallback(this, _totalTimeAfterMove);
if ((_syncFlag & NODE_TO_AGENT) != 0)
syncToAgent();
2021-12-25 10:04:45 +08:00
if (_needMove && _crowd && _navMeshQuery)
{
if (_state == DT_CROWDAGENT_STATE_OFFMESH)
return;
_state = DT_CROWDAGENT_STATE_WALKING;
2019-11-23 20:27:39 +08:00
_totalTimeAfterMove = 0.0f;
2021-12-25 10:04:45 +08:00
dtPolyRef pRef = 0;
2019-11-23 20:27:39 +08:00
float nearestPos[3];
2021-12-25 10:04:45 +08:00
_navMeshQuery->findNearestPoly(&_destination.x, _crowd->getQueryExtents(), _crowd->getFilter(0), &pRef,
nearestPos);
2019-11-23 20:27:39 +08:00
_crowd->requestMoveTarget(_agentID, pRef, nearestPos);
_needMove = false;
}
}
void NavMeshAgent::postUpdate(float /*delta*/)
{
if ((_syncFlag & AGENT_TO_NODE) != 0)
syncToNode();
}
void NavMeshAgent::syncToNode()
{
2021-12-25 10:04:45 +08:00
const dtCrowdAgent* agent = nullptr;
if (_crowd)
{
2019-11-23 20:27:39 +08:00
agent = _crowd->getAgent(_agentID);
}
2021-12-25 10:04:45 +08:00
if (agent)
{
2019-11-23 20:27:39 +08:00
Mat4 wtop;
Vec3 pos;
if (_owner->getParent())
wtop = _owner->getParent()->getWorldToNodeTransform();
wtop.transformPoint(Vec3(agent->npos[0], agent->npos[1], agent->npos[2]), &pos);
_owner->setPosition3D(pos);
_state = agent->state;
2021-12-25 10:04:45 +08:00
if (_needAutoOrientation)
{
2019-11-23 20:27:39 +08:00
if (std::abs(agent->vel[0]) > 0.3f || std::abs(agent->vel[1]) > 0.3f || std::abs(agent->vel[2]) > 0.3f)
{
Vec3 axes(_rotRefAxes);
axes.normalize();
Vec3 dir;
wtop.transformVector(Vec3(agent->vel[0], agent->vel[1], agent->vel[2]), &dir);
dir.normalize();
float cosTheta = Vec3::dot(axes, dir);
Vec3 rotAxes;
Vec3::cross(axes, dir, &rotAxes);
Quaternion rot = Quaternion(rotAxes, acosf(cosTheta));
_owner->setRotationQuat(rot);
}
}
}
}
void NavMeshAgent::syncToAgent()
{
2021-12-25 10:04:45 +08:00
if (_crowd)
{
auto agent = _crowd->getEditableAgent(_agentID);
Mat4 mat = _owner->getNodeToWorldTransform();
2019-11-23 20:27:39 +08:00
agent->npos[0] = mat.m[12];
agent->npos[1] = mat.m[13];
agent->npos[2] = mat.m[14];
2021-12-25 10:04:45 +08:00
// if (_needAutoOrientation){
2019-11-23 20:27:39 +08:00
// Vec3 vel = mat * _rotRefAxes;
// agent->vel[0] = vel.x;
// agent->vel[1] = vel.y;
// agent->vel[2] = vel.z;
2021-12-25 10:04:45 +08:00
// }
if (_needUpdateAgent)
{
2019-11-23 20:27:39 +08:00
dtCrowdAgentParams ap;
convertTodtAgentParam(_param, ap);
2021-12-25 10:04:45 +08:00
agent->params = ap;
agent->state = _state;
2019-11-23 20:27:39 +08:00
_needUpdateAgent = false;
}
}
}
Vec3 NavMeshAgent::getVelocity() const
{
2021-12-25 10:04:45 +08:00
const dtCrowdAgent* agent = nullptr;
if (_crowd)
{
2019-11-23 20:27:39 +08:00
agent = _crowd->getAgent(_agentID);
}
if (agent)
{
return Vec3(agent->vel[0], agent->vel[1], agent->vel[2]);
}
return Vec3::ZERO;
}
NS_CC_END
2021-12-25 10:04:45 +08:00
#endif // CC_USE_NAVMESH