Skip to content

Commit 4a86b9d

Browse files
authored
Merge pull request #76 from UCL/km/star-collector-data
Expand data for star collector game
2 parents c898034 + d30385b commit 4a86b9d

13 files changed

Lines changed: 207 additions & 44 deletions

File tree

‎docs/space-walking.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Data is saved to `SpaceWalkingScores.csv`, with one row per played game. Values
3939
- `endTime`: the game end time in format HH:MM:ss (local time - see startTime description)
4040
- `gameCompleted`: whether this game was completed. If they exited early, this will be false.
4141
- `timeLimitSeconds`: the time limit set for this game in seconds
42-
- `gameDurationSeconds`: how long the game was played in seconds. If the game was played through to completion this will be equal to timeLimitSeconds; if they exited early, it will be less.
42+
- `gameDurationSeconds`: how long the game was played (rounded to the nearest second). If the game was played through to completion this will be equal to timeLimitSeconds; if they exited early, it will be less.
4343
- `nCompleteSteps`: number of completed steps during this game. One complete step = a step out and back to the centre.
4444
- `headTurnsActive`: whether head turn arrows were active for this game (only active at highest difficulty level)
4545
- `nCompleteHeadTurns`: number of completed head turns (will be 0 if `headTurnsActive` is false)

‎docs/star-collector.md‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,46 @@ top of the screen.
55

66
## Main objects / values to edit during play testing
77

8-
- **StarCollectorManager**: values related to the overall win condition of the game
8+
- **StarCollectorManager**: values related to the overall win condition of the game, and head speed save data
99
- Min / max time limit in seconds
1010
- Time limit increment in seconds (if time limit upgrade % is met)
1111
- Number of games in a row that must meet the upgrade % to increase the time limit
1212
- The length of the time window used to evaluate player performance
1313
- The % of stars that must be collected to increase speed or time limit
14+
- The maximum number of head yaw readings to keep in the buffer at one time (these are used for the head speed save data)
15+
- The minimum number of head yaw readings needed to calculate a head speed
16+
- The number of seconds to calculate head yaw speed over
1417

1518
- **StarGenerator**: values related to generation of the wave of stars
1619
- Min, max and base star speed
1720
- The amount the star speed increases per upgrade
1821
- Shape of the wave (e.g. width, swerve, star sampling)
1922

2023
- **Ship**: values related to ship movement
21-
- The amount the ship moves per degree of head movement (X By Degrees)
24+
- The amount the ship moves per degree of head movement (X By Degrees)
25+
26+
## Save data
27+
28+
Data is saved to `StarCollectorScores.csv`, with one row per played game. Values are:
29+
30+
- `gameNumber`: a unique id per played star collector game
31+
- `sessionNumber`: the session this game was played in (corresponds to sessionNumber in [`SessionSummary.csv`](./session-summary.md))
32+
- `date`: the date of the game session in format YYYY-MM-DD
33+
- `startTime`: the game start time in format HH:MM:ss. This is the local time (e.g. if your computer is set to UK time - this is UK time).
34+
- `endTime`: the game end time in format HH:MM:ss (local time - see startTime description)
35+
- `gameCompleted`: whether this game was completed. If they exited early, this will be false.
36+
- `timeLimitSeconds`: the time limit set for this game in seconds
37+
- `gameDurationSeconds`: how long the game was played (rounded to the nearest second). If the game was played through to completion this will be equal to timeLimitSeconds; if they exited early, it will be less.
38+
- `nStarsCollected`: the number of stars collected during the game
39+
- `percentStarsCollected`: the percent of all stars collected during the game, rounded to 2 decimal places.
40+
- `adaptiveLevel`: an integer (1 or above) representing the current difficulty level. Every time the game time limit is increased, this level increases by one.
41+
- `finalStarFallSpeed`: Speed of falling stars (unity units per second) at the end of the game.
42+
43+
**Note**: all head speed measures below are calculated as follows. Every `samplingIntervalSeconds` (a configurable parameter in `StarCollectorManager`), the absolute differences of head yaw angle between consecutive readings in the time period are summed together and divided by the total difference in time to give a speed in degrees per second.
44+
If the player goes out of range at any point in those `samplingIntervalSeconds` (i.e. the tracker can no longer detect them), then that speed measurement is discarded.
45+
46+
At the end of the game, all sampled speed measurements are used to calculate the summary statistics below:
47+
48+
- `headSpeedDegPerSecMean`: Mean head yaw speed (left-right rotation) measured in degrees per second. Rounded to 2 decimal places.
49+
- `headSpeedDegPerSecPeak`: Peak head yaw speed (left-right rotation) measured in degrees per second. Rounded to 2 decimal places.
50+
- `headSpeedDegPerSecSD`: Standard deviation of head yaw speed (left-right rotation) measured in degrees per second. Rounded to 2 decimal places.

‎docs/star-seek.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ Data is saved to `StarSeekScores.csv`, with one row per played game. Values are:
3232
- `endTime`: the game end time in format HH:MM:ss (local time - see startTime description)
3333
- `gameCompleted`: whether this game was completed. If they exited early, this will be false.
3434
- `timeLimitSeconds`: the time limit set for this game in seconds
35-
- `gameDurationSeconds`: how long the game was played in seconds. If the game was played through to completion this will be equal to timeLimitSeconds; if they exited early, it will be less.
35+
- `gameDurationSeconds`: how long the game was played (rounded to the nearest second). If the game was played through to completion this will be equal to timeLimitSeconds; if they exited early, it will be less.
3636
- `nStarsCollected`: the number of stars collected during the game
3737
- `adaptiveLevel`: an integer (1 or above) representing the current difficulty level. Every time the time limit is increased, this level increases by one.

‎projects/AstroBalance/Assets/Scripts/CountdownTimer.cs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,23 @@ public void StopCountdown()
6767
{
6868
UpdateTimerText(0);
6969
timerRunning = false;
70+
timeRemaining = 0;
7071
}
7172

7273
/// <summary>
7374
/// Get remaining time in seconds
7475
/// </summary>
7576
public float GetTimeRemaining()
7677
{
77-
return timeRemaining;
78+
return timeRemaining < 0 ? 0 : timeRemaining;
79+
}
80+
81+
/// <summary>
82+
/// Get elapsed time in seconds (time limit - time remaining)
83+
/// </summary>
84+
public float GetElapsedTime()
85+
{
86+
return timeLimit - GetTimeRemaining();
7887
}
7988

8089
/// <summary>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using UnityEngine;
4+
5+
public static class MathsUtilities
6+
{
7+
public static int RoundToNearestInt(float number)
8+
{
9+
return Mathf.FloorToInt(number + 0.5f);
10+
}
11+
12+
public static float RoundTo2DecimalPlaces(float number)
13+
{
14+
return (float)Mathf.FloorToInt((number * 100) + 0.5f) / 100;
15+
}
16+
17+
public static float StandardDeviation(List<float> numbers)
18+
{
19+
float average = numbers.Average();
20+
return Mathf.Sqrt(numbers.Average(v => Mathf.Pow(v - average, 2)));
21+
}
22+
}

‎projects/AstroBalance/Assets/Scripts/MathsUtilities.cs.meta‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎projects/AstroBalance/Assets/Scripts/SpaceWalking/SpaceWalkingManager.cs‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -252,18 +252,9 @@ private void SaveGameData(bool gameComplete)
252252
// Update save data for this game
253253
gameData.gameCompleted = gameComplete;
254254
gameData.timeLimitSeconds = timeLimit;
255-
256-
float remainingTime = timer.GetTimeRemaining();
257-
if (remainingTime > 0)
258-
{
259-
gameData.gameDurationSeconds = Mathf.FloorToInt(timeLimit - remainingTime + 0.5f);
260-
}
261-
else
262-
{
263-
gameData.gameDurationSeconds = timeLimit;
264-
}
265-
255+
gameData.gameDurationSeconds = MathsUtilities.RoundToNearestInt(timer.GetElapsedTime());
266256
gameData.LogEndTime();
257+
267258
gameData.nCompleteSteps = stepScore;
268259
gameData.headTurnsActive = headTurnsActive;
269260
gameData.nCompleteHeadTurns = headTurnScore;

‎projects/AstroBalance/Assets/Scripts/StarCollector/StarCollectorData.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
public class StarCollectorData : GameData
66
{
77
public int timeLimitSeconds;
8+
public int gameDurationSeconds;
89
public int nStarsCollected;
910
public float percentStarsCollected;
11+
public int adaptiveLevel;
12+
public float finalStarFallSpeed;
13+
public float headSpeedDegPerSecMean;
14+
public float headSpeedDegPerSecPeak;
15+
public float headSpeedDegPerSecSD;
1016
}

‎projects/AstroBalance/Assets/Scripts/StarCollector/StarCollectorManager.cs‎

Lines changed: 125 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using TMPro;
4+
using Tobii.GameIntegration.Net;
45
using UnityEngine;
56

67
public class StarCollectorManager : MonoBehaviour
@@ -49,30 +50,55 @@ public class StarCollectorManager : MonoBehaviour
4950
]
5051
private int timeLimitUpgradePercent = 60;
5152

53+
[
54+
SerializeField,
55+
Tooltip("Maximum number of head yaw readings to keep in the buffer at one time")
56+
]
57+
private int maxNItemsInBuffer = 100;
58+
59+
[
60+
SerializeField,
61+
Tooltip("The minimum number of head yaw readings needed to calculate a head speed")
62+
]
63+
private int minNItemsForSpeed = 5;
64+
65+
[SerializeField, Tooltip("The number of seconds to calculate head yaw speed over")]
66+
private float samplingIntervalSeconds = 0.5f;
67+
5268
private TextMeshProUGUI winText;
69+
private Tracker tracker;
5370
private int timeLimit;
5471
private int score; // stars collected over whole game
5572
private int missed; // stars missed over whole game
5673
private bool gameActive = true;
5774

58-
private float windowStart;
75+
private float speedWindowStart; // start time of speed upgrade window
5976
private int scoreInTimeWindow = 0; // stars collected in time window
6077
private int missedInTimeWindow = 0; // stars missed in time window
6178
private string saveFilename = "StarCollectorScores";
6279
private StarCollectorData gameData;
6380

81+
private float bufferWindowStart; // start time of buffer update window
82+
private bool outOfRangeInWindow = false; // whether the player went out of range of the tracker during this window
83+
private HeadAngleBuffer headYawBuffer;
84+
private List<float> headYawSpeeds = new List<float>();
85+
6486
// Start is called once before the first execution of Update after the MonoBehaviour is created
6587
void Start()
6688
{
6789
ChooseGameTimeLimit();
6890

6991
winText = winScreen.GetComponentInChildren<TextMeshProUGUI>();
92+
tracker = FindFirstObjectByType<Tracker>();
7093
gameData = new StarCollectorData();
7194
score = 0;
7295
scoreText.text = score.ToString();
96+
97+
headYawBuffer = new HeadAngleBuffer(maxNItemsInBuffer, minNItemsForSpeed);
7398
timer.StartCountdown(timeLimit);
7499

75-
windowStart = Time.time;
100+
speedWindowStart = Time.time;
101+
bufferWindowStart = Time.time;
76102
}
77103

78104
/// <summary>
@@ -129,9 +155,18 @@ void Update()
129155
return;
130156
}
131157

158+
// Keep track of head yaw angles on every update
159+
UpdateHeadYawBuffer();
160+
161+
// Every 'samplingIntervalSeconds', record the speed averaged over that time period
162+
if (Time.time - bufferWindowStart >= samplingIntervalSeconds)
163+
{
164+
RecordHeadSpeed();
165+
}
166+
132167
// At end of time window, assess performance and update the difficulty
133168
// of the game
134-
if (Time.time - windowStart >= difficultyWindowSeconds)
169+
if (Time.time - speedWindowStart >= difficultyWindowSeconds)
135170
{
136171
UpdateDifficulty();
137172
}
@@ -143,6 +178,47 @@ void Update()
143178
}
144179
}
145180

181+
/// <summary>
182+
/// Add latest head yaw angle to buffer
183+
/// </summary>
184+
private void UpdateHeadYawBuffer()
185+
{
186+
// If the player goes out of range of the tracker
187+
if (!tracker.isPlayerDetected())
188+
{
189+
outOfRangeInWindow = true;
190+
}
191+
else
192+
{
193+
HeadPose headPose = tracker.getHeadPose();
194+
HeadYawItem headYaw = new HeadYawItem(headPose);
195+
headYawBuffer.addIfNew(headYaw);
196+
}
197+
}
198+
199+
/// <summary>
200+
/// Record the latest head yaw speed
201+
/// </summary>
202+
private void RecordHeadSpeed()
203+
{
204+
// Only record speeds if the player was in range of the tracker for the whole window.
205+
// (otherwise, if they've been out of range for a while, we may be calculating the speed of quite old data in the buffer)
206+
if (!outOfRangeInWindow)
207+
{
208+
float headSpeed = headYawBuffer.getSpeed(samplingIntervalSeconds);
209+
210+
// If there aren't enough recorded head yaw angles yet, the returned speed is zero.
211+
// We don't want to include these readings in the overall averages.
212+
if (headSpeed > 0)
213+
{
214+
headYawSpeeds.Add(headSpeed);
215+
}
216+
}
217+
218+
outOfRangeInWindow = false;
219+
bufferWindowStart = Time.time;
220+
}
221+
146222
/// <summary>
147223
/// Dynamically update the difficulty of the game based on player performance.
148224
///
@@ -164,7 +240,7 @@ private void UpdateDifficulty()
164240
starGenerator.DecreaseSpeed();
165241
}
166242

167-
windowStart = Time.time;
243+
speedWindowStart = Time.time;
168244
scoreInTimeWindow = 0;
169245
missedInTimeWindow = 0;
170246
}
@@ -220,26 +296,63 @@ private void EndGame()
220296
winScreen.SetActive(true);
221297

222298
// Save game details to file
223-
SaveGameData();
299+
SaveGameData(true);
224300
}
225301
}
226302

227-
private void SaveGameData()
303+
private void OnDestroy()
228304
{
229-
float totalStars = score + missed;
230-
float percentCollected = ((float)score / totalStars) * 100;
305+
// If the scene is exited early (e.g. with the exit button), then save this
306+
// partial game's data
307+
if (gameActive)
308+
{
309+
SaveGameData(false);
310+
}
311+
}
231312

313+
private void SaveGameData(bool gameComplete)
314+
{
232315
// Update save data for this game
233-
gameData.gameCompleted = true;
316+
gameData.gameCompleted = gameComplete;
234317
gameData.timeLimitSeconds = timeLimit;
235-
gameData.nStarsCollected = score;
236-
gameData.percentStarsCollected = percentCollected;
318+
gameData.gameDurationSeconds = MathsUtilities.RoundToNearestInt(timer.GetElapsedTime());
237319
gameData.LogEndTime();
238320

321+
float totalStars = score + missed;
322+
float percentCollected = ((float)score / totalStars) * 100;
323+
gameData.nStarsCollected = score;
324+
gameData.percentStarsCollected = MathsUtilities.RoundTo2DecimalPlaces(percentCollected);
325+
gameData.adaptiveLevel =
326+
1 + Mathf.CeilToInt((timeLimit - minTimeLimit) / timeLimitIncrement);
327+
gameData.finalStarFallSpeed = starGenerator.GetStarSpeed();
328+
329+
if (headYawSpeeds.Count() == 0)
330+
{
331+
gameData.headSpeedDegPerSecPeak = 0;
332+
gameData.headSpeedDegPerSecMean = 0;
333+
gameData.headSpeedDegPerSecSD = 0;
334+
}
335+
else
336+
{
337+
gameData.headSpeedDegPerSecPeak = MathsUtilities.RoundTo2DecimalPlaces(
338+
headYawSpeeds.Max()
339+
);
340+
gameData.headSpeedDegPerSecMean = MathsUtilities.RoundTo2DecimalPlaces(
341+
headYawSpeeds.Average()
342+
);
343+
float standardDeviation = MathsUtilities.StandardDeviation(headYawSpeeds);
344+
gameData.headSpeedDegPerSecSD = MathsUtilities.RoundTo2DecimalPlaces(standardDeviation);
345+
}
346+
239347
SaveGameData<StarCollectorData> saveData = new(saveFilename);
348+
gameData.sessionNumber = CaptureSessionData.CurrentSessionNumber();
349+
gameData.gameNumber = saveData.GetNextGameNumber();
240350
saveData.Save(gameData);
241351

242352
// Update save data for this session
243-
CaptureSessionData.MarkGameAsComplete("nCompleteStarCollectorGames");
353+
if (gameComplete)
354+
{
355+
CaptureSessionData.MarkGameAsComplete("nCompleteStarCollectorGames");
356+
}
244357
}
245358
}

‎projects/AstroBalance/Assets/Scripts/StarCollector/StarGenerator.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private void UpdateSpeed(float increment)
103103
baseStarSpeed = nextSpeed;
104104
}
105105

106-
Debug.Log($"Updated star speed to {nextSpeed}");
106+
Debug.Log($"Updated star speed to {baseStarSpeed}");
107107
}
108108

109109
public void StopGeneration()

0 commit comments

Comments
 (0)