-
Notifications
You must be signed in to change notification settings - Fork 0
/
AR.txt
262 lines (222 loc) · 6.43 KB
/
AR.txt
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//EXP1 Birds Eye View
1. CameraController.cs
using UnityEngine;
public class CameraController MonoBehaviour
{
public float moveSpeed = 5f;
void Update()
{
Get input for camera movement
float horizontal = Input.GetAxis(Horizontal);
float vertical = Input.GetAxis(Vertical);
Calculate movement direction
Vector3 movement = new Vector3(horizontal, 0, vertical).normalized;
Move the player in the camera's forward direction
transform.Translate(movement moveSpeed Time.deltaTime);
}
}
//EXP2 Linear Transform
1. TransformExperiment.cs
using UnityEngine;
public class TransformExperiment : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
// Initial position, rotation, and scale of the cube
Vector3 initialPosition = new Vector3(0f, 1f, 0f);
Quaternion initialRotation = Quaternion.identity;
Vector3 initialScale = new Vector3(1f, 1f, 1f);
// Apply the initial transformations to the cube
transform.position = initialPosition;
transform.rotation = initialRotation;
transform.localScale = initialScale;
// Perform linear transformations
PerformLinearTransformations();
}
void PerformLinearTransformations()
{
// Translate the cube along the x-axis
transform.Translate(Vector3.right * 2f, Space.World);
// Rotate the cube by 45 degrees around the y-axis
transform.Rotate(Vector3.up, 45f, Space.World);
// Scale the cube uniformly by 2
transform.localScale = new Vector3(2f, 2f, 2f);
}
}
//EXP3 2D Rigid Body
1. CustomTransformation.cs
using UnityEngine;
public class ObjectTransform : MonoBehaviour
{
public float moveSpeed = 5f;
public float scaleSpeed = 0.1f;
public float rotationSpeed = 180f;
void Update()
{
// Translation (Movement)
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 translation = new Vector3(horizontalInput, verticalInput, 0f) * moveSpeed * Time.deltaTime;
transform.Translate(translation);
// Scaling
if (Input.GetKey(KeyCode.T)) // Press 'T' to scale up
{
ScaleObject(1f + scaleSpeed * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.G)) // Press 'G' to scale down
{
ScaleObject(1f - scaleSpeed * Time.deltaTime);
}
// Rotation
if (Input.GetKey(KeyCode.U)) // Press 'U' to rotate counterclockwise
{
RotateObject(-rotationSpeed * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.J)) // Press 'J' to rotate clockwise
{
RotateObject(rotationSpeed * Time.deltaTime);
}
}
void ScaleObject(float scaleFactor)
{
transform.localScale *= scaleFactor;
}
void RotateObject(float rotationAngle)
{
transform.Rotate(Vector3.forward, rotationAngle);
}
}
2. Rotation.cs
using UnityEngine;
public class RotationExample : MonoBehaviour
{
public float rotationSpeed = 180f;
void Update()
{
// Rotation
float rotationInput = Input.GetAxis("Horizontal"); // Get rotation input from horizontal axis (A and D keys)
RotateObject(rotationInput);
}
void RotateObject(float rotationInput)
{
float rotationAmount = rotationInput * rotationSpeed * Time.deltaTime;
transform.Rotate(Vector3.forward * rotationAmount);
}
}
3. Scaling.cs
using UnityEngine;
public class ScalingExample : MonoBehaviour
{
public float scaleSpeed = 1.2f;
void Update()
{
// Scale up with 'W' key
if (Input.GetKey(KeyCode.W))
{
ScaleObject(1);
}
// Scale down with 'S' key
if (Input.GetKey(KeyCode.S))
{
ScaleObject(-1);
}
}
void ScaleObject(int direction)
{
float scaleFactor = Mathf.Pow(scaleSpeed, direction * Time.deltaTime);
transform.localScale *= scaleFactor;
}
}
4. Translation.cs
using UnityEngine;
public class TranslationExample : MonoBehaviour
{
public float moveSpeed = 5f;
void Update()
{
// Move horizontally and vertically based on user input
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
// Translate the object
Vector2 translation = new Vector2(horizontalInput, verticalInput) *
moveSpeed * Time.deltaTime;
transform.Translate(translation);
}
}
//EXP4 3D Rotations
1. RotationExperiment.cs
using UnityEngine;
public class RotationExperiment : MonoBehaviour
{
void Update()
{
// Rotate the cube using quaternion
transform.rotation *= Quaternion.Euler(0.5f, 1.0f, 0.2f);
// Convert quaternion to Euler angles
// Vector3 euler = transform.rotation.eulerAngles;
// Debug.Log("Euler Angles: " + euler);
// // Convert Euler angles back to quaternion
// Quaternion newRotation = Quaternion.Euler(euler);
// Debug.Log("Quaternion: " + newRotation);
}
}
//EXP5 Local vs Global Coordinate
1. GlobalTransformExperiment.cs
using UnityEngine;
public class GlobalTransformExperiment : MonoBehaviour
{
void Update()
{
// Move the cube globally
transform.Translate(Vector3.forward *
Time.deltaTime);
} }
2. HomogeneousTransformExperiment.cs
using UnityEngine;
public class HomogeneousTransformExperiment : MonoBehaviour
{
void Update()
{
// Move the parent GameObject using homogeneous transformation
transform.Translate(Vector3.up * Time.deltaTime);
}
}
3. LocalTransformExperiment.cs
using UnityEngine;
public class LocalTransformExperiment : MonoBehaviour
{
void Update()
{
// Move the cube locally
transform.Translate(Vector3.right *
Time.deltaTime); }
}
//EXP6
1. ViewingTransformExperiment.cs
using UnityEngine;
public class ViewingTransformExperiment :
MonoBehaviour
{
void Update()
{
// Example: Translate the camera forward
transform.Translate(Vector3.forward*
Time.deltaTime);
}
}
2. ViewingTransformExperiment2.cs
using UnityEngine;
public class ViewingTransformExperiment2 :
MonoBehaviour
{
void Update()
{
// Example: Convert mouse position to world space
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = 10; // Set a distance from the camera
Vector3 worldPosition =
Camera.main.ScreenToWorldPoint(mousePosition);
transform.position = worldPosition;
}
}