WiiLab Example Code - BouncingBall
Note:
- MATLAB R2007a or greater is required for this code to run correctly.
- This code is dependent upon several other functions and will not run as a standalone program.
Annotations
Basic steps:
- Establish a connection with the Wiimote
- Initialize figures
- interactive bouncing ball figure
- plot to track the ball's position
- Use the yMove function to move the ball up and down in response to the y-acceleration obtained from the Wiimote
- Set a constant x-velocity to the value obtained from the Wiimote's x-accelerometer when swinging the Wiimote to the right
- While the ball is still within the boundaries of the window
- Use the xMove function to keep the ball moving at a constant horizontal velocity
- Use the yMove function to move the ball vertically with a constantly decreasing y-velocity
- special case: When the ball hits the ground, the new y-velocity should account for elasticity and a change in direction
- Obtain and plot the ball's position
- Close the bouncing ball figure
Raw Source Code
The source code is also available as a .m file download at the bottom of the page.
function BouncingBall()
% usage: BouncingBall()
% purpose: Creates an interactive bouncing ball application.
% Users can lift and throw the ball using the Wiimote
% and watch it bounce.
% Note: The animation is a little off. If the y-velocity gets low
% enough, there are little "hiccups" during which the ball's
% bounce appears to be higher than the previous bounce.
global xVel;
global yVel;
global ball;
global graph;
% add the path containing simple graphics functions
addpath .\EG111-H
% add the path containing the Wiimote functions
addpath .\WiimoteFunctions
% add the path containing the bouncing ball functions
addpath .\BouncingBallFunctions
% add the path containing simple graph functions
addpath .\WiimoteFunctions\GraphingFunctions
% create an instance of the Wiimote
initializeWiimote();
% initialize a new figure for the bouncing ball interactions
createWindow(800, 480, 500, 300);
setTitle('Bouncing Ball');
bb_figure = gcf;
% if a connection has been established with the Wiimote
if(isWiimoteConnected())
% take care of all the basic stuff and prepare the ball for motion
resetWindow(bb_figure);
dir3 = 'Press HOME to quit and try again.';
t3 = text(250, 290, dir3, 'HorizontalAlignment', 'center', 'FontSize', 12);
% get the ball's location
[x, y] = getCenter(ball);
% plot the coordinates of the bottom of the ball
plot(graph, x, y-10, '*r');
% while the ball has not yet bounced off the right side of the window
while(x < 510)
% if the Wiimote's HOME button is pressed
if (isButtonPressed('HOME'))
% clear the current ball
hide(ball);
hide(t3);
% close existing graph
close(2);
% and start again
resetWindow(bb_figure);
t3 = text(250, 290, dir3, 'HorizontalAlignment', 'center', 'FontSize', 12);
end %if
% constant horizontal velocity
xMove(ball, 0.25*xVel);
redraw;
% get the new vertical velocity
getYVel(y);
% if the updated y-velocity will put the ball "into the ground",
% only move it enough so that it will stop when it hits the bottom
if ((y + yVel) < 10)
yMove(ball, 10-y);
% otherwise move it as far as the new y-velocity dictates
else
yMove(ball, yVel);
end %if
% for smoother animation
redraw;
pause(0.001);
% get the ball's new coordinates
[x, y] = getCenter(ball);
% plot the position
plot(graph, x, y-10, '*r');
pause(0.001);
end %while
hide(t3);
pause(0.5);
% close bouncing ball figure
close(bb_figure);
% done using the Wiimote
disconnectWiimote();
% if the Wiimote is not connected
else
text(250, 290, 'Wiimote not connected. Please try again.', ...
'HorizontalAlignment', 'center', 'FontSize', 12);
pause(2);
% close figures
close;
close;
end %if
end