Commit a0f15e00 authored by 18김민수's avatar 18김민수

Added ProperTime

parent b2015dec
......@@ -73,5 +73,5 @@ Material:
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _Color: {r: 0, g: 0, b: 0, a: 1}
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
This diff is collapsed.
......@@ -4,9 +4,9 @@ using UnityEngine;
public static class Constants
{
public static double C => 40; // speed of light.
public static double c => 40; // speed of light.
public static double Gamma(double v)
{
return 1 / Mathf.Sqrt((1 - (float)((v / C) * (v / C))));
return 1 / Mathf.Sqrt((1 - (float)((v / c) * (v / c))));
}
}
......@@ -5,10 +5,7 @@ using UnityEngine;
public class FlatLandObject : MonoBehaviour
{
public Vector2 speedVector = new Vector2(0, 0);
public double gameSpeed = 20;
public double properTime = 0;
public virtual void Update()
{
properTime += Time.deltaTime;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathRenderer : MonoBehaviour
{
LineRenderer line;
// Start is called before the first frame update
void Start()
{
Vector3 a = new Vector2(-30, -30);
Vector3 b = new Vector2(30, 30);
Vector3 c = new Vector2(-30, 30);
line = GetComponent<LineRenderer>();
Vector3[] lst = new Vector3[] { a, b, c };
line.positionCount = lst.Length;
line.SetPositions(lst);
}
// Update is called once per frame
void Update()
{
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Square : FlatLandObject
{
public LineRenderer pathRenderer; // About drawing paths.
public List<Vector2> pathList = new List<Vector2>();
public UIManager uiManager;
private Vector2 _currentPathEnd;
......@@ -16,10 +18,11 @@ public class Square : FlatLandObject
}
// Update is called once per frame
public override void Update()
public void Update()
{
if(Input.GetMouseButtonDown(1)) // If right mouse button is clicked
{
Debug.Log(getNthPath(0));
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit)) // If the click was on the background
{
......@@ -43,16 +46,18 @@ public class Square : FlatLandObject
}
public void CreatePath(Vector3 point) // Creates the fitst path.
public void CreatePath(Vector3 point) // Creates the fitst path, and updates Proper Time UI.
{
pathRenderer.positionCount = 2;
pathRenderer.SetPositions(new Vector3[] { transform.position, point });
uiManager.UpdateUI();
}
public void AddPath(Vector3 point) // Adds new path to current path.
public void AddPath(Vector3 point) // Adds new path to current path, and updates Proper Time UI.
{
pathRenderer.positionCount++;
pathRenderer.SetPosition(pathRenderer.positionCount - 1, point);
uiManager.UpdateUI();
}
public void Move()
......@@ -79,4 +84,33 @@ public class Square : FlatLandObject
pathRenderer.positionCount = 2;
pathRenderer.SetPositions(new Vector3[] { Vector3.zero, Vector3.zero });
}
public Vector2 getNthPath(int n) // returns movement vector stored in path renderer by index.
{
if (n >= pathRenderer.positionCount)
throw new InvalidOperationException(n + "th path is not stored.");
return pathRenderer.GetPosition(n + 1) - pathRenderer.GetPosition(n);
}
public double MovingTime(Vector2 v) // How long it takes to move given vector with current speed.
{
return v.sqrMagnitude / gameSpeed;
}
public double CalculateEntireMovingTime() // How long it takes to move current path all the way.
{
double result = 0;
for (int i = 0; i < pathRenderer.positionCount - 1; i++)
result += MovingTime(getNthPath(i));
return result / gameSpeed;
}
public void ModifySpeed(float d)
{
if (gameSpeed + d <= Constants.c && gameSpeed + d >= 0)
gameSpeed += d;
uiManager.UpdateUI();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIManager : MonoBehaviour
{
public Text properTime;
public Text currentSpeed;
public Square square;
public void UpdateUI()
{
string newPropertime, prevPropertime;
currentSpeed.text = "Current Speed : " + (square.gameSpeed / Constants.c).ToString("F2") + "c";
prevPropertime = square.CalculateEntireMovingTime().ToString();
newPropertime = (square.CalculateEntireMovingTime() / Constants.Gamma(square.gameSpeed)).ToString();
properTime.text = "New Proper Time : " + newPropertime + "\nPrev Proper Time : " + prevPropertime;
}
}
fileFormatVersion: 2
guid: 2386eefa66792304fa006e091f12c046
guid: 2c85c5f34b599bd47bda7810cd6b95a6
MonoImporter:
externalObjects: {}
serializedVersion: 2
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment