Repository navigation
Expand file tree
/
Copy pathGrabLeft.cs
More file actions
174 lines (140 loc) · 4.72 KB
/
Copy pathGrabLeft.cs
File metadata and controls
174 lines (140 loc) · 4.72 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrabLeft : OVRGrabber
{
public bool isGrabbing;
public bool insideObject;
private ThrowGrabbable throwGrabbable;
protected override void Start()
{
Debug.Log("start!");
base.Start();
insideObject = false;
isGrabbing = false;
}
public override void Update()
{
base.Update();
//object 닿지 않을 때
if (!(insideObject))
return;
}
public void InsideObjectTurnOn()
{
insideObject = true;
}
public void InsideObjectTurnOff()
{
m_grabbedObj = null;
insideObject = false;
isGrabbing = false;
}
public void GrabStart()
{
if (!(insideObject) || (isGrabbing))
{
return;
}
//Debug.Log("Grab Grabbing");
//canGrabbing = true;
//print("m_grabbedObj: " + m_grabbedObj);
//print("m_grabCandidates.cout: " + m_grabCandidates.Count);
List<OVRGrabbable> deleteCandidates = new List<OVRGrabbable>();
foreach (KeyValuePair<OVRGrabbable, int> grabCandidate in m_grabCandidates)
{
//print("grabCandidate: "+grabCandidate.Key); // log기록
if (grabCandidate.Key == null)
{
OVRGrabbable grabbable = grabCandidate.Key;
deleteCandidates.Add(grabbable);
//m_grabCandidates.Remove(grabbable);
}
}
foreach(OVRGrabbable deleteCandidate in deleteCandidates)
{
m_grabCandidates.Remove(deleteCandidate);
}
//print("canGrabbing: " + canGrabbing);
if (m_grabbedObj == null && m_grabCandidates.Count > 0 && !isGrabbing)
{
Debug.Log("grabbing");
GrabBegin();
isGrabbing = true;
}
}
public void GrabFinish()
{
if (!(insideObject) || !(isGrabbing))
return;
//Debug.Log("Grab Not Grabbing");
//canGrabbing = false;
//print("m_grabbedObj: " + m_grabbedObj);
//print("canGrabbing: " + canGrabbing);
if (m_grabbedObj != null && isGrabbing)
{
throwGrabbable = m_grabbedObj.gameObject.GetComponent<ThrowGrabbable>();
SetPlayerIgnoreCollision(m_grabbedObj.gameObject, false);
Debug.Log("not grabbing");
isGrabbing = false;
GrabEnd();
}
}
protected override void GrabBegin()
{
base.GrabBegin();
}
protected override void GrabEnd()
{
if (m_grabbedObj != null)
{
OVRPose localPose = new OVRPose { position = OVRInput.GetLocalControllerPosition(m_controller), orientation = OVRInput.GetLocalControllerRotation(m_controller) };
OVRPose offsetPose = new OVRPose { position = m_anchorOffsetPosition, orientation = m_anchorOffsetRotation };
localPose = localPose * offsetPose;
OVRPose trackingSpace = transform.ToOVRPose() * localPose.Inverse();
Vector3 linearVelocity = trackingSpace.orientation * OVRInput.GetLocalControllerVelocity(m_controller);
Vector3 angularVelocity = trackingSpace.orientation * OVRInput.GetLocalControllerAngularVelocity(m_controller);
Vector3 handCenterOfMass = GetComponent<Rigidbody>().centerOfMass;
Vector3 handVelocityCross = Vector3.Cross(angularVelocity, m_grabbedObjectPosOff - handCenterOfMass);
if(throwGrabbable!=null)
throwGrabbable.SetHandVelocityCross(handVelocityCross);
GrabbableRelease(linearVelocity, angularVelocity);
}
// Re-enable grab volumes to allow overlap events
GrabVolumeEnable(true);
}
public void RemoveCandidates(OVRGrabbable grabbable)
{
if (grabbable == null) return;
// Remove the grabbable
int refCount = 0;
bool found = m_grabCandidates.TryGetValue(grabbable, out refCount);
if (!found)
{
return;
}
if (refCount > 1)
{
m_grabCandidates[grabbable] = refCount - 1;
}
else
{
m_grabCandidates.Remove(grabbable);
}
}
public void AddCandidates(OVRGrabbable grabbable)
{
if (grabbable == null) return;
foreach (KeyValuePair<OVRGrabbable, int> grabCandidate in m_grabCandidates)
{
if (grabCandidate.Key == grabbable)
{
return;
}
}
// Add the grabbable
int refCount = 0;
m_grabCandidates.TryGetValue(grabbable, out refCount);
m_grabCandidates[grabbable] = refCount + 1;
}
}