骑行计算逻辑调整

This commit is contained in:
lishuo 2021-03-23 16:07:31 +08:00
parent 971a6f7e0e
commit 0ab0e99597
15 changed files with 309 additions and 393 deletions

View File

@ -1,7 +1,9 @@
namespace Mapbox.Unity.Map
{
using System.Collections;
using Mapbox.Unity.Location;
using Assets.Scenes.Ride.Scripts;
using Assets.Scripts.Apis;
using Mapbox.Unity.Location;
using UnityEngine;
public class InitializeMapWithLocationProvider : MonoBehaviour
@ -27,7 +29,19 @@
void LocationProvider_OnLocationUpdated(Unity.Location.Location location)
{
_locationProvider.OnLocationUpdated -= LocationProvider_OnLocationUpdated;
_map.Initialize(location.LatitudeLongitude, _map.AbsoluteZoom);
//_map.Initialize(location.LatitudeLongitude, _map.AbsoluteZoom);
MapApi mapApi = new MapApi();
var result = mapApi.GetData(5492);
if (result != null)
{
//全局保存当前路线信息
GameManger.MapData = result;
//初始化map
var point = result.List[0].Point;
_map.Initialize(new Utils.Vector2d(point[0], point[1]), _map.AbsoluteZoom);
//初始化人物 TODO
}
}
}
}

View File

@ -3,18 +3,21 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerFactory : MonoBehaviour
namespace Assets.Scenes.Ride.Scripts
{
[SerializeField]
public GameObject _playerPrefab;
// Start is called before the first frame update
void Start()
public class PlayerFactory : MonoBehaviour
{
if (_playerPrefab != null)
[SerializeField]
public GameObject _playerPrefab;
// Start is called before the first frame update
void Start()
{
for (int i = 0; i < 100; i++)
if (_playerPrefab != null)
{
Instantiate(_playerPrefab, new Vector3(i, 1, 0), Quaternion.identity);
for (int i = 0; i < 100; i++)
{
Instantiate(_playerPrefab, new Vector3(i, 1, 0), Quaternion.identity);
}
}
}
}

View File

@ -133,6 +133,7 @@ GameObject:
- component: {fileID: 159634027}
- component: {fileID: 159634025}
- component: {fileID: 159634026}
- component: {fileID: 159634028}
m_Layer: 0
m_Name: Map
m_TagString: Untagged
@ -544,6 +545,23 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &159634028
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 159634024}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5cdee5ecffe869b40913ef4eb53fd993, type: 3}
m_Name:
m_EditorClassIdentifier:
_map: {fileID: 0}
MeshModifiers:
- {fileID: 11400000, guid: 7c518d85eb2ed0440ac158307895eec4, type: 2}
_material: {fileID: 0}
UpdateFrequency: 2
--- !u!1 &234181014
GameObject:
m_ObjectHideFlags: 0
@ -729,54 +747,6 @@ Light:
m_UseBoundingSphereOverride: 0
m_ShadowRadius: 0
m_ShadowAngle: 0
--- !u!1 &1468894434
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1468894435}
- component: {fileID: 1468894436}
m_Layer: 0
m_Name: Route
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1468894435
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1468894434}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 1.0251179, y: 4.418831, z: -2.967451}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1468894436
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1468894434}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5cdee5ecffe869b40913ef4eb53fd993, type: 3}
m_Name:
m_EditorClassIdentifier:
_map: {fileID: 159634025}
MeshModifiers:
- {fileID: 11400000, guid: 7c518d85eb2ed0440ac158307895eec4, type: 2}
_material: {fileID: 2100000, guid: bdad3ccf6401a414188827874d8999aa, type: 2}
UpdateFrequency: 2
--- !u!1 &1746432969
GameObject:
m_ObjectHideFlags: 0

View File

@ -0,0 +1,40 @@
using GeoJSON.Net.Geometry;
using Mapbox.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using TurfCS;
using UnityEngine.Networking;
using Assets.Scripts.Apis.Models;
namespace Assets.Scenes.Ride.Scripts
{
public static class GameManger
{
public static double totaldistance;
public static List<Vector2d> vl;
public static int routeId;
public static bool IsStart = true;
public static MapDataModel MapData;
//记录当前游戏一些全局数据
public static Vector2d Along(double distance)
{
if (MapData != null)
{
var list = MapData.List.Select(p => new GeoJSON.Net.Geometry.GeographicPosition(p.Point[0], p.Point[1]));
LineString lineString = new LineString(list);
var pt1 = Turf.Along(lineString, distance);
var ll = ((GeographicPosition)((GeoJSON.Net.Geometry.Point)pt1.Geometry).Coordinates);
return new Vector2d(ll.Latitude, ll.Longitude);
}
return new Vector2d(0, 0);
}
}
}

View File

@ -0,0 +1,109 @@
using System;
using System.Globalization;
namespace Assets.Scenes.Ride.Scripts
{
public class Helper
{
/// <summary>
/// 计算速度
/// 参考 http://bikecalculator.com/index.html
/// </summary>
/// <param name="elevationv">海拔</param>
/// <param name="gradev">坡度%</param>
/// <param name="powerv">功率</param>
/// <param name="rweightv">体重</param>
/// <param name="bweightv">自行车重量</param>
/// <returns>km/h</returns>
public static double CalculateSpeed(double elevationv, double gradev, double powerv, double rweightv, double bweightv)
{
if (powerv < 0)
{
return 0;
}
if (gradev < -10)
{
gradev = -10;
}
var tireValues = new double[] { 0.005, 0.004, 0.012 };
var aeroValues = new double[] { 0.388, 0.445, 0.420, 0.300, 0.233, 0.200 };
var units = 0;
//温度
var temperaturev = 25;
//海拔
//var elevationv = 100;
//坡度
//var gradev = 6 * 0.01 * -1;
//风转换成m/s
var headwindv = 0 / 3.6;
var frontalArea = aeroValues[0];
var transv = 0.95; // no one knows what this is, so why bother presenting a choice?
var rollingRes = tireValues[0];
//应该是空气密度
var density = (1.293 - 0.00426 * temperaturev) * Math.Exp(-elevationv / 7000.0);
//全空气阻力参数
var a2 = 0.5 * frontalArea * density; // full air resistance parameter
var twt = 9.8 * (rweightv + bweightv); // total weight in newtons
var tres = twt * (gradev * 0.01 + rollingRes); // gravity and rolling resistance
//var powerv = 195;
var v = Newton(a2, headwindv, tres, transv, powerv) * 3.6;// convert to km/h
//Trace.WriteLine($"速度为:{ v }");
var t1 = makeDecimal2(v * (units == 1 ? 1.609 : 1.0));
return double.Parse(t1, CultureInfo.InvariantCulture);
}
private static double Newton(double aero, double hw, double tr, double tran, double p)
{
var vel = 20D;
var MAX = 10;
var TOL = 0.05;
for (int i = 0; i < MAX; i++)
{
var tv = vel + hw;
var aeroEff = (tv > 0.0) ? aero : -aero; // wind in face, must reverse effect
var f = vel * (aeroEff * tv * tv + tr) - tran * p; // the function
var fp = aeroEff * (3.0 * vel + hw) * tv + tr; // the derivative
var vNew = vel - f / fp;
if (Math.Abs(vNew - vel) < TOL)
{
return vNew; // success
}
vel = vNew;
}
return 0.0;
}
private static string makeDecimal2(double value)
{
if (value != 0)
{
var x = Math.Round(value * 100);
int d;
if (x < 100)
{
d = 0;
}
else
{
d = Convert.ToInt32(Math.Floor(x / 100), CultureInfo.InvariantCulture);
};
var c = x % 100;
var g = (c >= 10) ? "" : "0";
return "" + d + "." + g + c;
}
return value.ToString(CultureInfo.InvariantCulture);
}
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 4e343b676bd532340a23969ea51e7e94
guid: ba2d01db4eb55e54b830f8c931fc9cae
MonoImporter:
externalObjects: {}
serializedVersion: 2

File diff suppressed because one or more lines are too long

View File

@ -1,13 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mapbox.Unity.Map;
using Mapbox.Utils;
using Mapbox.Unity.Utilities;
using System.Linq;
using Assets.Scripts.Apis.Models;
using static Assets.Scripts.Apis.Models.MapDataModel;
namespace Ride.Scripts
namespace Assets.Scenes.Ride.Scripts
{
public class PlayerController : MonoBehaviour
{
@ -18,126 +16,105 @@ namespace Ride.Scripts
float characterSpeed;
[SerializeField]
Animator characterAnimator;
//[Header("References")]
//[SerializeField]
//AstronautDirections directions;
//[SerializeField]
//Transform startPoint;
//[SerializeField]
//Transform endPoint;
[SerializeField]
AbstractMap map;
[SerializeField]
GameObject rayPlane;
//[SerializeField]
//Transform _movementEndPoint;
[SerializeField]
LayerMask layerMask;
Ray ray;
RaycastHit hit;
LayerMask raycastPlane;
float clicktime;
bool moving;
bool characterDisabled;
private List<Vector2d> rideList;
void Start()
{
characterAnimator = GetComponentInChildren<Animator>();
//初始化玩家坐标
currentlatlong = map.WorldToGeoPosition(transform.localPosition);
nextlatlong = currentlatlong;
totalDistance = 0;
//StartCoroutine("Timer");
//if (!Application.isEditor)
//{
// this.enabled = false;
// return;
//}
}
//gameStatus idle start pause finish
Vector3 nextPos;
Vector3 prePos;
private Vector2d currentlatlong; //当前坐标
private Vector2d nextlatlong; //下一个点的坐标
private double totalDistance;
private int ticks;//秒数
public float timer = 1.0f;//计时器
void Start()
{
characterAnimator = GetComponentInChildren<Animator>();
InitializePlayer();
}
//初始化人物
void InitializePlayer()
{
//获取玩家初始距离
currentlatlong = map.WorldToGeoPosition(transform.localPosition);
nextlatlong = currentlatlong;
totalDistance = 0;
ticks = 0;
}
void Update()
{
if (characterDisabled)
return;
//CamControl();
bool click = false;
if (Input.GetMouseButtonDown(0))
timer -= Time.deltaTime;
if (timer <= 0 && GameManger.IsStart)//定时器
{
clicktime = Time.time;
}
if (Input.GetMouseButtonUp(0))
{
if (Time.time - clicktime > 0.15f)
{
click = true;
}
}
//开始骑行
//if (click)
{
StartCoroutine("Walk");
ticks++;
Riding();
timer = 1.0f;
}
}
IEnumerator Walk()
//计算当前玩家高度和坡度
Item find(double totalDistance)
{
yield return new WaitForSeconds(1f);
//读取当前速度以及骑行距离
double distance = MockData.CalculateSpeed(Random.Range(100,300), 10, 100, 65, 7) / 3600;
if (totalDistance + distance <= MockData.totaldistance)
{
totalDistance += distance;
//计算下一个点的位置
nextlatlong = MockData.Along(totalDistance);
double sumDistance = 0;
MapDataModel mapDataModel = GameManger.MapData;
foreach (var item in mapDataModel.List)
{
sumDistance += item.Distance;
if (totalDistance < sumDistance)
{
return item;
}
}
return mapDataModel.List[0];
}
void Riding()
{
//yield return new WaitForSeconds(1.0f);
////读取当前速度以及骑行距离
MapDataModel mapDataModel = GameManger.MapData;
Item nextRange = find(totalDistance);
double elevation = nextRange.Elevation;
double gradev = nextRange.Grade;
double power = Random.Range(700, 2000);//TODO
double distance = Helper.CalculateSpeed(elevation, gradev, power, 65, 7) / 3600;
characterAnimator.SetBool("IsRide", false);
totalDistance += distance;
if (totalDistance <= mapDataModel.TotalDistance)
{
if (distance > 0)
{
CamControl();
var v = GameManger.Along(totalDistance);
StartCoroutine(CamControl());
//设定动画状态
characterAnimator.SetBool("IsRide", true);
nextPos = map.GeoToWorldPosition(nextlatlong);
nextPos = map.GeoToWorldPosition(v);
prePos = transform.localPosition;
//转向
StartCoroutine(LookAtNextPos());
//让人物移动 TODO 可以继续分点增加动画的流畅度
transform.localPosition = nextPos;
//StartCoroutine(MoveTo());
//移动
StartCoroutine(MoveTo());
}
else
{
moving = false;
characterAnimator.SetBool("IsRide", false);
}
}
else
{
moving = false;
characterAnimator.SetBool("IsRide", false);
}
}
#region Character : Movement
List<Vector3> futurePositions = new List<Vector3>();
bool interruption;
Vector3 nextPos;
Vector3 prevPos;
#endregion
IEnumerator MoveTo()
{
//让人物移动分点增加动画的流畅度
float t = 0;
while (t < 1)
{
t += Time.deltaTime;
Vector3 v = Vector3.Lerp(transform.localPosition, nextPos, t);
transform.localPosition = v;
yield return null;
}
}
#region Character : Rotation
IEnumerator LookAtNextPos()
{
Quaternion neededRotation = Quaternion.LookRotation(character.transform.position - nextPos);
Quaternion neededRotation = Quaternion.LookRotation(transform.localPosition - nextPos);
Quaternion thisRotation = character.transform.localRotation;
float t = 0;
@ -158,14 +135,20 @@ namespace Ride.Scripts
Vector3 previousPos = Vector3.zero;
Vector3 deltaPos = Vector3.zero;
void CamControl()
IEnumerator CamControl()
{
if (cam != null)
{
deltaPos = transform.position - previousPos;
//deltaPos.y = 0;
cam.transform.position = Vector3.Lerp(cam.transform.position, cam.transform.position + deltaPos, Time.time);
previousPos = transform.position;
float t = 0;
while (t < 1.0f)
{
t += Time.deltaTime / 0.5f;
deltaPos = transform.position - previousPos;
//deltaPos.y = 0;
cam.transform.position = Vector3.Lerp(cam.transform.position, cam.transform.position + deltaPos, t);
previousPos = transform.position;
yield return null;
}
}
}
#endregion

View File

@ -1,18 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerData : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 0e420a61df06f8b428bbd6b9f11190a4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,20 +1,15 @@
namespace Mapbox.Unity.MeshGeneration.Factories
{
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using Mapbox.Unity.Map;
using Data;
using Modifiers;
using Mapbox.Utils;
using Mapbox.Unity.Utilities;
using System.Collections;
using Newtonsoft.Json;
using System.IO;
using Newtonsoft.Json.Linq;
using System;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using Mapbox.Unity.Map;
using Mapbox.Utils;
using System.Collections;
using Mapbox.Unity.MeshGeneration.Modifiers;
using Mapbox.Unity.MeshGeneration.Data;
public class RouteController : MonoBehaviour
namespace Assets.Scenes.Ride.Scripts
{
public class RouteController : MonoBehaviour
{
[SerializeField]
AbstractMap _map;
@ -48,7 +43,7 @@
{
modifier.Initialize();
}
StartCoroutine(QueryTimer());
}
@ -71,22 +66,18 @@
var meshData = new MeshData();
var dat = new List<Vector3>();
List<Vector2d> points = MockData.vl;
if (points == null)
var mapData = GameManger.MapData;
if (mapData != null)
{
points = MockData.GetRideList();
}
{
foreach (var point in points)
foreach (var mapDataItem in mapData.List)
{
Vector3 item = _map.GeoToWorldPosition(point);//point.x, point.y, _map.CenterMercator, _map.WorldRelativeScale);
//item.y = (float)(_map.QueryElevationInUnityUnitsAt(point))+0.3f;
item.y += 0.5f;
var point = mapDataItem.Point;
Vector3 item = _map.GeoToWorldPosition(new Vector2d(point[0], point[1]));
item.y += 0.4f;
dat.Add(item);
}
var feat = new VectorFeatureUnity();
feat.Points.Add(dat);
foreach (MeshModifier mod in MeshModifiers.Where(x => x.Active))
{
mod.Run(feat, meshData, _map.WorldRelativeScale * 8);
@ -102,7 +93,7 @@
{
_directionsGO.Destroy();
}
_directionsGO = new GameObject("direction waypoint " + " entity");
_directionsGO = new GameObject("MapRoute");
var mesh = _directionsGO.AddComponent<MeshFilter>().mesh;
mesh.subMeshCount = data.Triangles.Count;

View File

@ -1,8 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RouteData
{
}

View File

@ -1,5 +1,6 @@
{
"dependencies": {
"com.unity.cinemachine": "2.6.3",
"com.coffee.softmask-for-ugui": "https://github.com/mob-sakai/SoftMaskForUGUI.git",
"com.unity.2d.sprite": "1.0.0",
"com.unity.collab-proxy": "1.2.16",

View File

@ -1,5 +1,11 @@
{
"dependencies": {
"com.unity.cinemachine": {
"version": "2.6.3",
"depth": 0,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.cn"
"com.coffee.softmask-for-ugui": {
"version": "https://github.com/mob-sakai/SoftMaskForUGUI.git",
"depth": 0,