2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2015-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2022-07-10 09:47:41 +08:00
|
|
|
https://axis-project.github.io/
|
2021-12-31 12:12:40 +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-31 12:12:40 +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-31 12:12:40 +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 "NavMeshTest.h"
|
|
|
|
#include "physics3d/CCPhysics3DWorld.h"
|
|
|
|
#include "physics3d/CCPhysics3D.h"
|
|
|
|
#include "3d/CCBundle3D.h"
|
|
|
|
#include "2d/CCLight.h"
|
|
|
|
|
|
|
|
USING_NS_CC_EXT;
|
|
|
|
USING_NS_CC;
|
|
|
|
|
|
|
|
struct AgentUserData
|
|
|
|
{
|
|
|
|
float time;
|
|
|
|
};
|
|
|
|
|
|
|
|
NavMeshTests::NavMeshTests()
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
#if (CC_USE_NAVMESH == 0) || (CC_USE_PHYSICS == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
ADD_TEST_CASE(NavMeshDisabled);
|
|
|
|
#else
|
|
|
|
ADD_TEST_CASE(NavMeshBasicTestDemo);
|
|
|
|
ADD_TEST_CASE(NavMeshAdvanceTestDemo);
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
#if (CC_USE_NAVMESH == 0) || (CC_USE_PHYSICS == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
void NavMeshDisabled::onEnter()
|
|
|
|
{
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 16);
|
2021-12-31 12:12:40 +08:00
|
|
|
auto label =
|
|
|
|
Label::createWithTTF(ttfConfig, "Should define CC_USE_NAVMESH & CC_USE_PHYSICS\n to run this test case");
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
label->setPosition(Vec2(size.width / 2, size.height / 2));
|
|
|
|
|
|
|
|
addChild(label);
|
|
|
|
|
|
|
|
TestCase::onEnter();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
NavMeshBaseTestDemo::NavMeshBaseTestDemo() : _camera(nullptr), _needMoveAgents(false) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
NavMeshBaseTestDemo::~NavMeshBaseTestDemo()
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
for (auto iter : _agents)
|
|
|
|
{
|
|
|
|
AgentUserData* data = static_cast<AgentUserData*>(iter.first->getUserData());
|
2019-11-23 20:27:39 +08:00
|
|
|
delete data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool NavMeshBaseTestDemo::init()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!TestCase::init())
|
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
if (initPhysicsWorld())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
_angle = 0.0f;
|
|
|
|
|
|
|
|
Size size = Director::getInstance()->getWinSize();
|
|
|
|
_camera = Camera::createPerspective(30.0f, size.width / size.height, 1.0f, 1000.0f);
|
|
|
|
_camera->setPosition3D(Vec3(0.0f, 50.0f, 100.0f));
|
|
|
|
_camera->lookAt(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
_camera->setCameraFlag(CameraFlag::USER1);
|
|
|
|
this->addChild(_camera);
|
|
|
|
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
listener->onTouchesBegan = CC_CALLBACK_2(NavMeshBaseTestDemo::onTouchesBegan, this);
|
|
|
|
listener->onTouchesMoved = CC_CALLBACK_2(NavMeshBaseTestDemo::onTouchesMoved, this);
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(NavMeshBaseTestDemo::onTouchesEnded, this);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
|
|
|
|
initScene();
|
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void NavMeshBaseTestDemo::onTouchesBegan(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_needMoveAgents = true;
|
|
|
|
touchesBegan(touches, event);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void NavMeshBaseTestDemo::onTouchesMoved(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (touches.size() && _camera)
|
|
|
|
{
|
|
|
|
auto touch = touches[0];
|
|
|
|
auto delta = touch->getDelta();
|
|
|
|
|
|
|
|
_angle -= CC_DEGREES_TO_RADIANS(delta.x);
|
|
|
|
_camera->setPosition3D(Vec3(100.0f * sinf(_angle), 50.0f, 100.0f * cosf(_angle)));
|
|
|
|
_camera->lookAt(Vec3(0.0f, 0.0f, 0.0f), Vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
|
|
|
|
if (delta.lengthSquared() > 16)
|
|
|
|
{
|
|
|
|
_needMoveAgents = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
touchesMoved(touches, event);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void NavMeshBaseTestDemo::onTouchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
touchesEnded(touches, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavMeshBaseTestDemo::initScene()
|
|
|
|
{
|
|
|
|
getPhysics3DWorld()->setDebugDrawEnable(false);
|
2021-12-31 12:12:40 +08:00
|
|
|
// create mesh
|
2019-11-23 20:27:39 +08:00
|
|
|
std::vector<Vec3> trianglesList = Bundle3D::getTrianglesList("NavMesh/scene.obj");
|
|
|
|
|
|
|
|
Physics3DRigidBodyDes rbDes;
|
|
|
|
rbDes.mass = 0.0f;
|
|
|
|
rbDes.shape = Physics3DShape::createMesh(&trianglesList[0], (int)trianglesList.size() / 3);
|
|
|
|
auto rigidBody = Physics3DRigidBody::create(&rbDes);
|
|
|
|
auto component = Physics3DComponent::create(rigidBody);
|
2022-07-05 14:48:46 +08:00
|
|
|
auto mesh = MeshRenderer::create("NavMesh/scene.obj");
|
|
|
|
mesh->addComponent(component);
|
|
|
|
mesh->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
this->addChild(mesh);
|
2019-11-23 20:27:39 +08:00
|
|
|
setPhysics3DDebugCamera(_camera);
|
|
|
|
|
|
|
|
auto navMesh = NavMesh::create("NavMesh/all_tiles_tilecache.bin", "NavMesh/geomset.txt");
|
|
|
|
navMesh->setDebugDrawEnable(true);
|
|
|
|
setNavMesh(navMesh);
|
|
|
|
setNavMeshDebugCamera(_camera);
|
|
|
|
|
|
|
|
auto ambientLight = AmbientLight::create(Color3B(64, 64, 64));
|
|
|
|
ambientLight->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
this->addChild(ambientLight);
|
|
|
|
|
|
|
|
auto dirLight = DirectionLight::create(Vec3(1.2f, -1.1f, 0.5f), Color3B(255, 255, 255));
|
|
|
|
dirLight->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
this->addChild(dirLight);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void NavMeshBaseTestDemo::createAgent(const Vec3& pos)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-05 14:48:46 +08:00
|
|
|
std::string filePath = "MeshRendererTest/girl.c3b";
|
2019-11-23 20:27:39 +08:00
|
|
|
NavMeshAgentParam param;
|
|
|
|
param.radius = 2.0f;
|
|
|
|
param.height = 8.0f;
|
|
|
|
param.maxSpeed = 8.0f;
|
|
|
|
auto agent = NavMeshAgent::create(param);
|
2022-07-05 14:48:46 +08:00
|
|
|
auto agentNode = MeshRenderer::create(filePath);
|
2019-11-23 20:27:39 +08:00
|
|
|
agent->setOrientationRefAxes(Vec3(-1.0f, 0.0f, 1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
AgentUserData* data = new AgentUserData{0.0f};
|
2019-11-23 20:27:39 +08:00
|
|
|
agent->setUserData(data);
|
|
|
|
agentNode->setScale(0.05f);
|
|
|
|
agentNode->addComponent(agent);
|
|
|
|
|
|
|
|
auto node = Node::create();
|
|
|
|
node->addChild(agentNode);
|
|
|
|
node->setPosition3D(pos);
|
|
|
|
node->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
this->addChild(node);
|
|
|
|
|
|
|
|
auto animation = Animation3D::create(filePath);
|
|
|
|
auto animate = Animate3D::create(animation);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (animate)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
agentNode->runAction(RepeatForever::create(animate));
|
|
|
|
animate->setSpeed(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
_agents.push_back(std::make_pair(agent, animate));
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void NavMeshBaseTestDemo::createObstacle(const Vec3& pos)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto obstacle = NavMeshObstacle::create(2.0f, 8.0f);
|
2022-07-05 14:48:46 +08:00
|
|
|
auto obstacleNode = MeshRenderer::create("MeshRendererTest/cylinder.c3b");
|
2019-11-23 20:27:39 +08:00
|
|
|
obstacleNode->setPosition3D(pos + Vec3(0.0f, -0.5f, 0.0f));
|
|
|
|
obstacleNode->setRotation3D(Vec3(-90.0f, 0.0f, 0.0f));
|
|
|
|
obstacleNode->setScale(0.3f);
|
|
|
|
obstacleNode->addComponent(obstacle);
|
|
|
|
obstacleNode->setCameraMask((unsigned short)CameraFlag::USER1);
|
|
|
|
this->addChild(obstacleNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec3 jump(const Vec3* pV1, const Vec3* pV2, float height, float t)
|
|
|
|
{
|
|
|
|
Vec3 pOut;
|
|
|
|
pOut.x = pV1->x + t * (pV2->x - pV1->x);
|
|
|
|
pOut.y = pV1->y + t * (pV2->y - pV1->y);
|
|
|
|
pOut.z = pV1->z + t * (pV2->z - pV1->z);
|
|
|
|
pOut.y += height * sinf((float)M_PI * t);
|
|
|
|
return pOut;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void NavMeshBaseTestDemo::moveAgents(const cocos2d::Vec3& des)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
for (auto iter : _agents)
|
|
|
|
{
|
|
|
|
NavMeshAgent::MoveCallback callback = [](NavMeshAgent* agent, float totalTimeAfterMove) {
|
|
|
|
AgentUserData* data = static_cast<AgentUserData*>(agent->getUserData());
|
|
|
|
if (agent->isOnOffMeshLink())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
agent->setAutoTraverseOffMeshLink(false);
|
|
|
|
agent->setAutoOrientation(false);
|
|
|
|
OffMeshLinkData linkdata = agent->getCurrentOffMeshLinkData();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
agent->getOwner()->setPosition3D(
|
|
|
|
jump(&linkdata.startPosition, &linkdata.endPosition, 10.0f, data->time));
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec3 dir = linkdata.endPosition - linkdata.startPosition;
|
|
|
|
dir.y = 0.0f;
|
|
|
|
dir.normalize();
|
|
|
|
Vec3 axes;
|
|
|
|
Vec3 refAxes = Vec3(-1.0f, 0.0f, 1.0f);
|
|
|
|
refAxes.normalize();
|
|
|
|
Vec3::cross(refAxes, dir, &axes);
|
|
|
|
float angle = Vec3::dot(refAxes, dir);
|
|
|
|
agent->getOwner()->setRotationQuat(Quaternion(axes, acosf(angle)));
|
|
|
|
data->time += 0.01f;
|
2021-12-31 12:12:40 +08:00
|
|
|
if (1.0f < data->time)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
agent->completeOffMeshLink();
|
|
|
|
agent->setAutoOrientation(true);
|
|
|
|
data->time = 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
iter.first->move(des, callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavMeshBaseTestDemo::update(float delta)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
for (auto iter : _agents)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
float speed = iter.first->getCurrentVelocity().length() * 0.2f;
|
|
|
|
iter.second->setSpeed(0.0f < speed ? speed : 0.0f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
NavMeshBasicTestDemo::NavMeshBasicTestDemo() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
NavMeshBasicTestDemo::~NavMeshBasicTestDemo() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
std::string NavMeshBasicTestDemo::title() const
|
|
|
|
{
|
|
|
|
return "Navigation Mesh Test";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string NavMeshBasicTestDemo::subtitle() const
|
|
|
|
{
|
|
|
|
return "Basic Test";
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void NavMeshBasicTestDemo::touchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!_needMoveAgents)
|
|
|
|
return;
|
|
|
|
if (!touches.empty())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto touch = touches[0];
|
|
|
|
auto location = touch->getLocationInView();
|
|
|
|
Vec3 nearP(location.x, location.y, 0.0f), farP(location.x, location.y, 1.0f);
|
|
|
|
|
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
_camera->unproject(size, &nearP, &nearP);
|
|
|
|
_camera->unproject(size, &farP, &farP);
|
|
|
|
|
|
|
|
Physics3DWorld::HitResult result;
|
|
|
|
getPhysics3DWorld()->rayCast(nearP, farP, &result);
|
|
|
|
moveAgents(result.hitPosition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NavMeshBasicTestDemo::init()
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!NavMeshBaseTestDemo::init())
|
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 15);
|
|
|
|
_debugLabel = Label::createWithTTF(ttfConfig, "Debug Draw ON");
|
|
|
|
_debugLabel->retain();
|
2021-12-31 12:12:40 +08:00
|
|
|
auto menuItem1 = MenuItemLabel::create(_debugLabel, [=](Ref*) {
|
2019-11-23 20:27:39 +08:00
|
|
|
bool enabledDebug = !getNavMesh()->isDebugDrawEnabled();
|
|
|
|
getNavMesh()->setDebugDrawEnable(enabledDebug);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (enabledDebug)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_debugLabel->setString("Debug Draw ON");
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_debugLabel->setString("Debug Draw OFF");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
menuItem1->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
|
|
|
|
menuItem1->setPosition(Vec2(VisibleRect::left().x, VisibleRect::top().y - 100));
|
|
|
|
auto menu = Menu::create(menuItem1, nullptr);
|
|
|
|
menu->setPosition(Vec2::ZERO);
|
|
|
|
addChild(menu);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavMeshBasicTestDemo::onEnter()
|
|
|
|
{
|
|
|
|
NavMeshBaseTestDemo::onEnter();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Physics3DWorld::HitResult result;
|
|
|
|
getPhysics3DWorld()->rayCast(Vec3(0.0f, 50.0f, 0.0f), Vec3(0.0f, -50.0f, 0.0f), &result);
|
|
|
|
createAgent(result.hitPosition);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
NavMeshAdvanceTestDemo::NavMeshAdvanceTestDemo() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
NavMeshAdvanceTestDemo::~NavMeshAdvanceTestDemo() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool NavMeshAdvanceTestDemo::init()
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!NavMeshBaseTestDemo::init())
|
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 15);
|
|
|
|
_obstacleLabel = Label::createWithTTF(ttfConfig, "Create Obstacle");
|
|
|
|
_obstacleLabel->retain();
|
|
|
|
_agentLabel = Label::createWithTTF(ttfConfig, "Create Agent");
|
|
|
|
_agentLabel->retain();
|
|
|
|
_debugLabel = Label::createWithTTF(ttfConfig, "Debug Draw ON");
|
|
|
|
_debugLabel->retain();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto menuItem0 = MenuItemLabel::create(_obstacleLabel, [=](Ref*) {
|
2019-11-23 20:27:39 +08:00
|
|
|
float x = cocos2d::random(-50.0f, 50.0f);
|
|
|
|
float z = cocos2d::random(-50.0f, 50.0f);
|
|
|
|
Physics3DWorld::HitResult result;
|
|
|
|
getPhysics3DWorld()->rayCast(Vec3(x, 50.0f, z), Vec3(x, -50.0f, z), &result);
|
|
|
|
createObstacle(result.hitPosition);
|
|
|
|
});
|
|
|
|
menuItem0->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
|
|
|
|
menuItem0->setPosition(Vec2(VisibleRect::left().x, VisibleRect::top().y - 50));
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto menuItem1 = MenuItemLabel::create(_agentLabel, [=](Ref*) {
|
2019-11-23 20:27:39 +08:00
|
|
|
float x = cocos2d::random(-50.0f, 50.0f);
|
|
|
|
float z = cocos2d::random(-50.0f, 50.0f);
|
|
|
|
Physics3DWorld::HitResult result;
|
|
|
|
getPhysics3DWorld()->rayCast(Vec3(x, 50.0f, z), Vec3(x, -50.0f, z), &result);
|
|
|
|
createAgent(result.hitPosition);
|
|
|
|
});
|
|
|
|
menuItem1->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
|
|
|
|
menuItem1->setPosition(Vec2(VisibleRect::left().x, VisibleRect::top().y - 100));
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto menuItem2 = MenuItemLabel::create(_debugLabel, [=](Ref*) {
|
2019-11-23 20:27:39 +08:00
|
|
|
bool enabledDebug = !getNavMesh()->isDebugDrawEnabled();
|
|
|
|
getNavMesh()->setDebugDrawEnable(enabledDebug);
|
2021-12-31 12:12:40 +08:00
|
|
|
if (enabledDebug)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_debugLabel->setString("Debug Draw ON");
|
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
else
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_debugLabel->setString("Debug Draw OFF");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
menuItem2->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
|
|
|
|
menuItem2->setPosition(Vec2(VisibleRect::left().x, VisibleRect::top().y - 150));
|
|
|
|
|
|
|
|
auto menu = Menu::create(menuItem0, menuItem1, menuItem2, nullptr);
|
|
|
|
menu->setPosition(Vec2::ZERO);
|
|
|
|
addChild(menu);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavMeshAdvanceTestDemo::onEnter()
|
|
|
|
{
|
|
|
|
NavMeshBaseTestDemo::onEnter();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Physics3DWorld::HitResult result;
|
|
|
|
getPhysics3DWorld()->rayCast(Vec3(0.0f, 50.0f, 0.0f), Vec3(0.0f, -50.0f, 0.0f), &result);
|
|
|
|
createAgent(result.hitPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string NavMeshAdvanceTestDemo::title() const
|
|
|
|
{
|
|
|
|
return "Navigation Mesh Test";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string NavMeshAdvanceTestDemo::subtitle() const
|
|
|
|
{
|
|
|
|
return "Advance Test";
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void NavMeshAdvanceTestDemo::touchesEnded(const std::vector<cocos2d::Touch*>& touches, cocos2d::Event* event)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!_needMoveAgents)
|
|
|
|
return;
|
|
|
|
if (!touches.empty())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
auto touch = touches[0];
|
|
|
|
auto location = touch->getLocationInView();
|
|
|
|
Vec3 nearP(location.x, location.y, 0.0f), farP(location.x, location.y, 1.0f);
|
|
|
|
|
|
|
|
auto size = Director::getInstance()->getWinSize();
|
|
|
|
_camera->unproject(size, &nearP, &nearP);
|
|
|
|
_camera->unproject(size, &farP, &farP);
|
|
|
|
|
|
|
|
Physics3DWorld::HitResult result;
|
|
|
|
getPhysics3DWorld()->rayCast(nearP, farP, &result);
|
|
|
|
moveAgents(result.hitPosition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|