Moderators: Sascha Willems, walaber
class MyClosestNode
{
MyClosestNode ( );
void BuildDataBase (Vector pointList, int count);
int GetClosestPoints (int* const array);
};
void GameMainLoop()
{
MyClosestNode nearestNeighborg;
while(1)
{
nearestNeighborg. BuildDataBase ();
for (i = 0; i < pointCount; i ++) {
int indexArray[128];
int count = nearestNeighborg.GetClosestPoints(indexArray);
Object obj1 = ObjList[i];
// for each point in indexArray do you AI stuff
for (j = 0; j < count; j ++) {
Object obj2 = ObjList[indexArray]
// do you AI stuff on this pair
DoAI (obj1, obj2);
}
}
}
}
nearestNeighborg.BuildDatabase(&PlayerCoords[0], PlayerCoords.size());
for(int k = 0; k < PlayersInZoneManager::GetInstance().PlayersInZone.size(); ++k)
{
int indexArray[128];
int count = nearestNeighborg.GetClosestPoints(indexArray);
Object obj1 = ObjList[i]; //Whats this??
// for each point in indexArray do you AI stuff
for(j = 0; j < count; ++j)
{
Object obj2 = ObjList[indexArray]
// do you AI stuff on this pair
DoAI (obj1, obj2); //I only need to know if body is within range of another for now
}
}
// VoronoiDemo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "dg.h"
class ClosestNeighborg: public dgConvexHull3d
{
class FaceList: public dgList<dgConvexHull3d::dgListNode*>
{
public:
FaceList (dgMemoryAllocator* const allocator)
:dgList<dgConvexHull3d::dgListNode*>(allocator)
{
}
};
class FaceMap: public dgTree<FaceList, int>
{
public:
FaceMap (dgMemoryAllocator* const allocator)
:dgTree<FaceList, int>(allocator)
{
}
};
public:
ClosestNeighborg(dgMemoryAllocator* const allocator, dgBigVector* const points, dgInt32 count)
:dgConvexHull3d (allocator)
,m_lru(0)
,m_faceMap (allocator)
,m_mask(256, allocator)
{
for (dgInt32 i = 0; i < count; i ++) {
points[i].m_z = points[i].m_x * points[i].m_x + points[i].m_y * points[i].m_y;
points[i].m_w = i;
}
m_mask[count - 1] = 0;
memset (&m_mask[0], 0, sizeof (unsigned));
BuildHUll (&points[0].m_x, sizeof (dgBigVector), count, 0.0, 0x7fffffff);
}
int GetClosestPoints (int index, int* const arrayOut, int maxCount)
{
FaceMap::dgTreeNode* const faceMapNode = m_faceMap.Find(index);
_ASSERTE (faceMapNode);
int count = 0;
m_lru ++;
unsigned* markIndex = &m_mask[0];
markIndex[index] = m_lru;
FaceList& faceList = faceMapNode->GetInfo();
for (FaceList::dgListNode* faceNode = faceList.GetFirst(); faceNode; faceNode = faceNode->GetNext()) {
dgConvexHull3DFace* const face = &faceNode->GetInfo()->GetInfo();
if (IsLowerHull (face)) {
for (int i = 0; i < 3; i ++) {
int k = face->m_index[i];
const dgBigVector& vertex = GetVertex(k);
k = int (vertex.m_w);
if (markIndex[k] != m_lru) {
arrayOut[count] = k;
markIndex[k] = m_lru;
count ++;
_ASSERTE (count < maxCount);
}
}
}
}
return count;
}
// for incremental update wne a point move, but let us see hwo tehis work first.
void AddPoint(int point)
{
}
void RemovePoint(int point)
{
}
void DebugRender()
{
for (ClosestNeighborg::dgListNode* node = GetFirst(); node; node = node->GetNext()) {
dgConvexHull3DFace* const face = &node->GetInfo();
if (IsLowerHull (face)) {
dgVector vertex0 (GetVertex(face->m_index[0]));
dgVector vertex1 (GetVertex(face->m_index[1]));
dgVector vertex2 (GetVertex(face->m_index[2]));
vertex0.m_z = 0.0f;
vertex1.m_z = 0.0f;
vertex2.m_z = 0.0f;
// Render this triangle (vertex0, vertex1, vertex2);
}
}
}
private:
virtual dgListNode* AddFace (dgInt32 i0, dgInt32 i1, dgInt32 i2)
{
dgListNode* const face = dgConvexHull3d::AddFace(i0, i1, i2);
const dgBigVector& vertex0 = GetVertex(i0);
const dgBigVector& vertex1 = GetVertex(i1);
const dgBigVector& vertex2 = GetVertex(i2);
int index[3];
index[0] = int (vertex0.m_w);
index[1] = int (vertex1.m_w);
index[2] = int (vertex2.m_w);
for (int i = 0; i < 3; i ++) {
FaceMap::dgTreeNode* node = m_faceMap.Find(index[i]);
if (!node) {
FaceList faceList (GetAllocator());
node = m_faceMap.Insert(faceList, index[i]);
}
FaceList& faceList = node->GetInfo();
faceList.Append(face);
}
return face;
}
virtual void DeleteFace (dgListNode* const node)
{
dgConvexHull3DFace* const face = &node->GetInfo();
for (int i = 0; i < 3; i ++) {
int index = face->m_index[i];
const dgBigVector& vertex = GetVertex(index);
index = int (vertex.m_w);
FaceMap::dgTreeNode* const mapNode = m_faceMap.Find(index);
_ASSERTE (mapNode);
FaceList& faceList = mapNode->GetInfo();
for (FaceList::dgListNode* faceNode = faceList.GetFirst(); faceNode; faceNode = faceNode->GetNext()) {
if (faceNode->GetInfo() == node) {
faceList.Remove(faceNode);
_ASSERTE (faceList.GetCount());
break;
}
}
}
dgConvexHull3d::DeleteFace(node);
}
bool IsLowerHull (dgConvexHull3DFace* const face) const
{
const dgBigVector& vertex0 = GetVertex(face->m_index[0]);
const dgBigVector& vertex1 = GetVertex(face->m_index[1]);
const dgBigVector& vertex2 = GetVertex(face->m_index[2]);
dgBigVector e0 (vertex1 - vertex0);
dgBigVector e1 (vertex2 - vertex0);
dgBigVector n (e0 * e1);
return (n.m_z < 0.0f);
}
int m_lru;
FaceMap m_faceMap;
dgArray<unsigned> m_mask;
};
int _tmain(int argc, _TCHAR* argv[])
{
dgMemoryAllocator allocator;
dgInt32 count = 0;
dgBigVector points[1000];
// populated the map with point representing the entities
while (count < sizeof (points)/ sizeof (points[0])) {
// make sure no point is duplicated
dgBigVector p (100.0f * (float (rand()) / RAND_MAX - 1.0f), 100.0f * (float (rand()) / RAND_MAX - 1.0f), 0, 0) ;
int i = 0;
for (; i < count; i ++) {
dgBigVector dist (p - points[i]);
if ((dist % dist) < 1.0e-6) {
break;
}
}
if (i == count) {
points[count] = p;
count ++;
}
}
// how to use it
ClosestNeighborg closertNighborg (&allocator, points, count);
for (int i = 0; i < 20; i ++) {
int arrayOut[20];
int index = rand () % (sizeof (points) / sizeof (points[0]));
int count = closertNighborg.GetClosestPoints (index, arrayOut, sizeof (arrayOut) / sizeof (arrayOut[0]));
count *=1;
}
return 0;
}
dgBigVector point(indexx + 12, indexx +22, 0.0f, 0.0f); //Test values, I tried with real values as well
BigVector[indexx] = point;
extern dgBigVector BigVector[1000];
dgMemoryAllocator allocator;
dgInt32 count = 5; //tried 0, still crash but elsewhere
ClosestNeighborg closertNighborg(&allocator, BigVector, count);
for(int i = 0; i < 5; ++i)
{
int arrayOut[2];
int count = closertNighborg.GetClosestPoints(i, arrayOut, sizeof (arrayOut) / sizeof (arrayOut[0]));
count *=1;
std::cout << "arrayOut[0] = : " << arrayOut[0] << " " << "arrayOut[1] = : " << arrayOut[1] << " " << "count = " << count << std::endl;
}
KingSnail wrote:closertNighborg.GetClosestPoints(...);
that gets closest points but how close?
If I have 2 players and they are apart like 100000 units.
If I query cloest points for player 1 does that mean that it
will give me the player that was 100000 units away? since he is closest?
Do I still have to check distance? or does this algorithm incorporate it.
I havent compiled it yet, just trying now.
Users browsing this forum: No registered users and 1 guest