This project assumes you have completed the following guides:
If you've completed everything above, keep reading. If not, make sure you double back first!
VINT Hub Phidget
|Phidget Cable||USB Cable|
Connect your Phidget Spatial to your VINT Hub, and your VINT Hub to your computer.
Open the Phidget Control Panel and verify everything is connected properly.
The orientation of the maze is directly linked to the Spatial Phidget.
After completing the Roll-a-Ball Tutorial, you'll have a play area with raised edges and yellow collectibles. You can reuse these items to create a 3D maze.
When your program starts, you need to create your Accelerometer object and get everything
set up as usual.
For now, skip the maze creation and just have your ball on a flat surface. Generating a maze will be discussed below.
Next, you can add your events. Here's the relevant code you will need:
Acceleration Change Event
Next, you can take a look at your update code. There are two used in this project:
- Fixed Update
Your Update code will handle things like an escape key being pressed to exit the game, or the space key being pressed to reset the ball and generate a new maze.
In your Fixed Update code, you should convert your angles and rotate your maze. This process is described in more detail below!
When the ball reaches the goal, you will need to end the game and print a win statement. For bonus points, you could try having multiple levels of increasing maze difficulty. When the goal is reached, the level is passed and the next stage is generated.
As always, you will want to properly close your Phidget objects when an application ends.
Add the following Unity method to your code:
In order to implement some of the code described above, you will need to learn about angle
calculations. Check out the Calculating Tilt Angles with the Spatial Phidget document.
If you've already viewed the document, here are the relevant formulas:
`theta = "roll angle "phi = "pitch angle"`
`M_x = "Measured x-axis acceleration"`
`M_y = "Measured y-axis acceleration"`
`M_z = "Measured z-axis acceleration"`
Now that you've calcluated your tilt angles, the next step is converting them into a form Unity understands. Unity uses a system to describe rotations called Quaternions. You can convert your accelerometer angles to a Quaternion like so:
Hint: Check out the Slerp function on the Unity API if you are stuck.
After making sure the maze tilts properly, the game is almost complete. Any good maze needs walls to complete it and here are two ways to go about it.
- Manually create and layout the walls.
- Automate the process using a Maze Generation Algorithm
By using an automated system, users can reset their ball and generate new mazes by simply pressing
the space bar.
To put the final touches on the game, try tinkering with the physics options. Changing gravity and the angular drag on the ball can change the responsiveness of the system. Additionally, it can be helpful to spawn the player ball a bit above the maze's surface to ensure it does not fall through or stick to the surface.
Thanks for reading! If you have any suggestions for future Phidgets Education projects, let us know at email@example.com