-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMomentumScript.cs
104 lines (82 loc) · 3.13 KB
/
MomentumScript.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//Takes in settings from UI and the initial velocity set by vectors attached to each puck. Then adds the appropriate momentum
// to each puck and traces the motion. Output properties momentum, velocity etc of each puck.
public class MomentumScript : MonoBehaviour {
public InputField mass1; //the masses of each puck as set by the user. Defaults are already set to 1 unit
public InputField mass2;
public Transform puck1; //the puck objects themselves
public Transform puck2;
private Rigidbody rb_1; //the rigidbodies attached to the pucks
private Rigidbody rb_2;
public Transform vectorHead1;
public Transform vectorHead2;
public Toggle twod; //if 2d is selected then allow motion in 2d
// Use this for initialization
void Start () {
rb_1 = puck1.GetComponentInChildren<Rigidbody>();
//rb_1 = puck1.GetComponent<Rigidbody>();
rb_2 = puck2.GetComponentInChildren<Rigidbody>();
//set the mass values to the default rigidbody start masses
mass1.text = rb_1.mass.ToString();
mass2.text = rb_2.mass.ToString();
//add listeners for mass changes
mass1.onEndEdit.AddListener(delegate { UpdateMass(); });
mass2.onEndEdit.AddListener(delegate { UpdateMass(); });
//load the trail particles
}
// Update is called once per frame
void FixedUpdate () {
}
//actually gives an object a specific velocity rather than momentum
public void AddMomentum()
{
Vector3 vel = GetVelocity(puck1, vectorHead1);
Vector3 vel2 = GetVelocity(puck2, vectorHead2);
rb_1.velocity = vel;
rb_2.velocity = vel2;
RemoveVectors(); //remove the initialisation vectors and their labels
}
//All vectors and labels are given the tag "vectorArrow" allowing them to be easily removed from the scene when pucks
// are launched
private void RemoveVectors()
{
GameObject[] vectors = GameObject.FindGameObjectsWithTag("vectorArrow");
foreach (GameObject vector in vectors)
{
Destroy(vector);
}
}
//returns the velocity defined by the speed and direction of the vector associated with a puck
private Vector3 GetVelocity(Transform puck, Transform vectorHead)
{
float vel_x = vectorHead.position.x - puck.position.x;
float vel_z = vectorHead.position.z - puck.position.z;
Vector3 vel;
if (twod.isOn)
{
vel = new Vector3(vel_x, 0, vel_z);
}
else
{
vel = new Vector3(vel_x, 0, 0);
}
return vel;
}
//when the mass Input fields are edited then run this update function
private void UpdateMass()
{
float mass1_value;
float mass2_value;
if(float.TryParse(mass1.text, out mass1_value))
{
rb_1.mass = mass1_value;
}
if (float.TryParse(mass2.text, out mass2_value))
{
rb_2.mass = mass2_value;
}
}
}