DotRecastNetSim/test/DotRecast.Detour.Crowd.Test/AbstractCrowdTest.cs

173 lines
6.0 KiB
C#
Raw Normal View History

2023-03-14 08:02:43 +03:00
/*
Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
2023-03-15 17:00:29 +03:00
DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
2023-03-14 08:02:43 +03:00
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
using System;
using System.Collections.Generic;
2023-03-29 18:59:00 +03:00
using DotRecast.Core;
2023-04-23 08:13:10 +03:00
using DotRecast.Detour.QueryResults;
2023-03-14 08:02:43 +03:00
using NUnit.Framework;
namespace DotRecast.Detour.Crowd.Test;
2023-03-25 09:43:20 +03:00
using static DotRecast.Core.RecastMath;
2023-03-14 08:02:43 +03:00
2023-04-25 17:22:44 +03:00
[Parallelizable]
2023-03-16 19:48:49 +03:00
public class AbstractCrowdTest
{
protected readonly long[] startRefs =
{
281474976710696L, 281474976710773L, 281474976710680L, 281474976710753L,
281474976710733L
};
2023-03-14 08:02:43 +03:00
protected readonly long[] endRefs = { 281474976710721L, 281474976710767L, 281474976710758L, 281474976710731L, 281474976710772L };
2023-03-29 18:59:00 +03:00
protected readonly Vector3f[] startPoss =
2023-03-16 19:48:49 +03:00
{
2023-03-29 18:59:00 +03:00
Vector3f.Of(22.60652f, 10.197294f, -45.918674f),
Vector3f.Of(22.331268f, 10.197294f, -1.0401875f),
Vector3f.Of(18.694363f, 15.803535f, -73.090416f),
Vector3f.Of(0.7453353f, 10.197294f, -5.94005f),
Vector3f.Of(-20.651257f, 5.904126f, -13.712508f),
2023-03-16 19:48:49 +03:00
};
2023-03-14 08:02:43 +03:00
2023-03-29 18:59:00 +03:00
protected readonly Vector3f[] endPoss =
2023-03-16 19:48:49 +03:00
{
2023-03-29 18:59:00 +03:00
Vector3f.Of(6.4576626f, 10.197294f, -18.33406f),
Vector3f.Of(-5.8023443f, 0.19729415f, 3.008419f),
Vector3f.Of(38.423977f, 10.197294f, -0.116066754f),
Vector3f.Of(0.8635526f, 10.197294f, -10.31032f),
Vector3f.Of(18.784092f, 10.197294f, 3.0543678f),
2023-03-16 19:48:49 +03:00
};
2023-03-14 08:02:43 +03:00
protected MeshData nmd;
protected NavMeshQuery query;
protected NavMesh navmesh;
protected Crowd crowd;
protected List<CrowdAgent> agents;
[SetUp]
2023-05-05 02:44:48 +03:00
public void SetUp()
2023-03-16 19:48:49 +03:00
{
2023-05-05 02:44:48 +03:00
nmd = new RecastTestMeshBuilder().GetMeshData();
2023-03-14 08:02:43 +03:00
navmesh = new NavMesh(nmd, 6, 0);
query = new NavMeshQuery(navmesh);
CrowdConfig config = new CrowdConfig(0.6f);
crowd = new Crowd(config, navmesh);
2023-04-23 08:15:45 +03:00
ObstacleAvoidanceParams option = new ObstacleAvoidanceParams();
2023-03-14 08:02:43 +03:00
option.velBias = 0.5f;
option.adaptiveDivs = 5;
option.adaptiveRings = 2;
option.adaptiveDepth = 1;
2023-05-05 02:44:48 +03:00
crowd.SetObstacleAvoidanceParams(0, option);
2023-04-23 08:15:45 +03:00
option = new ObstacleAvoidanceParams();
2023-03-14 08:02:43 +03:00
option.velBias = 0.5f;
option.adaptiveDivs = 5;
option.adaptiveRings = 2;
option.adaptiveDepth = 2;
2023-05-05 02:44:48 +03:00
crowd.SetObstacleAvoidanceParams(1, option);
2023-04-23 08:15:45 +03:00
option = new ObstacleAvoidanceParams();
2023-03-14 08:02:43 +03:00
option.velBias = 0.5f;
option.adaptiveDivs = 7;
option.adaptiveRings = 2;
option.adaptiveDepth = 3;
2023-05-05 02:44:48 +03:00
crowd.SetObstacleAvoidanceParams(2, option);
2023-04-23 08:15:45 +03:00
option = new ObstacleAvoidanceParams();
2023-03-14 08:02:43 +03:00
option.velBias = 0.5f;
option.adaptiveDivs = 7;
option.adaptiveRings = 3;
option.adaptiveDepth = 3;
2023-05-05 02:44:48 +03:00
crowd.SetObstacleAvoidanceParams(3, option);
2023-03-14 08:02:43 +03:00
agents = new();
}
2023-05-05 02:44:48 +03:00
protected CrowdAgentParams GetAgentParams(int updateFlags, int obstacleAvoidanceType)
2023-03-16 19:48:49 +03:00
{
2023-03-14 08:02:43 +03:00
CrowdAgentParams ap = new CrowdAgentParams();
ap.radius = 0.6f;
ap.height = 2f;
ap.maxAcceleration = 8.0f;
ap.maxSpeed = 3.5f;
ap.collisionQueryRange = ap.radius * 12f;
ap.pathOptimizationRange = ap.radius * 30f;
ap.updateFlags = updateFlags;
ap.obstacleAvoidanceType = obstacleAvoidanceType;
ap.separationWeight = 2f;
return ap;
}
2023-05-05 02:44:48 +03:00
protected void AddAgentGrid(int size, float distance, int updateFlags, int obstacleAvoidanceType, Vector3f startPos)
2023-03-16 19:48:49 +03:00
{
2023-05-05 02:44:48 +03:00
CrowdAgentParams ap = GetAgentParams(updateFlags, obstacleAvoidanceType);
2023-03-16 19:48:49 +03:00
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
2023-03-28 18:03:33 +03:00
Vector3f pos = new Vector3f();
2023-04-29 07:29:40 +03:00
pos.x = startPos.x + i * distance;
pos.y = startPos.y;
pos.z = startPos.z + j * distance;
2023-05-05 02:44:48 +03:00
agents.Add(crowd.AddAgent(pos, ap));
2023-03-14 08:02:43 +03:00
}
}
}
2023-05-05 02:44:48 +03:00
protected void SetMoveTarget(Vector3f pos, bool adjust)
2023-03-16 19:48:49 +03:00
{
2023-05-05 02:44:48 +03:00
Vector3f ext = crowd.GetQueryExtents();
QueryFilter filter = crowd.GetFilter(0);
2023-03-16 19:48:49 +03:00
if (adjust)
{
2023-05-05 02:44:48 +03:00
foreach (CrowdAgent ag in crowd.GetActiveAgents())
2023-03-16 19:48:49 +03:00
{
2023-05-05 02:44:48 +03:00
Vector3f vel = CalcVel(ag.npos, pos, ag.option.maxSpeed);
crowd.RequestMoveVelocity(ag, vel);
2023-03-14 08:02:43 +03:00
}
2023-03-16 19:48:49 +03:00
}
else
{
2023-05-05 02:44:48 +03:00
Result<FindNearestPolyResult> nearest = query.FindNearestPoly(pos, ext, filter);
foreach (CrowdAgent ag in crowd.GetActiveAgents())
2023-03-16 19:48:49 +03:00
{
2023-05-05 02:44:48 +03:00
crowd.RequestMoveTarget(ag, nearest.result.GetNearestRef(), nearest.result.GetNearestPos());
2023-03-14 08:02:43 +03:00
}
}
}
2023-05-05 02:44:48 +03:00
protected Vector3f CalcVel(Vector3f pos, Vector3f tgt, float speed)
2023-03-16 19:48:49 +03:00
{
2023-05-05 02:44:48 +03:00
Vector3f vel = VSub(tgt, pos);
2023-04-29 07:29:40 +03:00
vel.y = 0.0f;
2023-05-05 02:44:48 +03:00
VNormalize(ref vel);
vel = VScale(vel, speed);
2023-03-14 08:02:43 +03:00
return vel;
}
2023-05-05 02:44:48 +03:00
protected void DumpActiveAgents(int i)
2023-03-16 19:48:49 +03:00
{
2023-05-05 02:44:48 +03:00
Console.WriteLine(crowd.GetActiveAgents().Count);
foreach (CrowdAgent ag in crowd.GetActiveAgents())
2023-03-16 19:48:49 +03:00
{
2023-03-14 08:02:43 +03:00
Console.WriteLine(ag.state + ", " + ag.targetState);
2023-04-29 07:29:40 +03:00
Console.WriteLine(ag.npos.x + ", " + ag.npos.y + ", " + ag.npos.z);
Console.WriteLine(ag.nvel.x + ", " + ag.nvel.y + ", " + ag.nvel.z);
2023-03-14 08:02:43 +03:00
}
}
2023-03-16 19:48:49 +03:00
}