Tutorial_PointOfInterest.cpp

Tutorial_PointOfInterest.cpp
/*
* Copyright 2015 Autodesk, Inc. All rights reserved.
* Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
* or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
*/
#include "GwNavTestFwk/GwNavTestFwk.h"
#include "GwNavTestFwk/TestEnv.h"
#include "common/oneworldenv.h"
#include "LabEngine/utils/labengineutils.h"
//RUN_THIS_FILE
namespace
{
// Game object representing a point of interest.
class MyGamePointOfInterest
{
public:
MyGamePointOfInterest() : m_navPointOfInterest(KY_NULL) {}
void Initialize(Kaim::World* world, const Kaim::Vec3f& position);
void Destroy();
void SetPosition(const Kaim::Vec3f& newPosition);
Kaim::Ptr<Kaim::PointOfInterest> m_navPointOfInterest;
KyUInt32 m_id; // used only to differentiate different instances in this tutorial.
};
void MyGamePointOfInterest::Initialize(Kaim::World* world, const Kaim::Vec3f& position)
{
// Set an id
static KyUInt32 id = 0;
m_id = id;
++id;
// Set up the PointOfInterestInitConfig
Kaim::PointOfInterestInitConfig pointOfInterestInitConfig;
pointOfInterestInitConfig.m_world = world;
pointOfInterestInitConfig.m_startPosition = position;
pointOfInterestInitConfig.m_poiType = Kaim::PointOfInterestType_FirstClient;
// Create and initialize the point of interest, and add it to its World
m_navPointOfInterest = *KY_NEW Kaim::PointOfInterest;
m_navPointOfInterest->Init(pointOfInterestInitConfig);
m_navPointOfInterest->AddToWorld();
// Set a pointer to your own data
m_navPointOfInterest->SetUserData(this);
// We want to draw a maximum amount of information
m_navPointOfInterest->SetCurrentVisualDebugLOD(Kaim::VisualDebugLOD_Maximal);
}
void MyGamePointOfInterest::Destroy()
{
m_navPointOfInterest->RemoveFromWorld();
m_navPointOfInterest = KY_NULL;
}
void MyGamePointOfInterest::SetPosition(const Kaim::Vec3f& newPosition)
{
m_navPointOfInterest->SetPosition(newPosition);
}
// A simple collection function that just render the nearby PointOfInterest
void CollectAndRenderNearbyPointsOfInterest(Kaim::Database* database, const Kaim::Vec3f& collectorBoxCenter, KyFloat32 collectorBoxHalfExtentFloat)
{
// Collect nearby PointOfInterest
// Set up a filter
Kaim::SpatializedPointCollectorFilter_SelectiveCollect selectiveCollect;
selectiveCollect.Select(Kaim::SpatializedPointObjectType_PointOfInterest);
// Instantiate the query
query.BindToDatabase(database);
// Set up the query parameters
const Kaim::Vec3f collectorBoxHalfExtentVec3f(collectorBoxHalfExtentFloat, collectorBoxHalfExtentFloat, collectorBoxHalfExtentFloat);
const Kaim::Box3f collectorBoxExtent(collectorBoxHalfExtentVec3f, collectorBoxHalfExtentVec3f);
query.Initialize(collectorBoxCenter, collectorBoxExtent);
// Perform the query
// Do something with them... => render their id
Kaim::ScopedDisplayList displayList(database->GetWorld(), Kaim::DisplayList_Enable);
displayList.InitSingleFrameLifespan("Nearby POIs", "Tutorial_PointOfInterest");
Kaim::VisualShapeColor displayColor;
displayColor.m_lineColor = Kaim::VisualColor::Magenta;
displayList.PushBox(query.ComputeAABB(), displayColor);
if (query.GetResult() == Kaim::SPATIALIZEDPOINTCOLLECTOR_DONE)
{
Kaim::QueryDynamicOutput* queryDynamicOutput = query.GetQueryDynamicOutput();
if (queryDynamicOutput)
{
for(KyUInt32 i = 0; i < queryDynamicOutput->GetSpatializedPointCount(); ++i)
{
Kaim::SpatializedPoint* spatializedPoint = queryDynamicOutput->GetSpatializedPoint(i);
if (spatializedPoint)
{
const Kaim::Vec3f& position = spatializedPoint->GetPosition();
displayList.PushArrow(position + Kaim::Vec3f::UnitZ(), position, 0.1f, displayColor);
if (spatializedPoint->GetObjectType() == Kaim::SpatializedPointObjectType_PointOfInterest)
{
Kaim::StringStream displayText;
Kaim::PointOfInterest* pointOfInterest = (Kaim::PointOfInterest*)spatializedPoint->GetObject();
MyGamePointOfInterest* myGamePointOfInterest = (MyGamePointOfInterest*)pointOfInterest->GetUserData();
displayText << " POI #" << myGamePointOfInterest->m_id;
displayList.PushText(position + Kaim::Vec3f::UnitZ(), Kaim::VisualColor::White, displayText.CStr());
}
}
}
}
}
}
#define TEST_ENV_CLASS OneWorldEnv
TEST_ENV {}
TUTORIAL
{
KT_LOG_TITLE_BEGIN("TUTORIAL - PointOfInterest");
// Tutorial parameters
const KyUInt32 simulationFrameCount = 60; // Increase this value if you want to visual debug this tutorial.
const KyFloat32 simulationDeltaTimeInSeconds = 0.016f;
// Simulation environment setup
// Setup a world (load some NavData, start VisualDebug...)
CHECK(env.LoadAndAddNavData("GeneratedNavData/plan/plan.NavData") != KY_NULL);
env.StartVisualDebugUsingLab();
// Useful references
Kaim::World* navWorld = env.GetWorld();
Kaim::Database* navDatabase = env.GetDefaultDatabase();
// Create a bot with simple route
LabEngine::GameBotInitConfig gameBotInitConfig;
gameBotInitConfig.m_botInitConfig.m_database = navDatabase;
gameBotInitConfig.m_botInitConfig.m_startPosition.Set(0.0f, 0.0f, 0.0f);
LabEngine::GameBot* gameBot = env.AddBotFromConfig(gameBotInitConfig);
gameBot->PushWayPoint(Kaim::Vec3f( 0.0f, 0.0f, 0.0f));
gameBot->PushWayPoint(Kaim::Vec3f(15.0f, 15.0f, 0.0f));
// Define some interesting positions
Kaim::Array<Kaim::Vec3f> positions;
positions.PushBack(Kaim::Vec3f( 5.0f, 5.0f, 0.0f));
positions.PushBack(Kaim::Vec3f( 5.0f, 10.0f, 0.0f));
positions.PushBack(Kaim::Vec3f(10.0f, 10.0f, 0.0f));
positions.PushBack(Kaim::Vec3f(10.0f, 5.0f, 0.0f));
// Tutorial-specific setup
// Create a MyGamePointOfInterest
MyGamePointOfInterest pointOfInterest[2];
pointOfInterest[0].Initialize(navWorld, positions[0]);
pointOfInterest[1].Initialize(navWorld, Kaim::Vec3f(7.5f, 7.5f, 0.0f));
// Simulation loop
for (KyUInt32 frameIdx = 0; frameIdx < simulationFrameCount; ++frameIdx)
{
env.Update(simulationDeltaTimeInSeconds);
LabEngine::Utils::TimedBusyWait(1000.0f * simulationDeltaTimeInSeconds);
// Collect and render nearby PointOfInterest
CollectAndRenderNearbyPointsOfInterest(navDatabase, gameBot->m_navBot->GetPosition(), 2.0f);
// Sometimes move the first PointOfInterest
if (frameIdx % 10 == 0)
{
const KyUInt32 scenarioCycleIdx = frameIdx / 10;
const Kaim::Vec3f& newPosition = positions[scenarioCycleIdx % positions.GetSize()];
pointOfInterest[0].SetPosition(newPosition);
}
}
// Clean up
// Destroy PointOfInterest instances
pointOfInterest[0].Destroy();
pointOfInterest[1].Destroy();
// Clean up the world
env.CleanUpWorld();
}
}