Skip to content

Commit a3f2698

Browse files
committed
[src] Update SofaObjectcontroller to handle rigid dof and script to record and replay a serie of keys actions
1 parent e5be39d commit a3f2698

3 files changed

Lines changed: 214 additions & 24 deletions

File tree

Scripts/Modules/Controllers/SofaObjectController.cs

Lines changed: 68 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ public class SofaObjectController : MonoBehaviour
1010
public string m_dataName = "";
1111
public float m_speed = 0.1f;
1212

13+
/// Parameter bool to store information if vec3 or rigid are parsed.
14+
[SerializeField]
15+
public bool isRigidMesh = false;
1316

1417
private bool m_ready = false;
1518
private Vector3 unityToSofa;
@@ -18,6 +21,10 @@ public class SofaObjectController : MonoBehaviour
1821
private Vector3 objectOri = Vector3.zero;
1922
private Vector3[] newPosition;
2023
private Vector3[] stopVelocity;
24+
25+
private float[] newPositionRigid;
26+
private float[] stopVelocityRigid;
27+
2128
private bool valueTracker;
2229
private SofaBoolData m_value = null;
2330

@@ -62,11 +69,22 @@ void Start()
6269
newPosition = new Vector3[1];
6370
stopVelocity = new Vector3[1];
6471
stopVelocity[0] = Vector3.zero;
72+
73+
newPositionRigid = new float[7];
74+
stopVelocityRigid = new float[7];
75+
for (int i = 0; i < 6; i++)
76+
{
77+
newPositionRigid[i] = 0;
78+
stopVelocityRigid[i] = 0;
79+
}
80+
newPositionRigid[6] = 1;
81+
stopVelocityRigid[6] = 0;
82+
6583
m_ready = true;
6684
}
6785

6886
// Update is called once per frame
69-
void Update()
87+
void FixedUpdate()
7088
{
7189
if (!m_ready)
7290
return;
@@ -86,20 +104,19 @@ void Update()
86104
else if (Input.GetKey(KeyCode.Keypad2))
87105
MoveDown();
88106

89-
if (Input.GetKeyDown(KeyCode.Space) && m_value != null)
107+
if (Input.GetKeyDown(KeyCode.Space))
90108
{
91-
valueTracker = !valueTracker;
92-
m_value.Value = valueTracker;
109+
ApplyAction();
93110
}
94111
}
95112

96-
void FixedUpdate()
97-
{
98-
if (!m_ready)
99-
return;
113+
//void FixedUpdate()
114+
//{
115+
// if (!m_ready)
116+
// return;
100117

101-
UpdateCapsuleFromSofa();
102-
}
118+
// //UpdateCapsuleFromSofa();
119+
//}
103120

104121

105122
protected void UpdateCapsuleFromSofa()
@@ -115,44 +132,71 @@ protected void UpdateCapsuleFromSofa()
115132
this.transform.position = objectOri;
116133
}
117134

118-
protected void MoveForward()
135+
public void MoveForward()
119136
{
120137
newPosition[0] = objectOri - this.transform.up * m_speed;
121-
m_sofaMesh.SetVelocities(stopVelocity);
122-
m_sofaMesh.SetVertices(newPosition);
138+
UpdateToSofa(newPosition[0]);
123139
}
124140

125-
protected void MoveBackward()
141+
public void MoveBackward()
126142
{
127143
newPosition[0] = objectOri + this.transform.up * m_speed;
128-
m_sofaMesh.SetVelocities(stopVelocity);
129-
m_sofaMesh.SetVertices(newPosition);
144+
UpdateToSofa(newPosition[0]);
130145
}
131146

132147

133148

134149

135-
protected void MoveUp()
150+
public void MoveUp()
136151
{
137152
newPosition[0] = objectOri + this.transform.forward * m_speed;
138-
m_sofaMesh.SetVertices(newPosition);
153+
UpdateToSofa(newPosition[0]);
139154
}
140155

141-
protected void MoveDown()
156+
public void MoveDown()
142157
{
143158
newPosition[0] = objectOri - this.transform.forward * m_speed;
144-
m_sofaMesh.SetVertices(newPosition);
159+
UpdateToSofa(newPosition[0]);
145160
}
146161

147-
protected void MoveLeft()
162+
public void MoveLeft()
148163
{
149164
newPosition[0] = objectOri + this.transform.right * m_speed;
150-
m_sofaMesh.SetVertices(newPosition);
165+
UpdateToSofa(newPosition[0]);
151166
}
152167

153-
protected void MoveRight()
168+
public void MoveRight()
154169
{
155170
newPosition[0] = objectOri - this.transform.right * m_speed;
156-
m_sofaMesh.SetVertices(newPosition);
171+
UpdateToSofa(newPosition[0]);
172+
}
173+
174+
public void ApplyAction()
175+
{
176+
if (m_value != null)
177+
{
178+
valueTracker = !valueTracker;
179+
m_value.Value = valueTracker;
180+
}
181+
}
182+
183+
protected void UpdateToSofa(Vector3 my_newPosition)
184+
{
185+
if (isRigidMesh)
186+
{
187+
newPositionRigid[0] = my_newPosition[0];
188+
newPositionRigid[1] = my_newPosition[1];
189+
newPositionRigid[2] = my_newPosition[2];
190+
m_sofaMesh.SetVelocities(stopVelocityRigid);
191+
m_sofaMesh.SetPositions(newPositionRigid);
192+
}
193+
else
194+
{
195+
m_sofaMesh.SetVelocities(stopVelocity);
196+
m_sofaMesh.SetVertices(newPosition);
197+
}
198+
199+
this.transform.position = my_newPosition;
200+
objectOri = my_newPosition;
157201
}
158202
}

0 commit comments

Comments
 (0)