-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplayingTrajectoryPath.cs
More file actions
183 lines (161 loc) · 6.76 KB
/
DisplayingTrajectoryPath.cs
File metadata and controls
183 lines (161 loc) · 6.76 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
public Transform someObject; //object that moves along parabola.
float objectT = 0; //timer for that object
public Transform Ta, Tb; //transforms that mark the start and end
public float h; //desired parabola height
Vector3 a, b; //Vector positions for start and end
void Update () {
if ( Ta && Tb ) {
a = Ta.position; //Get vectors from the transforms
b = Tb.position;
if ( someObject ) {
//Shows how to animate something following a parabola
objectT = Time.time % 1; //completes the parabola trip in one second
someObject.position = SampleParabola( a, b, h, objectT );
}
}
}
void OnDrawGizmos () {
//Draw the parabola by sample a few times
Gizmos.color = Color.red;
Gizmos.DrawLine( a, b );
float count = 20;
Vector3 lastP = a;
for ( float i = 0; i < count + 1; i++ ) {
Vector3 p = SampleParabola( a, b, h, i / count );
Gizmos.color = i % 2 == 0 ? Color.blue : Color.green;
Gizmos.DrawLine( lastP, p );
lastP = p;
}
}
#region Parabola sampling function
/// <summary>
/// Get position from a parabola defined by start and end, height, and time
/// </summary>
/// <param name='start'>
/// The start point of the parabola
/// </param>
/// <param name='end'>
/// The end point of the parabola
/// </param>
/// <param name='height'>
/// The height of the parabola at its maximum
/// </param>
/// <param name='t'>
/// Normalized time (0->1)
/// </param>S
Vector3 SampleParabola ( Vector3 start, Vector3 end, float height, float t ) {
float parabolicT = t * 2 - 1;
if ( Mathf.Abs( start.y - end.y ) < 0.1f ) {
//start and end are roughly level, pretend they are - simpler solution with less steps
Vector3 travelDirection = end - start;
Vector3 result = start + t * travelDirection;
result.y += ( -parabolicT * parabolicT + 1 ) * height;
return result;
} else {
//start and end are not level, gets more complicated
Vector3 travelDirection = end - start;
Vector3 levelDirecteion = end - new Vector3( start.x, end.y, start.z );
Vector3 right = Vector3.Cross( travelDirection, levelDirecteion );
Vector3 up = Vector3.Cross( right, travelDirection );
if ( end.y > start.y ) up = -up;
Vector3 result = start + t * travelDirection;
result += ( ( -parabolicT * parabolicT + 1 ) * height ) * up.normalized;
return result;
}
}
/* OPTIMIZE */
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Test5 : MonoBehaviour
{
public Transform _moveableObject; // Objet to move on path
public GameObject _trajectoryElement; // GO to draw in path of trajectory. GO having signle white arrow attached on it
public Transform _startPosition; // Start position from where trajectory should start and mouse of finger position is the destination
public float _height; //desired parabola height
public int _numberOfElements = 10; // Number of elements should draw in path of parabola
Vector3 a, b; //Vector positions for start and end
List<GameObject> _trajectoryElementsContainer = new List<GameObject> ();
void Start ()
{
// For now I have instantiated all the objects will be drawn on the path of parabola. Then did handle them later.
// Please modify it according to your needs
for (int i = 0; i < _numberOfElements; i++)
_trajectoryElementsContainer.Add (Instantiate (_trajectoryElement) as GameObject);
}
void Update ()
{
if (Input.GetMouseButton (0) && _startPosition) {
a = _startPosition.position; //Get vectors from the transforms
a = new Vector3 (a.x, a.y, 0);
b = Camera.main.ScreenToWorldPoint (Input.mousePosition);
b = new Vector3 (b.x, b.y, 0);
float distributionTime = 0;
for (float i = 1; i <= _numberOfElements; i++) {
distributionTime++;
Vector3 currentPosition = SampleParabola (a, b, _height, i / (float)_numberOfElements);
_trajectoryElementsContainer [(int)i - 1].transform.position = new Vector3 (currentPosition.x, currentPosition.y, 0);
Vector3 nextPosition = SampleParabola (a, b, _height, (i + 1) / (float)_numberOfElements);
float angleInR = Mathf.Atan2 ((nextPosition.y - currentPosition.y), (nextPosition.x - currentPosition.x));
_trajectoryElementsContainer [(int)i - 1].transform.eulerAngles = new Vector3 (0, 0, (Mathf.Rad2Deg * angleInR) - 90);
}
if (_moveableObject) {
//Shows how to animate something following a parabola
_moveableObject.position = SampleParabola (a, b, _height, Time.time % 1);
}
}
}
void OnDrawGizmos ()
{
//Draw the parabola by sample a few times
Gizmos.color = Color.red;
Gizmos.DrawLine (a, b);
float count = 20;
Vector3 lastP = a;
for (float i = 0; i < count + 1; i++) {
Vector3 p = SampleParabola (a, b, _height, i / count);
Gizmos.color = i % 2 == 0 ? Color.blue : Color.green;
Gizmos.DrawLine (lastP, p);
lastP = p;
}
}
#region Parabola sampling function
/// <summary>
/// Get position from a parabola defined by start and end, height, and time
/// </summary>
/// <param name='start'>
/// The start point of the parabola
/// </param>
/// <param name='end'>
/// The end point of the parabola
/// </param>
/// <param name='height'>
/// The height of the parabola at its maximum
/// </param>
/// <param name='t'>
/// Normalized time (0->1)
/// </param>S
Vector3 SampleParabola (Vector3 start, Vector3 end, float height, float t)
{
float parabolicT = t * 2 - 1;
if (Mathf.Abs (start.y - end.y) < 0.1f) {
//start and end are roughly level, pretend they are - simpler solution with less steps
Vector3 travelDirection = end - start;
Vector3 result = start + t * travelDirection;
result.y += (-parabolicT * parabolicT + 1) * height;
return result;
} else {
//start and end are not level, gets more complicated
Vector3 travelDirection = end - start;
Vector3 levelDirecteion = end - new Vector3 (start.x, end.y, start.z);
Vector3 right = Vector3.Cross (travelDirection, levelDirecteion);
Vector3 up = Vector3.Cross (right, travelDirection);
if (end.y > start.y)
up = -up;
Vector3 result = start + t * travelDirection;
result += ((-parabolicT * parabolicT + 1) * height) * up.normalized;
return result;
}
}
#endregion
}