Hardware Reference
In-Depth Information
You may not see the paddles until you flex
the sensors. The range of your sensors will be
different, depending on how they're physically
attached to the monkey, and how far you can flex his arms.
The map() function maps the sensors' ranges to the range
of the paddle movement, but you need to determine what
the sensors' range is. For this part, it's important that
you have the sensors embedded in the monkey's arms,
as you'll be fine-tuning the system, and you want the
sensors in the locations where they'll actually get used.
Once you've set the sensors' positions in the monkey,
run the Processing program again and watch the left and
right sensor numbers as you flex the monkey's arms.
Write down the maximum and minimum values on each
arm. Then, fill them into the four variables, leftMinimum ,
leftMaximum , rightMinimum , and leftMaximum, in the
setup() method. Once you've adjusted these variables, the
paddles' movement should cover the screen height when
you move the monkey's arms.
Finally, it's time to add the ball.
The ball will move from left to right
diagonally. When it hits the top or
bottom of the screen, it will bounce off
and change vertical direction. When it
reaches the left or right, it will reset to
the center. If it touches either of the
paddles, it will bounce off and change
horizontal direction. To make all that
happen, you'll need five new variables
at the top of the program, just before
the setup() method.
8
int ballSize = 10; // the size of the ball
int xDirection = 1; // the ball's horizontal direction.
// left is -1, right is 1.
int yDirection = 1; // the ball's vertical direction.
// up is -1, down is 1.
int xPos, yPos; // the ball's horizontal and vertical positions
At the end of the setup() method,
you need to give the ball an initial
position in the middle of the window.
8
// initialize the ball in the center of the screen:
xPos = width/2;
yPos = height/2;
8
Now, add two methods at the end of
the program, one called animateBall()
and another called resetBall() . You'll
call these from the draw() method
shortly.
void animateBall() {
// if the ball is moving left:
if (xDirection < 0) {
// if the ball is to the left of the left paddle:
if ((xPos <= leftPaddleX)) {
// if the ball is in between the top and bottom
// of the left paddle:
if((leftPaddle - (paddleHeight/2) <= yPos) &&
(yPos <= leftPaddle + (paddleHeight /2))) {
// reverse the horizontal direction:
xDirection =-xDirection;
}
}
}
// if the ball is moving right:
else {
// if the ball is to the right of the right paddle:
if ((xPos >= ( rightPaddleX + ballSize/2))) {
// if the ball is in between the top and bottom
// of the right paddle:
if((rightPaddle - (paddleHeight/2) <=yPos) &&
ยป
 
Search WWH ::




Custom Search