mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 02:36:14 +00:00
提交RVO寻路
This commit is contained in:
@@ -8,7 +8,7 @@ namespace Game.Plugins.App
|
||||
|
||||
public static readonly JNGSocket Socket = new JNGSocket();
|
||||
public static readonly JNGSyncFrame Sync = new JNGSyncFrame();
|
||||
public static readonly JAPI Api = new(new JAPIConfig(){BaseURL = "http://192.168.0.126:8080"});
|
||||
public static readonly JAPI Api = new(new JAPIConfig(){BaseURL = "http://192.168.0.123:8080"});
|
||||
public static readonly EventDispatcher Event = EventDispatcher.Event;
|
||||
|
||||
public static SystemBase[] System()
|
||||
|
3
JNFrame/Assets/Game/Plugins/App/Game/RVO2.meta
Normal file
3
JNFrame/Assets/Game/Plugins/App/Game/RVO2.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 137b1b51468447048d60cf74f154783e
|
||||
timeCreated: 1708511241
|
87
JNFrame/Assets/Game/Plugins/App/Game/RVO2/JNGRVO2Agent.cs
Normal file
87
JNFrame/Assets/Game/Plugins/App/Game/RVO2/JNGRVO2Agent.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using Game.Plugins.App.Sync;
|
||||
using Plugins.JNGame.Sync.Frame.AStar.RVO2;
|
||||
using RVO;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
using Vector2 = RVO.Vector2;
|
||||
|
||||
namespace Game.Plugins.App.Game.RVO2
|
||||
{
|
||||
public class JNGRVO2Agent : JNGSyncFrameDefault
|
||||
{
|
||||
|
||||
private JNRVO2Simulator Simulator => GetComponentInParent<JNGRVO2Manager>().Simulator;
|
||||
public float maxSpeed = 20;
|
||||
public float radius = 0.5f;
|
||||
[HideInInspector] public int sid = -1;
|
||||
private Vector2 target;
|
||||
|
||||
public override void OnSyncLoad()
|
||||
{
|
||||
base.OnSyncLoad();
|
||||
|
||||
var position = this.transform.position;
|
||||
//设置Agent的默认属性
|
||||
float neighborDist = 15.0f;//检测与邻居避障的最大距离(要关心的范围),越大纳入思考的数据量越大,越小越不安全
|
||||
int maxNeighbors = 10;//检测与邻居避障的最大个数(要关心的其他单位),越大纳入思考的数据量越大,越小越不安全
|
||||
float timeHorizon = 5.0f; //预测提前规避时间, 提前得越多,速度变化越频繁
|
||||
float timeHorizonObst = 5.0f; //预测提前规避时间(针对固定障碍), 提前得越多,速度变化越频繁
|
||||
float radius = this.radius;//阻挡半径
|
||||
float maxSpeed = this.maxSpeed;//所能达到得最大速度
|
||||
Vector2 velocity = new Vector2(0.0f, 0.0f);//初始的 2元线性速度,影响出生单位时排挤其他的速度
|
||||
sid = Simulator.addAgent(new Vector2(position.x, position.z), neighborDist, maxNeighbors, timeHorizon,
|
||||
timeHorizonObst, radius, maxSpeed,velocity);
|
||||
|
||||
}
|
||||
|
||||
//设置目标点
|
||||
public void SetTarget(Vector2 targetPoint)
|
||||
{
|
||||
this.target = targetPoint;
|
||||
}
|
||||
public void SetTarget(Vector3 targetPoint)
|
||||
{
|
||||
this.target = new Vector2(targetPoint.x,targetPoint.z);
|
||||
}
|
||||
|
||||
public override void OnSyncUpdate(int dt, JNFrameInfo frame, Object input)
|
||||
{
|
||||
base.OnSyncUpdate(dt, frame, input);
|
||||
|
||||
if (sid >= 0)
|
||||
{
|
||||
Vector2 pos = Simulator.getAgentPosition(sid);
|
||||
Vector2 vel = Simulator.getAgentPrefVelocity(sid);
|
||||
transform.position = new Vector3(pos.x(), transform.position.y , pos.y());
|
||||
if (Math.Abs(vel.x()) > 0.01f && Math.Abs(vel.y()) > 0.01f)
|
||||
//以Y轴为移动朝向
|
||||
transform.forward = new Vector3(vel.x(), 0, vel.y()).normalized;
|
||||
}
|
||||
|
||||
// if (!Input.GetMouseButton(1))//如果没有按下右键的话, 就不给予速度向量
|
||||
// {
|
||||
// Simulator.Instance.setAgentPrefVelocity(sid, new Vector2(0, 0));
|
||||
// return;
|
||||
// }
|
||||
|
||||
Vector2 goalVector = new Vector2(target.x(),target.y()) - Simulator.getAgentPosition(sid);
|
||||
if (RVOMath.absSq(goalVector) > 1.0f)
|
||||
{
|
||||
goalVector = RVOMath.normalize(goalVector);
|
||||
}
|
||||
Simulator.setAgentPrefVelocity(sid, goalVector);
|
||||
|
||||
/* Perturb a little to avoid deadlocks due to perfect symmetry. */
|
||||
/* 因为完美对称,所以需要加入些许抖动用来避免死锁
|
||||
* 这里的完美对称,测试出是指两个完全一样的单位,不抖动=中心点一样=无法把其他排斥出去
|
||||
*/
|
||||
float angle = (float)GetSync().nRandomFloat(0,1) * 2.0f * (float)Math.PI;
|
||||
float dist = (float)GetSync().nRandomFloat(0,1) * 0.0001f;
|
||||
|
||||
Simulator.setAgentPrefVelocity(sid, Simulator.getAgentPrefVelocity(sid) +
|
||||
dist *
|
||||
new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)));
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0ba4af5643174f63918b15ca9c43c791
|
||||
timeCreated: 1708511254
|
47
JNFrame/Assets/Game/Plugins/App/Game/RVO2/JNGRVO2Manager.cs
Normal file
47
JNFrame/Assets/Game/Plugins/App/Game/RVO2/JNGRVO2Manager.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using Game.Plugins.App.Sync;
|
||||
using Plugins.JNGame.Sync.Frame.AStar.RVO2;
|
||||
using UnityEngine;
|
||||
using Vector2 = RVO.Vector2;
|
||||
|
||||
namespace Game.Plugins.App.Game.RVO2
|
||||
{
|
||||
public class JNGRVO2Manager : JNGSyncFrameDefault
|
||||
{
|
||||
|
||||
public readonly JNRVO2Simulator Simulator = new JNRVO2Simulator();
|
||||
|
||||
|
||||
public override void OnSyncLoad()
|
||||
{
|
||||
|
||||
base.OnSyncLoad();
|
||||
Simulator.setTimeStep(0.25f);
|
||||
// Simulator.setAgentDefaults(15.0f, 10, 5.0f, 5.0f, 2.0f, 2.0f, new Vector2(0.0f, 0.0f));
|
||||
Simulator.processObstacles();
|
||||
// //设置Agent的默认属性
|
||||
float neighborDist = 15.0f;//检测与邻居避障的最大距离(要关心的范围),越大纳入思考的数据量越大,越小越不安全
|
||||
int maxNeighbors = 10;//检测与邻居避障的最大个数(要关心的其他单位),越大纳入思考的数据量越大,越小越不安全
|
||||
float timeHorizon = 5.0f; //预测提前规避时间, 提前得越多,速度变化越频繁
|
||||
float timeHorizonObst = 5.0f; //预测提前规避时间(针对固定障碍), 提前得越多,速度变化越频繁
|
||||
float radius = .25f;//阻挡半径
|
||||
float maxSpeed = 20f;//所能达到得最大速度
|
||||
Vector2 velocity = new Vector2(0.0f, 0.0f);//初始的 2元线性速度,影响出生单位时排挤其他的速度
|
||||
Simulator.setAgentDefaults(neighborDist, maxNeighbors, timeHorizon, timeHorizonObst,
|
||||
radius, maxSpeed, velocity);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public override void OnSyncUpdate(int dt, JNFrameInfo frame, Object input)
|
||||
{
|
||||
base.OnSyncUpdate(dt, frame, input);
|
||||
// Simulator.doStep();
|
||||
// if (frame.Index > 0)
|
||||
// {
|
||||
// Simulator.doStep();
|
||||
// }
|
||||
Simulator.doStep();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 19fc411c2057411598f3006923e2767a
|
||||
timeCreated: 1708511299
|
41
JNFrame/Assets/Game/Plugins/App/Game/RVO2/JNGRVO2Obstacle.cs
Normal file
41
JNFrame/Assets/Game/Plugins/App/Game/RVO2/JNGRVO2Obstacle.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.Collections.Generic;
|
||||
using Game.Plugins.App.Sync;
|
||||
using Plugins.JNGame.Sync.Frame.AStar.RVO2;
|
||||
using UnityEngine;
|
||||
using Vector2 = RVO.Vector2;
|
||||
|
||||
namespace Game.Plugins.App.Game.RVO2
|
||||
{
|
||||
public class JNGRVO2Obstacle : JNGSyncFrameDefault
|
||||
{
|
||||
|
||||
private JNRVO2Simulator Simulator => GetComponentInParent<JNGRVO2Manager>().Simulator;
|
||||
|
||||
public override void OnSyncLoad()
|
||||
{
|
||||
base.OnSyncLoad();
|
||||
CreateObstacles();
|
||||
}
|
||||
|
||||
public void CreateObstacles()
|
||||
{
|
||||
var boxCollider = this.GetComponent<BoxCollider>();
|
||||
float minX = boxCollider.transform.position.x -
|
||||
boxCollider.size.x*boxCollider.transform.lossyScale.x*0.5f;
|
||||
float minZ = boxCollider.transform.position.z -
|
||||
boxCollider.size.z*boxCollider.transform.lossyScale.z*0.5f;
|
||||
float maxX = boxCollider.transform.position.x +
|
||||
boxCollider.size.x*boxCollider.transform.lossyScale.x*0.5f;
|
||||
float maxZ = boxCollider.transform.position.z +
|
||||
boxCollider.size.z*boxCollider.transform.lossyScale.z*0.5f;
|
||||
IList<Vector2> obstacle = new List<Vector2>();
|
||||
obstacle.Add(new Vector2(maxX, maxZ));
|
||||
obstacle.Add(new Vector2(minX, maxZ));
|
||||
obstacle.Add(new Vector2(minX, minZ));
|
||||
obstacle.Add(new Vector2(maxX, minZ));
|
||||
Simulator.addObstacle(obstacle);
|
||||
Simulator.processObstacles();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1285797067a24053bd74d9c6acdec716
|
||||
timeCreated: 1708568245
|
@@ -8,7 +8,7 @@ namespace Game.Plugins.App
|
||||
protected override async UniTask<string> GetUrl()
|
||||
{
|
||||
await UniTask.NextFrame();
|
||||
return "ws://192.168.0.126:8080/websocket";
|
||||
return "ws://192.168.0.123:8080/websocket";
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user