function toRet = bowling(parameters)
global Alpha;
global otherInfo;

otherInfo = [];

theta_o = parameters(1)
y_o = parameters(2)
v_o = parameters(3)
w_o = parameters(4)

mu_oil = .04;
mu_dry = .20;
w_lane = 1.0668;
l_lane = 18.288;
l_dry = 12.0;

pins = [31.2 0 ; 31.2 12 ; 31.2 24 ; 31.2 36 ; ... 
        20.8 6 ; 20.8 18 ; 20.8 30 ; ...
        10.4 12 ; 10.4 24 ; 0 18];
pins = .0254 * (pins + repmat([0 -18], 10, 1));
pins = pins + repmat([(l_lane) (w_lane/2)], 10, 1);
x_firstPin = pins(10, 1);


r_ball = .10795;
r_delta = .001;
m_ball = 5.44; 
%m_ball = 7.257;
inertiaConstant = 2/5*m_ball*r_ball*r_ball;
I = inertiaConstant * [1 0 0 ; 0 1 0 ; 0 0 1];


%R_delta = [0; r_delta ; 0]; % orient later
R_delta = [-r_delta ; 0 ; 0]; % orient later
W = [-w_o ; 0 ; 0];
Alpha = [0 ; 0 ; 0];

R_cm = -R_delta+[0 ; y_o ; 0];
Velo = [v_o*cos(theta_o); v_o*(sin(theta_o)) ; 0];

options = [];
initData = [R_cm ; Velo ; R_delta ; W];

approxTime = l_lane/Velo(1)*1.25;

[t,y]=ode45('ballode', [0,approxTime], initData, options, l_dry, mu_oil, mu_dry, r_ball, m_ball, r_delta, I);

y = dataThatMatters(y, l_lane);

hold off
plot(y(:, 1), y(:, 2));
hold on
plot(pins(:, 1), pins(:, 2), 'bo');
ylim([0, w_lane]);
xlim([0, l_lane+.85]);

%plot(y(:, 1), y(:, 7)/r_delta, 'g');
%plot(y(:, 1), y(:, 8)/r_delta, 'g');
%plot(y(:, 1), y(:, 9)/r_delta, 'g');
%plot(y(:, 1), y(:, 10)/w_o, 'k');
%plot(y(:, 1), y(:, 11)/w_o, 'k');
%plot(y(:, 1), y(:, 12)/w_o, 'k');
%plot(otherInfo(:, 1), otherInfo(:, 2), 'r');%/

leftGutterError = (max(0, (max(y(:, 2)) - w_lane)))^2
rightGutterError = (min(0, min(y(:, 2))))^2

impact = getImpact(y, x_firstPin);
IDEAL_Y_IMPACT = w_lane/2 - 3*.0254;
yError = (IDEAL_Y_IMPACT - impact(2))^2

IDEAL_POCKET_ANGLE = 6*pi/180;
angleError = (IDEAL_POCKET_ANGLE - impact(3))^2


toRet = angleError + yError + (rightGutterError + leftGutterError)*1000000;

return

function toRet = dataThatMatters(y, l_lane)
    toRet = [];
    for i = 1:size(y, 1)
        if (y(i, 1) <= l_lane+.85)
            toRet = [toRet ; y(i, :)];
        end
    end
return

function toRet = getImpact(y, x_firstPin)
    toRet = [0 ; 0 ; 0];
    for i = 1:size(y, 1)
        if (y(i, 1) > x_firstPin)
            toRet = [y(i, 1) ; y(i, 2) ; atan2(y(i, 5), y(i, 4))];
            break;
        end
    end
return