-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmegneticBall.cs
201 lines (180 loc) · 7.32 KB
/
megneticBall.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
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
using HTC.UnityPlugin.Vive;
public class megneticBall : MonoBehaviour {
private Vector3[] p2;
public Transform leftHandTransform;
public Transform rightHandTransform;
int leftHandLastZone = -1;
int rightHandLastZone = -1;
float maxMagneticDistance = 0;
public static float magneticSpeed = 1f;
GameObject[] balls;
Component[]ballMoveComponent;
Vector3 []target;
bool []shouldMove;
public SteamVR_Action_Vibration Vibration;
// Use this for initialization
void Start () {
p2 = nearballs.getCenters();
maxMagneticDistance = getMaxMagneticDistance(move.distanceFromBallsSphere, move.scaleOfBalls / 2);
ballMoveComponent = new Component[p2.Length];
balls = new GameObject[p2.Length];
target = new Vector3[p2.Length];
shouldMove = new bool[18];
for (int i=0;i<p2.Length;i++)
{
balls[i] = GameObject.Find("Sphere (" + i + ")");
//ballMoveComponent[i] = balls[i].GetComponent<ballMove>();
shouldMove[i] = false;
}
}
void moveBall(int ballNum, Vector3 t)
{
target[ballNum] = t;
shouldMove[ballNum] = true;
}
// Update is called once per frame
void Update () {
int leftHandThisZone = getZone(leftHandTransform.position, p2, maxMagneticDistance);
int rightHandThisZone = getZone(rightHandTransform.position, p2, maxMagneticDistance);
//处理左手
if(leftHandLastZone == -1 && leftHandThisZone == -1)
{
//区域外移动,什么也不做
}
else if(leftHandLastZone == -1 && leftHandThisZone != -1)
{
//进入某球的磁性区域
intoZone(leftHandTransform.position, leftHandThisZone, SteamVR_Input_Sources.LeftHand);
}
else if( leftHandLastZone != -1 && leftHandThisZone == -1)
{
//退出某区域
outZone(leftHandLastZone, SteamVR_Input_Sources.LeftHand);
}
else if(leftHandLastZone != -1 && leftHandThisZone != -1 && (leftHandThisZone == leftHandLastZone))
{
//同一区域内移动
inZoneMove(leftHandTransform.position, leftHandThisZone, SteamVR_Input_Sources.LeftHand);
}
else if(leftHandLastZone != -1 && leftHandThisZone != -1 && (leftHandThisZone != leftHandLastZone))
{
//迅速从一个区域移动到另一区域
outZone(leftHandLastZone, SteamVR_Input_Sources.LeftHand);
intoZone(leftHandTransform.position, leftHandThisZone, SteamVR_Input_Sources.LeftHand);
}
else
{
//不可能出现
}
//处理右手
if (rightHandLastZone == -1 && rightHandThisZone == -1)
{
//区域外移动,什么也不做
}
else if (rightHandLastZone == -1 && rightHandThisZone != -1)
{
//进入某球的磁性区域
intoZone(rightHandTransform.position, rightHandThisZone, SteamVR_Input_Sources.RightHand);
}
else if (rightHandLastZone != -1 && rightHandThisZone == -1)
{
//退出某区域
outZone(rightHandLastZone, SteamVR_Input_Sources.RightHand);
}
else if (rightHandLastZone != -1 && rightHandThisZone != -1 && (rightHandThisZone == rightHandLastZone))
{
//同一区域内移动
inZoneMove(rightHandTransform.position, rightHandThisZone, SteamVR_Input_Sources.RightHand);
}
else if (rightHandLastZone != -1 && rightHandThisZone != -1 && (rightHandThisZone != rightHandLastZone))
{
//迅速从一个区域移动到另一区域
outZone(rightHandLastZone, SteamVR_Input_Sources.RightHand);
intoZone(rightHandTransform.position, rightHandThisZone, SteamVR_Input_Sources.RightHand);
}
else
{
//不可能出现
}
//进行lastZone的更新
leftHandLastZone = leftHandThisZone;
rightHandLastZone = rightHandThisZone;
float step = megneticBall.magneticSpeed * Time.deltaTime;
for (int i=0;i<18;i++)
{
if (shouldMove[i])
{
Debug.Log(step);
balls[i].transform.position = Vector3.MoveTowards(balls[i].transform.position, target[i], step);
if (balls[i].transform.position.Equals(target[i]))
shouldMove[i] = false;
}
}
}
float getMaxMagneticDistance(float distanceBetweenBalls, float ballsRadius)
{
return Mathf.Max(distanceBetweenBalls / 4, ballsRadius);
}
int getZone(Vector3 pos, Vector3 []centers, float maxMagneticDistance)
{
for(int i=0;i<centers.Length;i++)
{
if(Mathf.Abs(pos.x-centers[i].x)<= maxMagneticDistance &&
Mathf.Abs(pos.y - centers[i].y) <= maxMagneticDistance &&
Mathf.Abs(pos.z - centers[i].z) <= maxMagneticDistance)
{
if((pos-centers[i]).magnitude<=maxMagneticDistance)
{
return i;
}
}
}
return -1;
}
void intoZone(Vector3 pos, int zoneNum, SteamVR_Input_Sources whichHand)
{
//吸附
//Debug.Log("ball " + zoneNum + " is magneted to " + (whichHand.Equals(SteamVR_Input_Sources.LeftHand) ? "lefthand." : "righthand"));
//ballMoveComponent[zoneNum]
//StartCoroutine(MoveObject(ballInThisZone.transform.position, pos, (ballInThisZone.transform.position - pos).magnitude / magneticSpeed));
moveBall(zoneNum,pos);
Pulse(0.3f, 200, 2f, whichHand);
//ballInThisZone.transform.position = Vector3.MoveTowards(ballInThisZone.transform.position, pos, magneticSpeed * Time.deltaTime);
//播放声音
//振动(?)
}
void outZone(int zoneNum, SteamVR_Input_Sources whichHand)
{
//小球归位
//Debug.Log("ball " + zoneNum + " return back to its center ");
moveBall(zoneNum, p2[zoneNum]);
Pulse(0.1f, 200, 0.5f, whichHand);
//ballInThisZone.transform.position = Vector3.MoveTowards(ballInThisZone.transform.position, p2[zoneNum], magneticSpeed * Time.deltaTime);
//(?)播放声音(?)
}
void inZoneMove(Vector3 moveto, int zoneNum, SteamVR_Input_Sources whichHand)
{
//小球跟随移动
moveBall(zoneNum, moveto);
}
//在time时间内移动物体
private IEnumerator MoveObject(Vector3 startPos, Vector3 endPos, float time)
{
var dur = 0.0f;
while (dur <= time)
{
dur += Time.deltaTime;
transform.position = Vector3.Lerp(startPos, endPos, dur / time);
yield return null;
}
}
public void Pulse(float duration, float frequency, float amplitude, SteamVR_Input_Sources source)
{
//print("make a pulse, duration = " + duration + ", frequency = " + frequency + ", amplitude = " + amplitude);
Vibration.Execute(0, duration, frequency, amplitude, source);
}
}