arskrald/AR-1/Assets/GPSScript.cs

76 lines
2.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GPSScript : MonoBehaviour
{
private Transform earth;
private Transform nose;
private Transform shuttle;
private Transform positionTest;
private Matrix4x4 earthLocalToWorldInverse;
private string printString;
private Vector3 posOnEarth = new Vector3();
// Start is called before the first frame update
void Start()
{
earth = GameObject.Find("EarthTarget").transform;
nose = GameObject.Find("NoseQuad").transform;
shuttle = GameObject.Find("ShuttleTarget").transform;
positionTest = GameObject.Find("PositionTest").transform;
}
// Update is called once per frame
void Update()
{
printString = "";
/*
Possible solution. Under the assumption we hold the
camera steadily, the earth stuff be done in the
Start() function.
This should be correct, according to slides.
*/
var shuttleLocalToWorld = shuttle.localToWorldMatrix;
var earthWorldToLocal = Matrix4x4.Inverse(Matrix4x4.TRS(earth.position, earth.rotation, Vector3.one));
var transMatrix = earthWorldToLocal * shuttleLocalToWorld;
posOnEarth = transMatrix.MultiplyPoint3x4(nose.localPosition);
printString += posOnEarth.ToString("F4") + "\n";
compareToNose(posOnEarth);
}
void compareToNose(Vector3 nosePosition) {
print($"LossyScale: {earth.lossyScale}");
//var radius = new Vector2(earth.lossyScale.x, earth.lossyScale.z).magnitude / 2;
var radius = earth.lossyScale.magnitude / 4;
//var radius = (new Vector2(earth.lossyScale.x, earth.lossyScale.z)).magnitude;
print($"Radius: {radius}");
var d = new Vector2(nosePosition.x, nosePosition.z).magnitude;
print($"D: {d}");
if (d <= radius && nosePosition.y <= 4) {
print("Within");
if (nosePosition.z >= 0) {
printString += "North";
} else {
printString += "South";
}
} else {
printString += "Outside";
}
}
void OnGUI()
{
GUI.color = Color.white;
GUI.Box(new Rect(10, 30, 200, 40), printString);
}
}