Introduction: How to Use PocketLab As a Rotational Controller for a Scratch Game
This instructable will give you some of the basics needed to put together a small Scratch game that uses a PocketLab sensor as a controller. The angular velocity data from the PocketLab gives the game information about rotational position, so that when the PocketLab is rotated, objects in the game are rotated as well.
Learn about Scratch here: https://scratch.mit.edu/about/.
Learn how to get Scratch connected to PocketLab here: http://support.thepocketlab.com/topic/scratch-getting-started-guide/.
What you will need:
- PocketLab sensor
- PocketLab ScratchX Chrome extension
- Windows 10 device
Step 1: Create the Lists and Variables to Store PocketLab Data
First, you need to create a couple of lists so that you can store information coming from the PocketLab in the background while the game is running. Click on Data, under Scripts, and make two lists: one named time and one named gyroZValues. The time list will store the number of seconds since the game has started and the gyroZValues will store the angular velocity values around the z-axis collected by the PocketLab gyroscope. Then make a variable named gyroZPosition so that you can track the current rotational position of the PocketLab in the z-axis.
Step 2: Create a Script to Store Angular Velocity Data in Your Lists
You'll want your game to be constantly collecting information, so this script should probably be created on the Stage sprite. When your game starts (in this case when the flag is clicked), this script should first clear the lists, variables, and reset the timer so that the game starts fresh. Then, it should begin to add timer and gyroZ values to their respective lists for as long as the game is running. Every time the game runs through the forever block, one timer value and one gyroZ value are added, so we have a running list of gyroZ values and the times at which they were collected. These will be used to integrate the angular velocity to find the PocketLab's rotational position.
Step 3: Create a Script to Integrate the Angular Velocity
To easily estimate the integral of the angular velocity graph and find the PocketLab's rotational position, you can use the trapezoid rule. Simply stated, you will need to calculate the area of the trapezoid formed by the gyroZ values across a certain period of time. For a trapezoid, Area = (Base1+ Base2)/2 * Height. Base1 will be one gyroZ reading from the PocketLab, Base2 will be the gyroZ reading directly before it in the list, and Height will be the amount of time between these two readings. The code statement above takes the last gyroZ value in its list and adds it to the second to last gyroZ value. Then its divided by 2 and multiplied by the difference between the last time value and second to last time value. The result is added to the gyroZPosition. If you place this code inside a forever block, the game will constantly update gyroZPosition with the PocketLab's current rotational position in the real world.
Step 4: Using GyroZPosition to Move Sprites
Now that you have a variable that tracks the rotational position of the PocketLab, you can use it to control the position of sprites in the game. If you just want your sprite to rotate in place, you can simply use the point in direction block to set the direction of your sprite to gyroZPosition. If you want your sprite to rotate around a single point, set its x position to the radius of rotation times the cosine of gyroZPosition and its y position to the radius of rotation times the sine of gyroZPosition. And if you want the sprite to move linearly, just set its x or y position to pi divided by 180 times the number of steps per rotation times gyroZPosition.
Step 5: Implementing Game Mechanics
Now that you have a way to move sprites in the game using the PocketLab sensor, you can start designing the game that uses these controls. For my game, I have the player control a spaceship that flies around the Earth and defends it from projectiles being fired at it by giant space crabs. This game involved the use of four main sprites: an Earth sprite that just sits in the middle of the screen, a spaceship sprite controlled by the player which rotates around the Earth sprite, a crab sprite that acts as an enemy, and a ball sprite that acts as a projectile fired by the crab. Whenever the ball sprite receives the shoot message, it glides from the crab to the Earth. Meanwhile, it is constantly checking to see if it has collided with the spaceship or the Earth. If the ball collides with the spaceship, it hides itself and nothing happens. If it collides with the Earth, however it sets the earthDestroyed variable to 1 so that the game knows that the Earth has been destroyed and the player has lost. There's more to the full programming of the game, but this is pretty much the bare bones of the the system behind it.
If you would like to play my game or see all the code behind it, click here: https://www.dropbox.com/s/hr5orm50lk2fldv/KillerCrabsFromOuterSpace.sbx?dl=0
Participated in the
Maker Olympics Contest 2016